项目结构

image.png
1.分别在mysql主节点和从节点创建db_test,并分别创建表user
搭建MySQL主从复制请参考使用docker搭建MySQL的主从同步
CREATE TABLE `user` (
`user_id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`password` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
1. 创建maven工程,并引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- sharding -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!--阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.14</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
2.创建application.yml并修改数据源配置
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names:
master,slave
# 主数据源
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xxx:3306/db_test?characterEncoding=utf-8
username: root
password: xxx
# 从数据源
slave:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xxx:3307/db_test?characterEncoding=utf-8
username: root
password: xxx
masterslave:
# 读写分离配置
load-balance-algorithm-type: round_robin
# 最终的数据源名称
name: dataSource
# 主库数据源名称
master-data-source-name: master
# 从库数据源名称列表,多个逗号分隔
slave-data-source-names: slave
props:
# 开启SQL显示,默认false
sql:
show: true
3.创建User实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long userId;
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
4.创建UserMapper
@Mapper
public interface UserMapper {
@Select("select * from user where user_id = #{userId}")
User findByUserId(Long userId);
@Insert("insert into user(username,password) values(#{username},#{password})")
void addUser(User user);
}
5.创建UserService
@Service
public class UserService {
@Resource
private UserMapper userMapper;
public User findByUserId(Long userId) {
return userMapper.findByUserId(userId);
}
public void addUser(User user) {
userMapper.addUser(user);
}
}
6.创建UserController
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findByUserId")
@ResponseBody
public User findByUserId(@RequestParam("userId") Long userId) {
return userService.findByUserId(userId);
}
@GetMapping("/addUser")
public String addUser(@RequestParam("username")String username,@RequestParam("password") String password) {
User user = new User(username,password);
userService.addUser(user);
return "success";
}
}
7.创建启动类
@SpringBootApplication
public class ReadWriteSeparationApplication {
public static void main(String[] args) {
SpringApplication.run(ReadWriteSeparationApplication.class,args);
}
}
8.启动服务并验证

并且master数据库和slave数据库也有对应的数据了
-
访问
http://localhost:8080/findByUserId?userId=9
是从从库查询数据
