使用Dubbo结合Zookeeper和Spring,
是使用比较广泛的一种组合,下面参考官方文档,做个简单的示例,一步步搭建一个使用dubbo结合Zookeeper和Spring的Demo工程。
代码已经上传到github,地址:https://github.com/bingyue/dubbodemo
1.集成Zookeeper
(1)安装Zookeeper
(2)启动Zookeeper服务
2.配置Dubbo的管理界面
Dubbo官方提供了几个管理工具,包括管理控制台dubbo-admin及简易监控中心等。
这里的Demo学习使用了最新的稳定的2.5.3版本。
官网指出在阿里巴巴内部广泛使用的GA版本为2.4.9,并且推荐此版本,
在实际项目中可以使用这一稳定版本。
(1)下载dubbo-admin,修改相关的配置
下载地址http://dubbo.io/Download-zh.htm,下载后解压war包,
找到dubbo.properties配置文件,修改其中的zookeeper地址为上面部署的zk地址。
(2)启动dubbo-admin工程
把工程文件夹放在Tomcat的工程目录下,启动后登录即可。
3.创建Demo项目,集成Spring
Demo项目主要是使用了CXF整合Spring创建WebService的技术,关于CXF可以参考:
WebService CXF学习(高级篇1):整合Spring框架
(1)创建服务提供接口项目
服务端接口需要单独打包,服务提供方和消费方共享,同时被提供端和消费端依赖。
1.新建Maven项目
创建dubbo-demo-api工程,pom文件如下:
1
2
3
4
5
|
<
groupId
>dubbo-demo</
groupId
>
<
artifactId
>dubbo-demo-api</
artifactId
>
<
version
>0.1</
version
>
<
packaging
>jar</
packaging
>
<
name
>dubbo-demo-api</
name
>
|
2.编写服务接口
1
2
3
|
public
interface
TestService {
public
String sayHello(String name);
}
|
(2)创建项目实现接口
1.创建具体接口实现项目
1
2
3
4
5
6
7
8
9
|
public
class
TestServiceImpl
implements
TestService {
/**
* 服务提供方实现接口:(对服务消费方隐藏实现)
*/
public
String sayHello(String name) {
System.out.println(
"调用provider服务"
);
return
"hello"
+name;
}
}
|
添加api的依赖,添加Spring、dubbo和zookeeper的依赖,pom文件如下:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
<groupId>dubbo-demo</groupId>
<artifactId>dubbo-demo-provider</artifactId>
<version>
0.1
</version>
<packaging>jar</packaging>
<name>dubbo-demo-provider</name>
<properties>
<project.build.sourceEncoding>UTF-
8
</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>
3.8
.
1
</version>
<scope>test</scope>
</dependency>
<!-- 添加Dubbo服务接口的依赖 -->
<dependency>
<groupId>dubbo-demo</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>
0.1
</version>
</dependency>
<!-- 添加Spring context依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context</artifactId>
<version>
3.1
.
2
.RELEASE</version>
</dependency>
<!-- 引入Dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>
2.5
.
3
</version>
</dependency>
<!-- zookeeper依赖 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>
3.4
.
6
</version>
</dependency>
<!-- zookeeper客户端 zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>
0.1
</version>
</dependency>
</dependencies>
|
2.暴露服务,整合Spring完成服务提供者
配置provider.xml:
1
2
3
4
5
6
7
8
9
10
|
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name=
"dubbo-demo-provider"
/>
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address=
"multicast://192.168.106.129:2182"
/>
<!-- 用dubbo协议在
20880
端口暴露服务 -->
<dubbo:protocol name=
"dubbo"
port=
"20880"
/>
<!-- 声明需要暴露的服务接口 -->
<dubbo:service
interface
=
"bingyue.dubbo.provider.TestService"
ref=
"testService"
/>
<!-- 和本地bean一样实现服务 -->
<bean id=
"testService"
class
=
"bingyue.dubbo.provider.TestServiceImpl"
/>
|
(3)创建服务消费端项目
1.创建项目和相关配置
在pom.xml中依赖接口项目:
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
|
<
groupId
>dubbo-demo</
groupId
>
<
artifactId
>dubbo-demo-consumer</
artifactId
>
<
version
>0.1</
version
>
<
packaging
>jar</
packaging
>
<
dependencies
>
<!-- 添加Spring context依赖 -->
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>org.springframework.context</
artifactId
>
<
version
>3.1.2.RELEASE</
version
>
</
dependency
>
<
dependency
>
<
groupId
>junit</
groupId
>
<
artifactId
>junit</
artifactId
>
<
version
>3.8.1</
version
>
<
scope
>test</
scope
>
</
dependency
>
<!-- 添加Dubbo服务接口的依赖 -->
<
dependency
>
<
groupId
>dubbo-demo</
groupId
>
<
artifactId
>dubbo-demo-api</
artifactId
>
<
version
>0.1</
version
>
</
dependency
>
</
dependencies
>
|
配置consumer.xml:
1
2
3
4
5
6
7
8
9
|
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<
dubbo:application
name="dubbo-demo-consumer" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<
dubbo:registry
address="multicast://192.168.106.129:2182" />
<!-- 把接口的声明引入到本项目中,通过jar或者maven依赖都可以 -->
<!-- 生成远程服务代理,可以和使用本地bean一样使用服务者提供的服务 -->
<
dubbo:reference
id="testService" interface="bingyue.dubbo.provider.TestService" />
|
2.配置和启动消费者服务
加载配置文件后,消费端调用服务端接口的方式有多种,可以从上下文中获取bean,也可以使用注解。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public
class
Tester {
//使用注解或者使用远程服务代理取得接口实例
// @Autowired TestService testService;
public
static
void
main(String[] args){
//加载同级目录下的消费者配置文件
ClassPathXmlApplicationContext ctx=
new
ClassPathXmlApplicationContext(
"consumer.xml"
);
ctx.start();
TestService testService = (TestService) ctx.getBean(
"testService"
);
String result=testService.sayHello(
"cnblogs"
);
System.out.println(result);
}
}
|
3.使用Maven结合Dubbo
这种Dubbo使用方式可以不用考虑Maven如何单独对接口打包,
在顶层的服务提供方项目中编写接口,
消费端项目会依赖服务提供方项目的jar包,从中获得接口实例,
然后在服务方项目中新建单独的子项目去编写接口的实现类,
这样通过Maven的继承,将消费端项目和具体的接口实现类代码分离开。
本文转自邴越博客园博客,原文链接:http://www.cnblogs.com/binyue/p/3723460.html,如需转载请自行联系原作者