SpringBoot整合dubbo,使用zookeeper作为注册中心

SpringBoot整合dubbo,使用zookeeper作为注册中心


  1. 安装zookeeper,从官网下载并配置zoo.cfg配置文件,然后启动

  2. 创建工程dubboServer用于注册服务

    pom.xml

    
            
                org.springframework.boot
                spring-boot-starter
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
                com.alibaba.boot
                dubbo-spring-boot-starter
                0.1.1
            
        

    application.properties

    # Spring boot application
    spring.application.name = dubbo-provider-demo
    server.port = 9090
    management.port = 9091
    ​
    # Service version
    demo.service.version = 1.0.0
    ​
    # Base packages to scan Dubbo Components (e.g @Service , @Reference)
    dubbo.scan.basePackages  = com.cc
    ​
    # Dubbo Config properties
    ## ApplicationConfig Bean
    dubbo.application.id = dubbo-provider-demo
    dubbo.application.name = dubbo-provider-demo
    ​
    ## ProtocolConfig Bean
    dubbo.protocol.id = dubbo
    dubbo.protocol.name = dubbo
    dubbo.protocol.port = 12345
    ​
    ## RegistryConfig Bean
    dubbo.registry.id = zdubbo
    dubbo.registry.address = zookeeper://127.0.0.1:2181

    创建接口,用于注册

    package com.cc.dubboServer.dubboServer;
    ​
    public interface IDubboServer {
    ​
        String helloWord(String str);
    }

    实现接口

    package com.cc.dubboServer.dubboServer;
    ​
    import com.alibaba.dubbo.config.annotation.Service;
    import org.springframework.stereotype.Component;
    ​
    @Service(version = "${demo.service.version}")
    @Component
    public class DubboServerimpl implements IDubboServer {
        @Override
        public String helloWord(String str) {
            return str + "调用了服务";
        }
    }
    1. 创建dubboClient工程调用服务

      application.properties

      # Spring boot application
      spring.application.name = dubbo-provider-demo
      server.port = 9090
      management.port = 9091
      ​
      # Service version
      demo.service.version = 1.0.0
      ​
      # Base packages to scan Dubbo Components (e.g @Service , @Reference)
      dubbo.scan.basePackages  = com.cc
      ​
      # Dubbo Config properties
      ## ApplicationConfig Bean
      dubbo.application.id = dubbo-consume-demo
      dubbo.application.name = dubbo-consume-demo
      ​
      ​
      ## RegistryConfig Bean
      dubbo.registry.id = zdubbo
      dubbo.registry.address = zookeeper://127.0.0.1:2181

      创建与服务提供方相同的接口

      package com.cc.dubboServer.dubboServer;
      ​
      public interface IDubboServer {
      ​
          String helloWord(String str);
      }

      单元测试,调用

      package com.cc.dubboClient;
      ​
      import com.alibaba.dubbo.config.annotation.Reference;
      import com.cc.dubboServer.dubboServer.IDubboServer;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.test.context.junit4.SpringRunner;
      ​
      @RunWith(SpringRunner.class)
      @SpringBootTest
      public class DubboClientApplicationTests {
      ​
          @Reference(version = "${demo.service.version}")
          private IDubboServer dubboServer;
      ​
          @Test
          public void contextLoads() {
              System.out.println(dubboServer.helloWord("二傻子"));
          }
      ​
      }

       

     

你可能感兴趣的:(springboot)