Spring整合dubbo+zookeeper

1.下载并启动zookeeper

2.创建父工程

 <!-- 父工程标记为pom-->
    <packaging>pom</packaging>

    <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>
    <dependencies>
        <!-- 整合dubbo -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- zookeeper客户端 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.7</version>
        </dependency>
        <!--web工程-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

        <!--mybatis启动器-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
        <!--添加JDBC依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--添加MySql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

3.创建子工程

在这里插入图片描述

4.实体和service交给common管理

因为dubbo要进行服务的注册和调用 注册serviceImpl 调用方如果想使用serviceImpl 要创建一个service
所以将重复创建的service 交个common管理

在这里插入图片描述

5.服务提供者 provider

与以往的springboot 项目一样 但是不同点是serviceImpl 上的注解是dubbo的

在这里插入图片描述

导入依赖 这里父工程导入了连接数据库的依赖 所以子工程不用

  <dependencies>

        <!-- Spring Boot Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!-- 自定义 子模块依赖-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- zookeeper依赖 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>


    </dependencies>

配置文件

server.port=8081

#dubbo 服务名称
spring.dubbo.application.name=provider
# zookeeper 注册中心
spring.dubbo.registry.address=zookeeper://192.168.74.134:2181

#zookeeper 通信端口

spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20881

#配置数据源
spring.datasource.url=jdbc:mysql://localhost:3306/wecart?serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=1234

#配置连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=2
spring.datasource.druid.max-active=20

#配置mybatis
#配置别名
mybatis.type-aliases-package=com.zyf.entity
#配置驼峰  _ 转大写
mybatis.configuration.map-underscore-to-camel-case=true

#自定配置maper 的位置,一般情况下不需要
#mybatis.mapper-locations=classpath:/mapper/*.xml
# com.qfedu.dao 所有的日志打印  开启mybatis 日志
logging.level.com.zyf.dao=debug

#开启二级缓存开关 默认开启
#mybatis.configuration.cache-enabled=true
#初始化pagehelper
#数据库为mysql 在oracle 中和mysql 不一样
pagehelper.helper-dialect=mysql
#当pagesize 为 0 时 查询所有
pagehelper.page-size-zero=true

6.服务的消费者 customer

与一样的springboot 项目一样 不同之处在于 service 的调用 注解不同

在这里插入图片描述

1.导入依赖

 <dependencies>
        <!-- Spring Boot Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>


        <!-- zookeeper依赖 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--        分页-->
        <!--mapper-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.1.4</version>
        </dependency>
        <!--pagehelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.9</version>
        </dependency>
    </dependencies>

2.配置文件

server.port=8090


#dubbo 服务名称
spring.dubbo.application.name=customer
# zookeeper 注册中心
spring.dubbo.registry.address=zookeeper://192.168.74.134:2181

#dubbo 通信端口
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20881

#配置数据源
spring.datasource.url=jdbc:mysql://localhost:3306/wecart?serverTimezone=GMT%2b8&useSSL=false
spring.datasource.username=root
spring.datasource.password=1234
#配置连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=2
spring.datasource.druid.max-active=20

#配置mybatis
#配置别名
mybatis.type-aliases-package=com.zyf.entity
#配置驼峰  _ 转大写
mybatis.configuration.map-underscore-to-camel-case=true

#自定配置maper 的位置,一般情况下不需要
#mybatis.mapper-locations=classpath:/mapper/*.xml
# com.qfedu.dao 所有的日志打印  开启mybatis 日志
logging.level.com.zyf.dao=debug

#开启二级缓存开关 默认开启
#mybatis.configuration.cache-enabled=true
#初始化pagehelper
#数据库为mysql 在oracle 中和mysql 不一样
pagehelper.helper-dialect=mysql
#当pagesize 为 0 时 查询所有
pagehelper.page-size-zero=true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zyf_fly66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值