0101读写分离测试-jdbc-shardingsphere-中间件

文章目录

    • 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 框架。

0101读写分离测试-jdbc-shardingsphere-中间件_第1张图片

关于mysql配置主从复制,可以参考之前写的文章=0101docker mysql8镜像主从复制-运维-mysql=

2、创建SpringBoot程序

2.1、创建项目

项目类型:Spring Initializr

SpringBoot脚手架:http://start.aliyun.com

项目名:sharding-jdbc-demo

SpringBoot版本:2.7.10

2.2、添加依赖


<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.0modelVersion>
    <groupId>com.gaogzhengroupId>
    <artifactId>sharging-jdbc-demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>sharging-jdbc-demoname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.10version>
        <relativePath />
    parent>

    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <spring-boot.version>2.7.10spring-boot.version>
    properties>

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

        <dependency>
            <groupId>org.apache.shardingspheregroupId>
            <artifactId>shardingsphere-jdbc-core-spring-boot-starterartifactId>
            <version>5.1.1version>
        dependency>

        <dependency>
            <groupId>com.mysqlgroupId>
            <artifactId>mysql-connector-jartifactId>
        dependency>

        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.3.1version>
        dependency>

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

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.8.1version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>${spring-boot.version}version>
                <configuration>
                    <mainClass>com.gaogzhen.shardingjdbcdemo.ShardingJdbcDemoApplicationmainClass>
                configuration>
                <executions>
                    <execution>
                        <id>repackageid>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>

project>

2.3、生成实体类、service与Mapper

使用idea插件mybatisX,根据数据库连接表生成实体类、service、mapper

第一步:如下图2.3-1所示

0101读写分离测试-jdbc-shardingsphere-中间件_第2张图片

第二步:如下图2.3-2所示:

0101读写分离测试-jdbc-shardingsphere-中间件_第3张图片

  • 实体类
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 {

    @Resource
    private UserMapper userMapper;


     /**
      * 读写分离:写入数据的测试
      */
     @Test
     public 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
 @Test
 public 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 {

    @Resource
    private UserService userService;

    /**
     * 测试负载均衡策略
     */
    @GetMapping("selectAll")
    public void selectAll(){
        List<User> users = userService.list();
        users.forEach(System.out::println);
    }
}

命令行测试结果如下图所示:

0101读写分离测试-jdbc-shardingsphere-中间件_第4张图片

结语

如果小伙伴什么问题或者指教,欢迎交流。

QQ:806797785

仓库源代码地址:https://github.com/gaogzhen/shardingsphere-jdbc-demo.git

参考链接:

[1]ShardingSphere5实战教程[CP/OL].2022-09-14.p18-23.

[2]mysql官网使用配置文件文档[CP/OL].

你可能感兴趣的:(中间件,中间件,shardingsphere,读写分离)