Dubbo出現的目的是爲了應對如今高併發,高數據量請求的問題。目前的垂直應用架構已經沒法知足如今大數據的衝擊,SOA就應運而生,而Dubbo在國內使用的仍是比較多,穩定性也比較不錯。java
Provider: 暴露服務的服務提供方。
Consumer: 調用遠程服務的服務消費方。
Registry: 服務註冊與發現的註冊中心。
Monitor: 統計服務的調用次調和調用時間的監控中心。
Container: 服務運行容器。git
服務容器負責啓動,加載,運行服務提供者。github
服務提供者在啓動時,向註冊中心註冊本身提供的服務。web
服務消費者在啓動時,向註冊中心訂閱本身所需的服務。算法
註冊中心返回服務提供者地址列表給消費者,若是有變動,註冊中心將基於長鏈接推送變動數據給消費者。spring
服務消費者,從提供者地址列表中,基於軟負載均衡算法,選一臺提供者進行調用,若是調用失敗,再選另外一臺調用。數據庫
服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發送一次統計數據到監控中心。apache
(1) 連通性:api
註冊中心負責服務地址的註冊與查找,至關於目錄服務,服務提供者和消費者只在啓動時與註冊中心交互,註冊中心不轉發請求,壓力較小緩存
監控中心負責統計各服務調用次數,調用時間等,統計先在內存彙總後每分鐘一次發送到監控中心服務器,並以報表展現
服務提供者向註冊中心註冊其提供的服務,並彙報調用時間到監控中心,此時間不包含網絡開銷
服務消費者向註冊中心獲取服務提供者地址列表,並根據負載算法直接調用提供者,同時彙報調用時間到監控中心,此時間包含網絡開銷
註冊中心,服務提供者,服務消費者三者之間均爲長鏈接,監控中心除外
註冊中心經過長鏈接感知服務提供者的存在,服務提供者宕機,註冊中心將當即推送事件通知消費者
註冊中心和監控中心所有宕機,不影響已運行的提供者和消費者,消費者在本地緩存了提供者列表
註冊中心和監控中心都是可選的,服務消費者能夠直連服務提供者
(2) 健狀性:
監控中心宕掉不影響使用,只是丟失部分採樣數據
數據庫宕掉後,註冊中心仍能經過緩存提供服務列表查詢,但不能註冊新服務
註冊中心對等集羣,任意一臺宕掉後,將自動切換到另外一臺
註冊中心所有宕掉後,服務提供者和服務消費者仍能經過本地緩存通信
服務提供者無狀態,任意一臺宕掉後,不影響使用
服務提供者所有宕掉後,服務消費者應用將沒法使用,並沒有限次重連等待服務提供者恢復
(3) 伸縮性:
註冊中心爲對等集羣,可動態增長機器部署實例,全部客戶端將自動發現新的註冊中心
服務提供者無狀態,可動態增長機器部署實例,註冊中心將推送新的服務提供者信息給消費者
(4) 升級性:
當服務集羣規模進一步擴大,帶動IT治理結構進一步升級,須要實現動態部署,進行流動計算,現有分佈式服務架構不會帶來阻力:
能夠得出,註冊中心若宕機,只要沒有註冊新的服務,服務提供者和消費者仍是能夠根據本地緩存進行溝通。
而且註冊中心不是請求轉發中心,因此壓力是比較小的。
一、ZooKeeper的配置安裝
http://blog.csdn.net/u013142781/article/details/50395650
二、Dubbo-admin配置安裝
http://blog.csdn.net/u013142781/article/details/50396621
三、工程API工程建立
工程使用Maven+SpringMVC的方式構建
ITestService.java
package com.gege.service; public interface ITestService { public String getName(); }
DubboInit.java
package com.gege.tool; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.apache.commons.lang.exception.ExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboInit extends HttpServlet { /** * */ private static final long serialVersionUID = 8279515033200832L; private Logger logger = (Logger) LoggerFactory.getLogger(DubboInit.class); /** * 啓動dubbo容器 */ public void init() throws ServletException { try { startApplicationContext(); } catch (Exception e) { e.printStackTrace(); logger.error(ExceptionUtils.getFullStackTrace(e)); } } public static ApplicationContext applicationContext = null; /** * 啓動spring容器 * @return */ public static ApplicationContext startApplicationContext() { if (applicationContext == null) { applicationContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml"); } return applicationContext; }; }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gege</groupId> <artifactId>api</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.2.8.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- spring相關 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>utf8</encoding> <fileNameMapping>no-version</fileNameMapping> </configuration> </plugin> </plugins> </build> </project>
四、Dubbo服務提供者工程建立與配置
工程使用Maven+SpringMVC的方式構建
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gege</groupId> <artifactId>provider</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>3.2.8.RELEASE</spring.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- 添加dubbo依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <!-- 添加zk客戶端依賴 --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- spring相關 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.gege</groupId> <artifactId>api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> </dependencies> <build> <resources> <resource> //靜態資源文件掃描地址 <directory>${project.basedir}/src/main/resources</directory> <includes> <include>*.*</include> </includes> <filtering>true</filtering> </resource> </resources> </build> <profiles> <profile> <id>local</id> <properties> //這是zookeeper的地址,要根據實際狀況進行配置,默認zookeeper://127.0.0.1:2181 <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address> //dubbo暴露的端口配置 <dubbo.gege.port>29014</dubbo.gege.port> //工程的服務集合,${user.name}表示當前PC的UserName <dubbo.gege.group>${user.name}</dubbo.gege.group> //dubbo服務版本號 <dubbo.gege.version>1.0.0</dubbo.gege.version> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>dev</id> <properties> <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address> <dubbo.gege.port>29014</dubbo.gege.port> <dubbo.gege.group>${user.name}</dubbo.gege.group> <dubbo.gege.version>1.0.0</dubbo.gege.version> </properties> </profile> <profile> <id>test</id> <properties> <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address> <dubbo.gege.port>29014</dubbo.gege.port> <dubbo.gege.group>${user.name}</dubbo.gege.group> <dubbo.gege.version>1.0.0</dubbo.gege.version> </properties> </profile> <profile> <id>product</id> <properties> <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address> <dubbo.gege.port>29014</dubbo.gege.port> <dubbo.gege.group>${user.name}</dubbo.gege.group> <dubbo.gege.version>1.0.0</dubbo.gege.version> </properties> </profile> </profiles> </project>
TestServiceImpl.java
package com.gege.service.impl; import org.springframework.stereotype.Service; import com.gege.service.ITestService; @Service public class TestServiceImpl implements ITestService { public String getName() { return "gege"; } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> //註解掃描 <context:component-scan base-package="com.gege"/> <context:annotation-config /> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:statics.properties</value> </list> </property> </bean> <import resource="dubbo-provider.xml"/> </beans>
dubbo-provider.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <dubbo:application name="provider" /> <dubbo:registry protocol="dubbo" address="${dubbo.gege.address}" port="${dubbo.gege.port}" /> <dubbo:protocol name="dubbo" port="${dubbo.gege.port}" /> <dubbo:monitor protocol="registry" /> <dubbo:service ref="testServiceImpl" interface="com.gege.service.ITestService" group="${dubbo.gege.group}" version="${dubbo.gege.version}"/> </beans>
log4j.properties
log4j.rootLogger=debug, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout #Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=[%d] %t (%F:%L) %-5p - %m%n log4j.appender.R.Encoding=UTF-8 log4j.appender.R=org.apache.log4j.DailyRollingFileAppender log4j.appender.R.DatePattern='_'yyyy-MM-dd_HH'.log' log4j.appender.R.File=${catalina.home}/logs/1.0_order_provider log4j.appender.R.ImmediateFlush=true log4j.appender.R.MaxFileSize=10000KB # Keep one backup file log4j.appender.R.MaxBackupIndex=20 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=[%d] %t %c (%F:%L) %-5p - %m%n log4j.category.com.cheguo=info log4j.category.org.springframework=info log4j.category.freemarker=info
statics.properties
dubbo.gege.address=${dubbo.gege.address} dubbo.gege.port=${dubbo.gege.port} dubbo.gege.version=${dubbo.gege.version} dubbo.gege.group=${dubbo.gege.group} dubbo.gege.check=false dubbo.gege.retries=0 dubbo.gege.timeout=10000
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>provider</display-name> <servlet> <servlet-name>dubboService</servlet-name> <servlet-class>com.gege.tool.DubboInit</servlet-class> <load-on-startup>1</load-on-startup> </servlet> </web-app>
五、Dubbo服務消費者工程建立與配置
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gege</groupId> <artifactId>consumer</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>3.2.8.RELEASE</spring.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- spring相關 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.gege</groupId> <artifactId>provider</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.gege</groupId> <artifactId>api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> </dependencies> <build> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory> <includes> <include>*.*</include> </includes> <filtering>true</filtering> </resource> </resources> </build> <profiles> <profile> <id>local</id> <properties> <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address> <dubbo.gege.group>${user.name}</dubbo.gege.group> <dubbo.gege.version>1.0.0</dubbo.gege.version> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>dev</id> <properties> <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address> <dubbo.gege.group>${user.name}</dubbo.gege.group> <dubbo.gege.version>1.0.0</dubbo.gege.version> </properties> </profile> <profile> <id>test</id> <properties> <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address> <dubbo.gege.group>${user.name}</dubbo.gege.group> <dubbo.gege.version>1.0.0</dubbo.gege.version> </properties> </profile> <profile> <id>product</id> <properties> <dubbo.gege.address>zookeeper://127.0.0.1:2181</dubbo.gege.address> <dubbo.gege.group>${user.name}</dubbo.gege.group> <dubbo.gege.version>1.0.0</dubbo.gege.version> </properties> </profile> </profiles> </project>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <task:annotation-driven /> <context:component-scan base-package="com.gege"/> <context:annotation-config /> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:statics.properties</value> </list> </property> </bean> <import resource="dubbo-customer.xml"/> </beans>
dubbo-customer.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <dubbo:application name="consumer"></dubbo:application> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry id="gege_center" protocol="dubbo" address="${dubbo.gege.address}" /> <dubbo:provider group="${dubbo.gege.group}" version="${dubbo.gege.version}" registry="gege_center"/> <dubbo:consumer group="${dubbo.gege.group}" version="${dubbo.gege.version}" registry="gege_center" check="false" /> <!-- 聲明須要引用的服務接口 --> <dubbo:reference id="testService" interface="com.gege.service.ITestService" retries="${dubbo.gege.retries}" timeout="${dubbo.gege.timeout}"/> </beans>
log4j.properties
同上
statics.properties
dubbo.gege.address=${dubbo.gege.address} dubbo.gege.group=${dubbo.gege.group} dubbo.gege.version=${dubbo.gege.version} dubbo.gege.retries=0 dubbo.gege.timeout=10000
ConsumerServiceTest.java
package com.gege.service; import java.io.IOException; import org.apache.log4j.Logger; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ConsumerServiceTest { Logger logger=Logger.getLogger(ConsumerServiceTest.class); public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); context.start(); ITestService testService = (ITestService) context.getBean("testService"); System.out.println(testService.getName()); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
六、運行測試
運行zookeeper,雙擊zkServer.cmd
運行dubbo-admin,雙擊Tomcat7w.exe,點擊start
在eclipse中用tomcat運行服務提供者,可能存在超時,配置tomcat的timeout配置就能夠了
在eclipse中用tomcat運行服務消費者
消費者工程的ConsumerServiceTest下,右鍵
就能夠查看是否成功
正常結果:
gege
更多內容能夠關注微信公衆號,或者訪問AppZone網站