Dubbo+Zookeeper 入门Demo

1.Zookeeper安装及启动

  可参考这篇文章https://www.cnblogs.com/geekdc/p/5948326.html

  从下载到启动都描述的很详细,按照文章一步一步走即可。

2.Dubbo-admin搭建及启动

  dubbo-admin的配置及启动推荐一篇文章https://blog.csdn.net/qq_28988969/article/details/79866111

  1. Github(https://github.com/apache/incubator-dubbo/tree/dubbo-2.6.0)) 下载发现没有dubbo-admin

  2. 在官网上下载dubbo-admin 2.6.0的Source Code(Zip) https://github.com/apache/incubator-dubbo/releases

    dubbo-admin的部署比较简单,可以打包放到Tomcat,也可以直接跑项目,我是直接导入Eclipse,步骤如下:

    2.1 修改WEB-INFO 下dubbo.properties文件

    dubbo.registry.address=zookeeper://127.0.0.1:2181
    dubbo.admin.root.password=root
    dubbo.admin.guest.password=guest

    2.2 在Tomcat中启动即可,查看控制中心,浏览器输入http://localhost:8888/dubbo-admin

3.创建provider注册dubbo服务

  • pom依赖

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>io.dubbo.springboot</groupId>
                <artifactId>spring-boot-starter-dubbo</artifactId>
                <version>1.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <scope>provided</scope>
            </dependency>
            <!--不加会抛异常,暂时不知道原理  -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
     
  • application.properties配置文件

    ## Dubbo 服务提供者配置
    spring.dubbo.application.name=provider
    spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
    spring.dubbo.protocol.name=dubbo
    spring.dubbo.protocol.port=20880
    spring.dubbo.scan=com.wiaoong.provider

     

  • 注册dubbo 服务

    //接口类
    public interface ICompanyDubboService {
        Company evaluateCompany(String company,String evaluation);
    }
    
    //实现类
    import com.alibaba.dubbo.config.annotation.Service;
    import com.wiaoong.provider.dubbo.ICompanyDubboService;
    import com.wiaoong.provider.entity.Company;
    
    /**
     * 注册dubbo服务
     * @author wiaoong
     * @date   2019.02.21 15:18
     */
    @Service
    public class CompanyDubboServiceImpl implements ICompanyDubboService {
        @Override
        public Company evaluateCompany(String company, String evaluation) {
            return new Company(company, evaluation);
        }
    }

     

  • provider项目整体目录,直接启动项目会在buddo-admin看到已注册的服务

     

 

4.创建dubbo-comon,封装provider中注册的服务接口

    • provider项目整体目录

5.创建consumer引用dubbo服务

    • pom依赖同provider项目,另需引入步骤4生成的包dubbo-common.jar

    • application.properties配置

      ## 避免和 server 工程端口冲突
      server.port=8081
      
      ## Dubbo 服务消费者配置
      spring.dubbo.application.name=consumer
      spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
      spring.dubbo.scan=com.wiaoong.consumer
    • 调用dubbo服务

      package com.wiaoong.consumer.dubbo;
      
      import org.springframework.scheduling.annotation.Scheduled;
      import org.springframework.stereotype.Service;
      
      import com.alibaba.dubbo.config.annotation.Reference;
      import com.wiaoong.provider.dubbo.ICompanyDubboService;
      
      /**
       * 调取dubbo服务
       * @author wiaoong
       * @date   2019.02.21 15:59
       */
      @Service
      public class CompanyDubboConsumerService {
      
          @Reference
          ICompanyDubboService companyDubboService;
          
          @Scheduled(fixedRate=5000)
          public void printStr() {
              System.err.println(companyDubboService.evaluateCompany("新博卓畅", "是一个骗子公司...是的..."));
          }
      }
    • consumer目录结构,直接启动项目,会在控制台打出所调用服务输出的结果

最后附上源码:https://gitee.com/wiaoong/studynotes.git

posted @ 2019-02-21 17:52  往霄龙  阅读(687)  评论(0编辑  收藏  举报
TOP