标签:
详细介绍请参照官方地址:http://alibaba.github.io/dubbo-doc-static/Home-zh.htm,不再重复描述,本文主要记录了详细的开发整合步骤。
这个网上很多教程,大家可以google/baidu,或者直接下载我已经构建好的。我做了更改,兼容JDK1.8.
网盘下载 http://pan.baidu.com/s/1sjFqt8T 密码:60u7
Dubbo 缺省配置通过 multicast 注册中心广播实现 Provider 和 Consumer 之间的简单远程过程调用(Simple RPC),不需要通过 Registry 注册中心进行注册调度,类似于spring rmi remoting调用,但由于不是Cluster部署,所以作为分布式RPC框架官方建议使用 Zookeeper 作为Registry注册中心服务器(同样支持Redis)实现服务的注册、发现、路由功能。
Dubbo在Zookeeper服务器端只增加了dubbo数据节点(如下图),无需其他任何配置,所以只需安装或使用现有 Zookeeper 服务器即可,关于Zookeeper的安装部署可以参照之前的博文:http://my.oschina.net/ihanfeng/blog/525255
1、创建基于maven管理的dubbo-provider项目。
<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.hanfeng.dubbo</groupId> <artifactId>dubbo-provider</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>dubbo-provider Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.8.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</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> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>WebRoot</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
2、创建测试服务接口(HelloService)
package com.hanfeng.dubbo.provider; public interface HelloService { public String sayHello(String text); }
3、创建服务接口实现类(HelloServiceImpl)
package com.hanfeng.dubbo.provider.impl; import com.alibaba.dubbo.rpc.RpcContext; import com.hanfeng.dubbo.provider.HelloService; public class HelloServiceImpl implements HelloService{ @Override public String sayHello(String text) { return "hello "+ text + "/n response form provider: " + RpcContext.getContext().getLocalAddress(); } }
4、通过spring 集成并配置dubbo测试服务,同时指定registry的zookeeper服务器地址。
<?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 服务提供者应用名称 --> <dubbo:application name="dubbo-provider" /> <!--dubbo 注册中心--> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!--服务提供者 端口--> <dubbo:protocol name="dubbo" port="30001" /> <!--dubbo提供服务--> <dubbo:service interface="com.hanfeng.dubbo.provider.HelloService" ref="helloService" /> <!--spring bean 对象--> <bean id="helloService" class="com.hanfeng.dubbo.provider.impl.HelloServiceImpl" /> </beans>
5、编写控制台程序启动spring容器,编译并打包Provider.jar
package com.hanfeng.dubbo.test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ProviderTest { @SuppressWarnings("resource") public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-dubbo-provider.xml"}); context.start(); System.out.println("请按任意键退出"); System.in.read(); // 按任意键退出 } }
1、通过spring 配置指定registry的zookeeper地址,实现对dubbo远程服务的调用
<?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-consumer" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用helloService --> <dubbo:reference interface="com.hanfeng.dubbo.provider.HelloService" id="helloService" /> </beans>
2、编写调用测试客户端代码,从容器中获取远程bean并调用。
package com.hanfeng.dubbo.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hanfeng.dubbo.provider.HelloService; public class ConsumerTest { @SuppressWarnings("resource") public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-dubbo-consumer.xml"}); context.start(); HelloService hellService = (HelloService)context.getBean("helloService"); // 获取远程服务代理 String res = hellService.sayHello("world"); // 执行远程方法 System.out.println( res ); // 显示调用结果 } }
首先启动zookeeper服务器,再启动dubbo服务器。
测试开始,我们首先运行ProviderTest,然后运行ConsumerTest
输出结果:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. hello world/n response form provider: 192.168.5.13:30001
登录 http://127.0.0.1:8080/,我们发现服务数、应用数、提供者都有对应的数据。
源代码:链接:http://pan.baidu.com/s/1sj8AyNb 密码:hwd* (上面案例还需要源代码得话,密码最后一个字母自己尝试)
标签:
原文地址:http://my.oschina.net/ihanfeng/blog/525322