SpringBoot下的Dubbo和Zookeeper整合

最近一直在学各种分布式的内容,学到了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
             com.alibaba
             dubbo
             2.5 . 3
            
                   //这个排除低版本的spring
                     org.springframework
                     spring
                
            
        
        
             org.apache.zookeeper
             zookeeper
             3.4 . 6
            
                 //冲突排除
                     org.slf4j
                     slf4j-log4j12
                
                 //冲突排除
                     log4j
                     log4j
                
            
        
        
             com.github.sgroschupf
             zkclient
             0.1
        

接下来是服务的提供方:

服务接口代码

 

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
"1.0"  encoding= "UTF-8" ?>
"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 ">
    
     "provider"  />
    
     "zookeeper"  address= "zookeeper://127.0.0.1:2181"  />
    
     interface = "com.distributed.TestService"  ref= "testService" >

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
"1.0"  encoding= "UTF-8" ?>
"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 ">
    
     "consumer"  />
    
     "zookeeper"  address= "zookeeper://127.0.0.1:2181"  />
    
     interface = "com.distributed.TestService"  id= "testService" >

 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成功调用服务。。。

你可能感兴趣的:(SpringBoot下的Dubbo和Zookeeper整合)