springboot-zookeeper-dubbo 整合

springboot-zookeeper-dubbo

  1. 建议maven项目,pom.xml加上相应依赖

    	<parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.3.3.RELEASEversion>
            <relativePath/> 
        parent>
    	<properties>
            <java.version>1.8java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                <scope>compilescope>
            dependency>
            
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
                <version>2.7.8version>
            dependency>
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-recipesartifactId>
                <version>2.13.0version>
            dependency>
            <dependency>
                <groupId>org.apache.zookeepergroupId>
                <artifactId>zookeeperartifactId>
                <version>3.4.13version>
                <type>pomtype>
            dependency>
        dependencies>
    
  2. 建立一个Module 专门存放Consumer和provider 公用元素,可取名xxx-base模板,存放共同的接口和工具类等

  3. 建立Consumer对应的Module和Provider对应的Module,两者都引用base Module

    	<dependencies>
            <dependency>
                <groupId>com.baoyonggroupId>
                <artifactId>dubbo-baseartifactId>
                <version>${project.parent.version}version>
            dependency>
        dependencies>
    
  4. 安装zookeeper,下载个压缩包本地解压修改一下conf/zoo.cfg即可,启动zk;

    1. 消费者 application配置:

      #Dubbo configuration
      dubbo.application.name=dubbo_consumer
      dubbo.registry.protocol=zookeeper
      dubbo.registry.address=zookeeper://127.0.0.1:2181
      # 注解中引用此版本,之后消费者可通过配置该参数实现动态切换引用的生产者
      dubbo.provider.version=2.0
      
    2. 生产者application配置:

      ## dubbo 配置
      dubbo.application.name=dubbo_provider
      dubbo.registry.protocol=zookeeper
      dubbo.registry.address=zookeeper://127.0.0.1:2181
      dubbo.protocol.name=dubbo
      dubbo.protocol.port=20880
      # 扫描注解包通过该设置将服务注册到zookeeper
      dubbo.scan.base-packages=com.baoyong.provider.service.impl
      
  5. 接口实现及调用:

    1. 在base的项目中定义接口 如 TestService。

    2. 在provider项目中实现base定义的接口,TestServiceImpl,注意引用注解:

      @DubboService(interfaceClass = TestService.class, interfaceName = "testServiceImplProvider", version = "${dubbo.provider.version}")
      public class TestServiceImpl implements TestService {
               
      	...
      }
      

      此处的实现类在bean容器中的id为 testServiceImpl

    3. 在consumer项目中引用provider的服务:

      @DubboReference(version = "${dubbo.provider.version}")
      private com.baoyong.base.service.TestService testService;
      
  6. 可配置不同version的provider,然后修改consumer调用provider的version即可切换不同版本调用。

你可能感兴趣的:(spring家族,spring,boot,zookeeper,dubbo)