1、搭建Maven工程:java
右鍵new->projecrt -> maven project git
2、配置Dubbo:github
DUBBO是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,是阿里巴巴SOA服務化治理方案的核心框架。算法
dubbo的工做模式:spring
節點角色說明:apache
調用關係說明:app
具體例子:負載均衡
一、編寫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.fan.test</groupId> <artifactId>dubboTest</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.5.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.9</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version> <exclusions> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.netflix.curator</groupId> <artifactId>curator-framework</artifactId> <version>1.1.16</version> </dependency> </dependencies> </project>
二、配置提供者:maven
package com.alibaba.dubbo.demo; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"}); context.start(); System.out.println("請按任意鍵退出"); System.in.read(); // 按任意鍵退出 } }
package com.alibaba.dubbo.demo; /** * 提供者和消費者公用接口 * 若是提供者和消費者是分別在兩個不一樣的工程中,則公共接口必須如出一轍,包括包名 */ public interface DemoService { String sayHello(String name); }
package com.alibaba.dubbo.demo.provider; import com.alibaba.dubbo.demo.DemoService; public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hello " + name; } }
<?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" 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 "> <!-- 提供方應用信息,用於計算依賴關係 --> <dubbo:application name="dubbo-test-service" /> <!-- 使用multicast廣播註冊中心暴露服務地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" /> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 聲明須要暴露的服務接口 --> <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" /> <!-- 和本地bean同樣實現服務 --> <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" /> </beans>
三、配置消費者:
package com.alibaba.dubbo.demo; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"}); context.start(); DemoService demoService = (DemoService)context.getBean("demoService"); // 獲取遠程服務代理 String res = demoService.sayHello("world"); // 執行遠程方法 System.out.println( res ); // 顯示調用結果 } }
<?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" 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 "> <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方同樣 --> <dubbo:application name="dubbo-test-consumer" /> <!-- 使用multicast廣播註冊中心暴露發現服務地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService --> <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" /> </beans>
3、啓動zookeeper:
保證啓動的zookeeper的ip和端口號和provider.xml、consummer.xml中的 <dubbo:registry address="zookeeper://127.0.0.1:2181" /> 保持一致。
四:運行:
啓動zookpeeper、啓動provider.java 而後啓動consummer.java,最後的運行結果以下:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Hello world
源碼下載: dubboTest.zip