SpringBoot整合Dubbo+Zookeeper

文章目录

    • 实践前知识储备
      • Dubbo概述
      • 安装zk
      • Dubbo在zk中的存储结构
      • Dubbo的注册中心有哪些
      • Dubbo支持的协议
    • Dubbo整合SpringBoot
      • 本案例工程结构
      • 具体实现
      • 开发两个接口进行测试
      • 实现步骤
      • 测试

实践前知识储备

Dubbo概述

学习Dubbo前你要了解这些

安装zk

Zookeeper概述与安装

Dubbo在zk中的存储结构

SpringBoot整合Dubbo+Zookeeper_第1张图片

Dubbo的注册中心有哪些

1、Multicast:Multicast 注册中心不需要启动任何中心节点,只要广播地址一样,就可以互相发现。

2、Zookeeper是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用;

3、Nacos 是 Dubbo 生态系统中重要的注册中心实现,其中 dubbo-registry-nacos 则是 Dubbo 融合 Nacos 注册中心的实现。

4、基于 Redis 实现的注册中心 。

5、Simple注册中心本身就是一个普通的 Dubbo 服务,可以减少第三方依赖,使整体通讯方式一致。

Dubbo支持的协议

dubbo、rmi、hessian、http、webservice、rest、thrift、memcached、redis、grpc

Dubbo官方推荐使用 dubbo协议

Dubbo整合SpringBoot

本案例工程结构

dubbo-boot pom父工程
dubbo-boot-bean java bean
dubbo-boot-interface 接口服务
dubbo-boot-user-service 服务提供者
dubbo-boot-consumer 服务消费者、

具体实现

创建dubbo-boot 父工程

引入maven依赖

 <packaging>pompackaging>

    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.2.6.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>

            
            <dependency>
                <groupId>com.alibaba.bootgroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
                <version>0.2.0version>
            dependency>

            
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>2.1.2version>
            dependency>

            
            <dependency>
                <groupId>com.github.pagehelpergroupId>
                <artifactId>pagehelper-spring-boot-starterartifactId>
                <version>1.2.10version>
            dependency>
        dependencies>
    dependencyManagement>

在dubbo-boot 父工程下创建bean工程

maven依赖

 <artifactId>dubbo-boot-beanartifactId>

  <dependencies>
        <dependency>
            <artifactId>dubbo-boot-beanartifactId>
            <groupId>com.etoak.et2001.dubbogroupId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>

在dubbo-boot 父工程下创建interface工程

maven依赖

    <artifactId>dubbo-boot-interfaceartifactId>

    <dependencies>
        <dependency>
            <artifactId>dubbo-boot-beanartifactId>
            <groupId>com.etoak.et2001.dubbogroupId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>

在dubbo-boot 父工程下创建consumer工程

maven依赖

   <artifactId>dubbo-boot-consumerartifactId>

    <dependencies>
        <dependency>
            <groupId>com.etoak.et2001.dubbogroupId>
            <artifactId>dubbo-boot-interfaceartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>com.alibaba.bootgroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
    dependencies>

消费者注册到zookeeper

application.yml文件

server:
  port: 8080

dubbo:
  application:
    name: user-service-consumer
  registry:
    address: zookeeper://192.168.149.128:2181

启动类

@SpringBootApplication
@EnableDubbo
public class ConsumerApp {
    public static void main(String[] args) {
         SpringApplication.run(ConsumerApp.class, args);
    }
}

在dubbo-boot 父工程下创建user-service工程

maven依赖

配置数据源整合mybatis

  <artifactId>dubbo-boot-user-serviceartifactId>
    <dependencies>
        
        <dependency>
            <artifactId>dubbo-boot-interfaceartifactId>
            <groupId>com.etoak.et2001.dubbogroupId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>

        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
        dependency>

        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelper-spring-boot-starterartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.bootgroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

    dependencies>

服务提供方user-service注册到zookeeper 并声明使用Dubbo协议端口为20080

application.yml文件

server:
  port: 9090

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/user?serverTimezone=GMT
    username: root
    password: etoak

