Springboot整合Dubbo和zookeeper-----XML版,即是服务消费者又是服务提供者

笔者个人更喜欢SpringCloud,也更喜欢它的一系列的解决方案。但是技术不在喜不喜欢,而是要符合市场需求。dubbo也是一个成熟的分布式服务的解决方案。所以笔者现在分享Xml方式SpringBoot整合Dubbo和Zookeeper。

背景原因,笔者也尝试过使用无xml的方式使用starter的方式整合过Dubbo,但是很遗憾,可能是笔者能力有限,在配置多个注册中心时笔者一直不成功,无赖的情况下,笔者只能选择xml方式。

首先看看项目结构:

Springboot整合Dubbo和zookeeper-----XML版,即是服务消费者又是服务提供者_第1张图片

其中dubbo-api中是定义接口和实体类的模块,dubbo1和dubbo2两者既是服务消费者,又是服务提供者。

dubbo1和dubbo2都要maven依赖于dubbo-api。

dubbo-api模块就不再多说,我们具体说说dubbo1和dubbo2

dubbo1:

首先是dubbo1的pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.7.RELEASE
         
    
    com.wangye
    dubbo1
    0.0.1-SNAPSHOT
    dubbo1
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        

        
            org.apache.dubbo
            dubbo
            2.7.0
        


        
        
            org.apache.zookeeper
            zookeeper
            3.4.14
        

        
            org.apache.curator
            curator-framework
            4.1.0
        

        
            org.apache.curator
            curator-recipes
            4.1.0
        
        

        
        
            com.wangye
            dubbo-api
            1.0-SNAPSHOT
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


重点依赖是下面这些:



        
            org.apache.dubbo
            dubbo
            2.7.0
        


        
        
            org.apache.zookeeper
            zookeeper
            3.4.14
        

        
            org.apache.curator
            curator-framework
            4.1.0
        

        
            org.apache.curator
            curator-recipes
            4.1.0
        
        

上面主要是dubbo依赖和zk。

那么看看我们dubbo1的application.properties

server.port=8081

#dubbo配置
# zk注册中心url
registryUrl=zookeeper1.XXXXX.org:16001,zookeeper2.XXXXX.org:16002,zookeeper3.XXXXX.org:16003
#dubbo1提供服务的版本号
dubbo1Version=dubbo1-DEV
#dubbo2提供服务的版本号
dubbo2Version=dubbo2-DEV
#dubbo服务的端口号
dubboPort=20002

注意上面的配置了多个注册中心

然后看看dubbo1的项目结构:

Springboot整合Dubbo和zookeeper-----XML版,即是服务消费者又是服务提供者_第2张图片

既然我们有实现的类,那么就看看:

HelloServiceImpl.java:

import com.wangye.dubboapi.api.HelloService;
import org.springframework.stereotype.Service;

@Service("helloService")
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

注意这里使用的service的注解是Spring的注解,接下来只要在provider.xml提供服务的标签中加上ref关联这个服务类就好

下面看看dubbo-config.xml:




    
    
    
 
    
    
    

 	
 	
 	 
 	
 	 

这里配置了dubbo应用的名字,注册中心协议、url、以及使用的客户端,配置了dubbo消费端免检查,提供服务的版本。以及import消费者和提供者文件配置。

接下来是provider.xml的配置:




 	
 

上面要注意,ref属性的值一定要和@Service(”helloService“) 里的helloService是一样的。

consumer.xml的配置:




    

最后看一下controller:

import com.wangye.dubboapi.api.ByeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ByeController {

    //这里调用了dubbo2模块提供的服务
    @Autowired
    private ByeService byeService;

    @RequestMapping("/sayBye/{name}")
    public String sayBye(@PathVariable String name){
        return byeService.sayBye(name);
    }

}

dubbo2配置:

dubbo2的配置和dubbo1的配置几乎以模一样,就是提供的服务和消费的服务不一样。

pom.xml:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.7.RELEASE
         
    
    com.wangye
    dubbo2
    0.0.1-SNAPSHOT
    dubbo2
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        

        
            org.apache.dubbo
            dubbo
            2.7.0
        


        
        
            org.apache.zookeeper
            zookeeper
            3.4.14
        

        
            org.apache.curator
            curator-framework
            4.1.0
        

        
            org.apache.curator
            curator-recipes
            4.1.0
        
        

        
            com.wangye
            dubbo-api
            1.0-SNAPSHOT
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

application.properties:

server.port=8082

#dubbo配置
# zk注册中心url
registryUrl=zookeeper1.XXXXX.org:16001,zookeeper2.XXXXX.org:16002,zookeeper3.XXXXX.org:16003
#dubbo1提供服务的版本号
dubbo1Version=dubbo1-DEV
#dubbo2提供服务的版本号
dubbo2Version=dubbo2-DEV
#dubbo服务的端口号
dubboPort=20002

service接口实现类:

@Service("byeService")
public class ByeServiceImpl implements ByeService {

    @Override
    public String sayBye(String name) {
        return "Bye " + name;
    }

}

dubbo-config.xml:



    
    
    
 
    
    
    

 	
 	
 	 
 	
 	 

provider.xml:




     

 

consumer.xml:




    

controller:

@RestController
public class HelloController {

    // 这里调用的是dubbo1提供的服务
    @Autowired
    private HelloService helloService;

    @RequestMapping("/sayHello/{name}")
    public String sayBye(@PathVariable String name){
        return helloService.sayHello(name);
    }

}

然后各自启动SpringBoot项目测试controller接口就好了

你可能感兴趣的:(框架技术)