文章目录
- 1 前言
- 2、创建SpringBoot程序
- 2.1、创建项目
- 2.2、添加依赖
- 2.3、生成实体类、service与Mapper
- 1.5、配置读写分离
- 2、测试
- 2.1、读写分离测试
- 2.2、事务测试
- 2.3、负载均衡测试
- 结语
1 前言
shardingshpere-jdbc定位为轻量级 Java 框架,在 Java 的 JDBC 层提供的额外服务
。 它使用客户端直连数据库,以 jar 包形式提供服务
,无需额外部署和依赖,可理解为增强版的 JDBC 驱动,完全兼容 JDBC 和各种 ORM 框架。
关于mysql配置主从复制,可以参考之前写的文章=0101docker mysql8镜像主从复制-运维-mysql=
2、创建SpringBoot程序
2.1、创建项目
项目类型:Spring Initializr
SpringBoot脚手架:http://start.aliyun.com
项目名:sharding-jdbc-demo
SpringBoot版本:2.7.10
2.2、添加依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.gaogzhen</groupId><artifactId>sharging-jdbc-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>sharging-jdbc-demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.10</version><relativePath /></parent><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.7.10</spring-boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId><version>5.1.1</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.1</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><configuration><mainClass>com.gaogzhen.shardingjdbcdemo.ShardingJdbcDemoApplication</mainClass></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
2.3、生成实体类、service与Mapper
使用idea插件mybatisX,根据数据库连接表生成实体类、service、mapper
第一步:如下图2.3-1所示
第二步:如下图2.3-2所示:
- 实体类
package com.gaogzhen.shardingjdbcdemo.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import lombok.Data;/*** 用户* @author gaogzhen* @TableName t_user*/
@TableName(value ="t_user")
@Data
public class User implements Serializable {@TableField(exist = false)private static final long serialVersionUID = 1L;@TableId(type = IdType.AUTO)private Long id;/*** 用户名*/private String name;}
- service
package com.gaogzhen.shardingjdbcdemo.service;import com.gaogzhen.shardingjdbcdemo.entity.User;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author gaogzhen
* @description 针对表【t_user】的数据库操作Service
* @createDate 2023-08-13 15:37:14
*/
public interface UserService extends IService<User> {}
- serviceImpl
package com.gaogzhen.shardingjdbcdemo.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaogzhen.shardingjdbcdemo.entity.User;
import com.gaogzhen.shardingjdbcdemo.service.UserService;
import com.gaogzhen.shardingjdbcdemo.mapper.UserMapper;
import org.springframework.stereotype.Service;/**
* @author gaogzhen
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2023-08-13 15:37:14
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService{}
- mapper
package com.gaogzhen.shardingjdbcdemo.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gaogzhen.shardingjdbcdemo.entity.User;
import org.apache.ibatis.annotations.Mapper;/**
* @author gaogzhen
* @description 针对表【t_user】的数据库操作Mapper
* @createDate 2023-08-13 15:37:14
* @Entity com.gaogzhen.shardingjdbcdemo.entity.User
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {}
1.5、配置读写分离
application.properties:
# 应用名称
spring.application.name=sharging-jdbc-demo
# 开发环境设置
spring.profiles.active=dev
# 内存模式
spring.shardingsphere.mode.type=Memory# 配置真实数据源
spring.shardingsphere.datasource.names=master,slave1,slave2# 配置第 1 个数据源
spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://127.0.0.1:3340/db_user?allowPublicKeyRetrieval=true&useSSL=false
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456# 配置第 2 个数据源
spring.shardingsphere.datasource.slave1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave1.jdbc-url=jdbc:mysql://127.0.0.1:3341/db_user?allowPublicKeyRetrieval=true&useSSL=false
spring.shardingsphere.datasource.slave1.username=root
spring.shardingsphere.datasource.slave1.password=123456# 配置第 3 个数据源
spring.shardingsphere.datasource.slave2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave2.jdbc-url=jdbc:mysql://127.0.0.1:3342/db_user?allowPublicKeyRetrieval=true&useSSL=false
spring.shardingsphere.datasource.slave2.username=root
spring.shardingsphere.datasource.slave2.password=123456# 读写分离类型,如: Static,Dynamic
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.type=Static
# 写数据源名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.write-data-source-name=master
# 读数据源名称,多个从数据源用逗号分隔
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.read-data-source-names=slave1,slave2# 负载均衡算法名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.load-balancer-name=alg_round# 负载均衡算法配置
# 负载均衡算法类型
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_round.type=ROUND_ROBIN
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_random.type=RANDOM
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.type=WEIGHT
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave1=1
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave2=2# 打印SQl
spring.shardingsphere.props.sql-show=true# mybatis plus 配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.gaogzhen.shardingjdbcdemo.entity
- myds为自定义的逻辑数据源名称
- 当前生效的负载均衡算法为轮询,可以通过更换负载均衡算法名称,来测试随机和权重。
2、测试
2.1、读写分离测试
package com.gaogzhen.shardingjdbcdemo;import com.gaogzhen.shardingjdbcdemo.entity.User;
import com.gaogzhen.shardingjdbcdemo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;
import java.util.List;@SpringBootTest
public class ReadWriteTest {@Resourceprivate UserMapper userMapper;/*** 读写分离:写入数据的测试*/@Testpublic void testInsert(){User user = new User();user.setName("张三");userMapper.insert(user);User user1 = userMapper.selectById(1L);}}
2.2、事务测试
为了保证主从库间的事务一致性,避免跨服务的分布式事务,ShardingSphere-JDBC的主从模型中,事务中的数据读写均用主库
。
- 不添加@Transactional:insert对主库操作,select对从库操作
- 添加@Transactional:则insert和select均对主库操作
- **注意:**在JUnit环境下的@Transactional注解,默认情况下就会对事务进行回滚(即使在没加注解@Rollback,也会对事务回滚)
/*** 事务测试*/@Transactional@Testpublic void testTrans() {User user = new User();user.setName("xxx001");userMapper.insert(user);List<User> users = userMapper.selectList(null);}
2.3、负载均衡测试
/*** 负载均衡测试*/
@Test
public void testLoadBalance() {List<User> users1 = userMapper.selectList(null);List<User> users2 = userMapper.selectList(null);}
也可以在web请求中测试负载均衡
package com.gaogzhen.shardingjdbcdemo.controller;import com.gaogzhen.shardingjdbcdemo.entity.User;
import com.gaogzhen.shardingjdbcdemo.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.List;@RestController
@RequestMapping("/user")
public class UserController {@Resourceprivate UserService userService;/*** 测试负载均衡策略*/@GetMapping("selectAll")public void selectAll(){List<User> users = userService.list();users.forEach(System.out::println);}
}
命令行测试结果如下图所示:
结语
如果小伙伴什么问题或者指教,欢迎交流。
QQ:806797785
仓库源代码地址:https://github.com/gaogzhen/shardingsphere-jdbc-demo.git
参考链接:
[1]ShardingSphere5实战教程[CP/OL].2022-09-14.p18-23.
[2]mysql官网使用配置文件文档[CP/OL].