mybatis:
  type-aliases-package: com.etoak.bean
  mapper-locations: classpath:mappers/*.xml

pagehelper:
  reasonable: true

# Dubbo配置
dubbo:
  application:
    name: user-service
  registry:
    address: zookeeper://192.168.149.128:2181
  protocol:
    name: dubbo
    port: 20880

启动类

@SpringBootApplication
@MapperScan(basePackages = "com.etoak.mapper")
@EnableDubbo
public class UserServiceApp {
     public static void main(String[] args) {
         SpringApplication.run(UserServiceApp.class, args);
     }
}

开发两个接口进行测试

根据id查询用户

查询用户列表

实现步骤

在dubbo-boot-bean中开发User.java

@Data
public class User implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
    private Integer status;
    private String createTime;
}

在dubbo-boot-bean中开发PageVo.java 分页查询时使用

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageVo<T> implements Serializable {

    private int pageNum;
    private int pageSize;
    private List<T> rows;
    private long total;
    private int pageCount;

}

在dubbo-boot-interface中开发UserService接口

/**
 * 服务接口
 */
public interface UserService {

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    User getById(int id);

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    PageVo<User> queryList(int pageNum, int pageSize);

}

在dubbo-boot-user-service中开发UserMapper接口

public interface UserMapper {

    User getById(int id);

    List<User> queryList();

}

在dubbo-boot-user-service中开发UserMapper.xml


DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.etoak.mapper.UserMapper">

    <sql id="base_sql">
        id, name, age, status, create_time AS createTime
    sql>

    <select id="getById" parameterType="int" resultType="user">
        SELECT
            <include refid="base_sql">include>
            FROM user
           WHERE id = #{value}
    select>

    <select id="queryList" resultType="user">
        SELECT
            <include refid="base_sql">include>
            FROM user
    select>


mapper>

在dubbo-boot-user-servcie中开发UserService接口

//注意这里的@Service注解为Dubbo包下的
import com.alibaba.dubbo.config.annotation.Service;
@Service // 发布Dubbo服务
@Slf4j     // 实现   dubbo-boot-interface工程下的 UserService 
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public User getById(int id) {
        log.info("getById param id - {}", id);
        return userMapper.getById(id);
    }

    @Override
    public PageVo<User> queryList(int pageNum, int pageSize) {
        log.info("queryList param paegNum - {}, pageSize - {}",
                pageNum, pageSize);
        // 设置分页条件
        PageHelper.startPage(pageNum, pageSize);
        // 查询用户列表
        List<User> userList = userMapper.queryList();
        // 创建PageInfo
        PageInfo<User> pageInfo = new PageInfo(userList);
        // 返回结果
        return new PageVo<User>(pageInfo.getPageNum(),
                pageInfo.getPageSize(),
                userList,
                pageInfo.getTotal(),
                pageInfo.getPages());
    }
}

在dubbo-boot-consumer开发两个接口

也可以单独建个Controller 我这里直接写在启动类了

@SpringBootApplication
@EnableDubbo
@RestController
@RequestMapping("/user")
public class ConsumerApp {

    public static void main(String[] args) {
         SpringApplication.run(ConsumerApp.class, args);
    }

    // 订阅远程服务
    @Reference(timeout = 3000)
    UserService userService;

    @GetMapping("/{id}")
    public User getUser(@PathVariable int id) {
        return userService.getById(id);
    }

    @GetMapping("/list")
    public PageVo<User> queryList(
    @RequestParam(required = false, defaultValue = "1") int pageNum,
    @RequestParam(required = false, defaultValue = "10") int pageSize) {
        return userService.queryList(pageNum, pageSize);
    }

}

测试

  • 先启动zookeeper,dubbo控制台

  • 再启动服务提供者(dubbo-user-service)

  • 再启动服务消费者(dubbo-mvc-consumer)

  • 最后调用

    http://localhost:8080/user/{id}

    http://localhost:8080/user/list

你可能感兴趣的:(Dubbo+,Zookeeper,spring,boot,dubbo,后端)