SpringBoot + Dubbo + Zookeeper +Mybatis-Plus + Mysql 搭建简单示例工程
1. Dubbo简介
Apache Dubbo 是一款微服务开发框架,它提供了 RPC通信 与 微服务治理 两大关键能力。这意味着,使用 Dubbo 开发的微服务,将具备相互之间的远程发现与通信能力, 同时利用 Dubbo 提供的丰富服务治理能力,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。同时 Dubbo 是高度可扩展的,用户几乎可以在任意功能点去定制自己的实现,以改变框架的默认行为来满足自己的业务需求。
官网:https://dubbo.apache.org/
2. 相关博客
3. 初始化数据库
# 创建数据库:dubbo-demo
# 初始化表结构,即基础数据
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`user_code` VARCHAR ( 64 ) NOT NULL COMMENT '用户编码',
`user_name` VARCHAR ( 50 ) NULL COMMENT '用户名称',
`status` CHAR ( 1 ) NOT NULL DEFAULT '0' COMMENT '用户状态(0-可用,1-删除,2-禁用)',
PRIMARY KEY ( `user_code` )
) COMMENT '用户表';
INSERT INTO `t_user` VALUES ('1', '张三', '0');
INSERT INTO `t_user` VALUES ('2', '李四', '1');
INSERT INTO `t_user` VALUES ('3', '王五', '2');
4. 示例代码
4.1 创建接口工程
接口工程主要存放实体类和定义接口。
- 创建工程
- 修改pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.c3stones</groupId>
<artifactId>spring-boot-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-dubbo-api</name>
<description>Spring Boot Dubbo Api</description>
<properties>
<java.version>1.8</java.version>
<mybatis-plus.version>3.3.1</mybatis-plus.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
</dependencies>
</project>
- 创建实体类
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
/**
* 用户Entity
*
* @author CL
*/
@TableName(value = "t_user")
public class User extends Model<User> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户编码
*/
@TableId(type = IdType.INPUT)
private String userCode;
/**
* 用户名称
*/
private String userName;
/**
* 用户状态(0-可用,1-删除,2-禁用)
*/
private String status;
public User() {
}
public User(String userCode, String userName) {
this.userCode = userCode;
this.userName = userName;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
- 创建Service
import com.baomidou.mybatisplus.extension.service.IService;
import com.c3stones.entity.User;
/**
* 用户Service
*
* @author CL
*/
public interface UserService extends IService<User> {
}
- 创建状态枚举(公共)
/**
* 状态枚举
*
* @author CL
*
*/
public enum Status {
/**
* 正常状态
*/
NORMAL("0", "正常"),
/**
* 删除状态
*/
DELETE("1", "删除"),
/**
* 禁用状态
*/
DISABLE("2", "禁用");
/**
* 状态值
*/
private String value;
/**
* 状态标签
*/
private String label;
private Status(String value, String label) {
this.value = value;
this.label = label;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
/**
* 翻译状态
*
* @param value 状态值
* @return
*/
public static String transf(String value) {
for (Status s : Status.values()) {
if (value.equals(s.getValue())) {
return s.label;
}
}
return null;
}
}
4.2 创建生产者服务
生产者服务主要存放Mapper和Mapper.xml,并实现Service。
- 创建工程
- 修改pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.c3stones</groupId>
<artifactId>spring-boot-dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-dubbo-provider</name>
<description>Spring Boot Dubbo Provider</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath />
</parent>
<properties>
<api.version>0.0.1-SNAPSHOT</api.version>
<dubbo.version>2.0.0</dubbo.version>
<zk.version>0.11</zk.version>
</properties>
<dependencies>
<!-- API接口 -->
<dependency>
<groupId>com.c3stones</groupId>
<artifactId>spring-boot-dubbo-api</artifactId>
<version>${api.version}</version>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- Zookeeper -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zk.version}</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 创建配置文件
在resources目录下,创建application.yml。
server:
port: 7001
dubbo:
application:
name: dubbo-provider
registry:
protocol: zookeeper
address: 127.0.0.1:2181
monitor:
protocol: register
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dubbo-demo?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
username: root
password: 123456
# Mybatis-plus配置
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
global-config:
db-config:
id-type: AUTO
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- 创建Mapper
import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.c3stones.entity.User;
/**
* 用户Mapper
*
* @author CL
*
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
- 创建Service实现
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c3stones.entity.User;
import com.c3stones.mapper.UserMapper;
import com.c3stones.service.UserService;
/**
* 用户Service实现
*
* @author CL
*/
@Service
@Component
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
- 创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
/**
* 生产者启动类
*
* @author CL
*/
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
4.3 创建消费者服务
消费者服务主要调用生产者服务,并暴露接口。
- 创建工程
- 修改pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.c3stones</groupId>
<artifactId>spring-boot-dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-dubbo-consumer</name>
<description>Spring Boot Dubbo Consumer</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath />
</parent>
<properties>
<api.version>0.0.1-SNAPSHOT</api.version>
<dubbo.version>2.0.0</dubbo.version>
<zk.version>0.11</zk.version>
</properties>
<dependencies>
<!-- API接口 -->
<dependency>
<groupId>com.c3stones</groupId>
<artifactId>spring-boot-dubbo-api</artifactId>
<version>${api.version}</version>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- Zookeeper -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zk.version}</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 创建配置文件
在resources目录下,创建application.yml。
server:
port: 7002
dubbo:
application:
name: dubbo-consumer
registry:
protocol: zookeeper
address: 127.0.0.1:2181
monitor:
protocol: register
- 创建Controller
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.c3stones.entity.User;
import com.c3stones.enums.Status;
import com.c3stones.service.UserService;
/**
* 用户Controller
*
* @author CL
*/
@RestController
@RequestMapping(value = "user")
public class UserController {
@Reference
private UserService userService;
/**
* 查询用户信息
*
* @param userCode 用户编码
* @return 用户信息
*/
@RequestMapping(value = "get")
public User get(String userCode) {
User user = userService.getById(userCode);
if (user != null) {
user.setStatus(Status.transf(user.getStatus()));
}
return user;
}
}
- 创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
/**
* 消费者启动类
*
* @author CL
*/
@EnableDubbo
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
- 启动zookeeper
- 启动生产者
- 启动消费者
5. 测试
# 请求1:查询用户编码为1的用户信息
curl http://127.0.0.1:7002/user/get?userCode=1
# 响应1:
{"userCode":"1","userName":"张三","status":"正常"}
# 请求2:查询用户编码为1的用户信息
curl http://127.0.0.1:7002/user/get?userCode=2
# 响应2:
{"userCode":"2","userName":"李四","status":"删除"}
# 请求3:查询用户编码为1的用户信息
curl http://127.0.0.1:7002/user/get?userCode=3
# 响应3:
{"userCode":"3","userName":"王五","status":"禁用"}
6. 项目地址
7. 扩展
Dubbo管理控制台搭建请浏览:Dubbo-Admin:Dubbo控制台搭建