文章目录
- 实践前知识储备
- Dubbo概述
- 安装zk
- Dubbo在zk中的存储结构
- Dubbo的注册中心有哪些
- Dubbo支持的协议
- Dubbo整合SpringBoot
- 本案例工程结构
- 具体实现
- 开发两个接口进行测试
- 实现步骤
- 测试
实践前知识储备
Dubbo概述
学习Dubbo前你要了解这些
安装zk
Zookeeper概述与安装
Dubbo在zk中的存储结构
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>pom</packaging>
<dependencyManagement>
<dependencies>
<!-- 导入spring-boot-dependencies所管理的maven依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
</dependencies>
</dependencyManagement>
在dubbo-boot 父工程下创建bean工程
maven依赖
<artifactId>dubbo-boot-bean</artifactId>
<dependencies>
<dependency>
<artifactId>dubbo-boot-bean</artifactId>
<groupId>com.etoak.et2001.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
在dubbo-boot 父工程下创建interface工程
maven依赖
<artifactId>dubbo-boot-interface</artifactId>
<dependencies>
<dependency>
<artifactId>dubbo-boot-bean</artifactId>
<groupId>com.etoak.et2001.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
在dubbo-boot 父工程下创建consumer工程
maven依赖
<artifactId>dubbo-boot-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.etoak.et2001.dubbo</groupId>
<artifactId>dubbo-boot-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</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-service</artifactId>
<dependencies>
<!-- interface -->
<dependency>
<artifactId>dubbo-boot-interface</artifactId>
<groupId>com.etoak.et2001.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-boot-starter-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>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</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
<?xml version="1.0" encoding="UTF-8" ?>
<!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