最近一直在学各种分布式的内容,学到了dubbo分布式服务框架就想写个小demo使用一下,但是由于我要整合在SpringBoot框架中,SpringBoot框架毕竟提倡的是java注解配置,XML文件不提倡,所以只能利用@ImportResource这个注解来引入dubbo配置的XML文件。
首先是Maven项目的Pom文件:
除了基本的SpringBoot的基本web配置jar包需要添加以下依赖(服务提供方和消费方一样):
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
|
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>
2.5
.
3
</version>
<exclusions>
<exclusion>
//这个排除低版本的spring
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>
3.4
.
6
</version>
<exclusions>
<exclusion>
//冲突排除
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
//冲突排除
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>
0.1
</version>
</dependency>
|
接下来是服务的提供方:
服务接口代码
1
2
3
|
public
interface
TestService {
public
String sayHello();
}
|
接口实现代码
1
2
3
4
5
6
7
|
@Service
(
"testService"
)
public
class
TestServiceImpl
implements
TestService {
@Override
public
String sayHello() {
return
"hello dubbo"
;
}
}
|
接下来是服务方的XML配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?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=
"provider"
/>
<!-- 使用zookeeper作为注册中心 -->
<dubbo:registry protocol=
"zookeeper"
address=
"zookeeper://127.0.0.1:2181"
/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:service
interface
=
"com.distributed.TestService"
ref=
"testService"
></dubbo:service>
</beans>
|
SpringBoot启动类(注意要引入XML配置文件):
1
2
3
4
5
6
7
8
9
|
@SpringBootApplication
@ImportResource
(
"classpath:provider.xml"
)
//很重要
public
class
MyApplication {
public
static
void
main(String[] args) {
SpringApplication.run(MyApplication.
class
,args);
}
}
|
接下来是服务消费方
这里接口要和服务的提供方保持一致不能有区别。
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?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=
"consumer"
/>
<!-- zookeeper作为注册中心 -->
<dubbo:registry protocol=
"zookeeper"
address=
"zookeeper://127.0.0.1:2181"
/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference
interface
=
"com.distributed.TestService"
id=
"testService"
></dubbo:reference>
</beans>
|
Controller类:
1
2
3
4
5
6
7
8
9
10
|
@RestController
public
class
MyController {
@Autowired
private
TestService testService;
@RequestMapping
public
String home(){
return
testService.sayHello();
}
}
|
启动类:
1
2
3
4
5
6
7
|
@SpringBootApplication
@ImportResource
(
"classpath:consumer.xml"
)
public
class
MyApplication {
public
static
void
main(String[] args) {
SpringApplication.run(MyApplication.
class
,args);
}
}
|
最后运行服务端和消费端,访问消费端的controller成功调用服务。。。