一、本文目的
通过Dubbo的官方Demo介绍,学会搭建一个简单的Dubbo程序,包括服务端、客户端、接口等。
二、Demo概况
1、Demo分为三个项目
a) dubbo-demo-api:服务接口,服务端和客户端都需要引用
b)dubbo-demo-provider:包含对服务接口的引用和实现
c)dubbo-demo-consumer:包含对服务接口的引用和使用
2、Demo三个项目的父项目是:dubbo-demo
三、Demo引用的Jar包说明
- JDK:1.6
- Dubbo版2号:2.5.4-SNAPSHOT
- Spring版本号:3.2.16.RELEASE<
- ZooKeeper版本号:3.3.3
- Jedis版本号:2.1.0
- Netty版本号:3.2.5.Final
- 其它:参考pom.xml https://github.com/alibaba/dubbo/blob/master/pom.xml
四、Demo代码说明
1、服务接口:dubbo-demo-api
本项目只包含一个接口及一个接口方法
1 2 3 4 5 6 7 | package com.alibaba.dubbo.demo; public interface DemoService { String sayHello(String name); } |
2、服务实现:dubbo-demo-provider
a) 服务端包含了对dubbo-demo-api的引用及实现
1 2 3 4 5 | < dependency > < groupId >com.alibaba</ groupId > < artifactId >dubbo-demo-api</ artifactId > < version >${project.parent.version}</ version > </ dependency > |
b) 配置文件(dubbo-demo-provider.xml)
其中<dubbo:servcie> 定义一个对外提供的接口,通过ref关联到具体的实现代码
1 2 3 4 5 6 7 8 9 10 11 | xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd < bean id = "demoService" class = "com.alibaba.dubbo.demo.provider.DemoServiceImpl" /> < dubbo:service interface = "com.alibaba.dubbo.demo.DemoService" ref = "demoService" /> </ beans > |
c) 实现代码(DemoServiceImpl)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.alibaba.dubbo.demo.provider; import java.text.SimpleDateFormat; import java.util.Date; import com.alibaba.dubbo.demo.DemoService; import com.alibaba.dubbo.rpc.RpcContext; public class DemoServiceImpl implements DemoService { public String sayHello(String name) { System.out.println( "[" + new SimpleDateFormat( "HH:mm:ss" ).format( new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress(); } } |
3、服务消费:dubbo-demo-consumer
a) 消费端也包含了对dubbo-demo-api的引用
1 2 3 4 5 | < dependency > < groupId >com.alibaba</ groupId > < artifactId >dubbo-demo-api</ artifactId > < version >${project.parent.version}</ version > </ dependency > |
b) 配置文件(dubbo-demo-consumer.xml)
通过<dubbo:reference>引用一个服务接口,客户端使用远程接口方法就和调用本地方法一致
1 2 3 4 5 6 7 8 9 | xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd < dubbo:reference id = "demoService" interface = "com.alibaba.dubbo.demo.DemoService" /> </ beans > |
c) 消费端调用代码:DemoAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.alibaba.dubbo.demo.consumer; import java.text.SimpleDateFormat; import java.util.Date; import com.alibaba.dubbo.demo.DemoService; public class DemoAction { private DemoService demoService; public void setDemoService(DemoService demoService) { this .demoService = demoService; } public void start() throws Exception { for ( int i = 0 ; i < Integer.MAX_VALUE; i ++) { try { String hello = demoService.sayHello( "world" + i); System.out.println( "[" + new SimpleDateFormat( "HH:mm:ss" ).format( new Date()) + "] " + hello); } catch (Exception e) { e.printStackTrace(); } Thread.sleep( 2000 ); } } } |