zoukankan      html  css  js  c++  java
  • SpringBoot+dubbo+zookeeper(windows )

     我把原来自己写的一个单体应用(论坛)小小地拆了一下。原来是整个项目打成war包仍到tomcat的webapps目录下访问,也就是基础公共类、接口类、controller类、dao类都属于同级目录。现在拆成四部分:

    • 1.common-core基础公共类:包括工具类、实体类(用于dao层与controller层数据模型转换)
    • 2.api 接口类:主要是对外部服务提供接口,因为要把单体应用改造为使用dubbo进行服务的远程调用。只有api接口,也可以存放一些工具类
    • 3.svsImpl:接口实现类,包括api的具体实现以及对数据库层访问等操作,编写serviceImpl、以及dao层的地方
    • 4.controller:web级应用也就是我们编写controller层的地方

     

    zookeeper

    首先zookeeper依赖Java环境,需要先配下Java环境变量,网上有很多。

    因为是我本地对项目改造,所以先在windows上搭下环境,先搞zk。

     http://www.apache.org/dyn/closer.cgi/zookeeper/

    访问zookeeper官网,下载  

     

     

     

     三个随便一个都行。进去后下载这个带bin的压缩文件。我是挺早下的zookeeper,是3.5.8版本的

     

     

     解压:

     

     

     进入解压文件目录  /apache-zookeeper-3.5.8-binapache-zookeeper-3.5.8-binconf

    找到文件zoo_sample.cfg 复制重命名一份叫zoo.cfg.然后修改下zoo.cfg文件的内容 我改成下面这些:

    # The number of milliseconds of each tick
    tickTime=2000
    # The number of ticks that the initial 
    # synchronization phase can take
    initLimit=10
    # The number of ticks that can pass between 
    # sending a request and getting an acknowledgement
    syncLimit=5
    # the directory where the snapshot is stored.
    # do not use /tmp for storage, /tmp here is just 
    # example sakes.
    dataDir=/tmp/zookeeper
    # the port at which the clients will connect
    clientPort=2181
    # the maximum number of client connections.
    # increase this if you need to handle more clients
    #maxClientCnxns=60
    #
    # Be sure to read the maintenance section of the 
    # administrator guide before turning on autopurge.
    #
    # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
    #
    # The number of snapshots to retain in dataDir
    #autopurge.snapRetainCount=3
    # Purge task interval in hours
    # Set to "0" to disable auto purge feature
    #autopurge.purgeInterval=1

    上面是zoo.cfg的文件内容,红色的代表可修改,如果没有哪一项,要手动加上。

    启动zookeeper,bin目录下,因为我是在windwos操作的,找到zkServer.cmd 执行即可:

     

     

     ps:如果执行zkServer.cmd  命令脚本失败可以修改下zkServer.cmd文件内容,加上这两行在末尾:保存再执行可以看的报错...

    pause
    endlocal

    如果是这样的,代表启动好了。

     

     

     

    添加dubbo的pom依赖:

    父级pom中定义dubbo版本、spring boot版本 等:

     <dubbo-version>2.7.7</dubbo-version>
    <spring-boot.version>2.3.0.RELEASE</spring-boot.version>

    关于dubbo的依赖(spring等其他的就不贴了):这里使用zookeeper作注册中心所以需要依赖  dubbo-dependencies-zookeeper  ps说一句废话:看坐标可以看出这个是apache提供的

     

     <!-- dubbo-springboor -->
                <dependency>
                    <groupId>org.apache.dubbo</groupId>
                    <artifactId>dubbo-spring-boot-starter</artifactId>
                    <version>${dubbo-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.dubbo</groupId>
                    <artifactId>dubbo-dependencies-zookeeper</artifactId>
                    <version>${dubbo-version}</version>
                    <type>pom</type>
                    <exclusions>
                        <exclusion>
                            <groupId>org.slf4j</groupId>
                            <artifactId>slf4j-log4j12</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <!-- bouncycastle-->

    上面关于dubbo的依赖需要在 controller和svsImpl两个工程中引入。

    有关的配置:配置文件我用application.yml

    controller工程的application.yml配置 增加:

    dubbo:
      application:
        name: hehe
      scan:
        base-packages: com.dabai.mytwo.controller
      protocol:
        port: 20881
        name: dubbo
      registry:
        #zookeeper注册中心地址
        address: zookeeper://localhost:2181

    svsImpl工程的application.yml配置 增加:

    #dubbo配置
    dubbo:
      application:
        name: hehe
      scan:
        base-packages: com.dabai.serviceImpl
      protocol:
        port: 20881
        name: dubbo
      registry:
        #zookeeper注册中心地址
        address: zookeeper://localhost:2181

    使用:

    svsImpl作为服务的提供者,需要明确接口的具体实现,在接口的实现类上加上注解 :@DubboService 如

    /**
     * @author dabai:
     * <p>
     * 类说明  commentService实现类
     */
    @DubboService
    @Component
    public class CommentServiceImpl implements CommentService {}

    controller作为服务的消费者,使用dubbo提供的注解,在controller类上加上注解:@DubboReference 如:

    @Controller
    public class CommentController {
    
        @DubboReference
        private CommentService commentService;
    }

    参考文档:

    https://dubbo.apache.org/zh/docs/

    ps:@DubboService,@DubboReference 两注解还包含其它很多属性可以设置,如口令等

     

  • 相关阅读:
    MRF能量优化
    Django:model中的ForeignKey理解
    Django:常见的orm操作
    Django:在模板中获取当前url信息
    Django:haystack全文检索详细教程
    Django:全文检索功能可参考博客
    看电影学英语
    Markdown中怎么上传图片
    Mosquitto的安装、配置、测试
    Django:评论文章后局部刷新评论区
  • 原文地址:https://www.cnblogs.com/notably/p/14361393.html
Copyright © 2011-2022 走看看