一、本文目的
通過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
);
}
}
}
|