springboot integrates mybatis-plus and sharding-jdbc to achieve sub-library, sub-table and separation of reading and writing (including complete project code)

springboot integrates mybatis-plus and sharding-jdbc to achieve sub-library, sub-table and separation of reading and writing (including complete project code)

1. Integrate sharding-jdbc

There are 4 ways to integrate springboot into the sharding-jdbc official website, and the .properties files are all used online. Here I mainly use the combination of yml+bean. Bean is mainly used to achieve more flexible sharding key sharding rules.

1.1Introduce maven dependencies

There is a small pit here. I first introduced the latest sharding-jdbc and mybatis-plus dependencies, but an error was reported when starting the project. Therefore, I personally do not recommend citing the latest package.

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.0.0-RC1</version>
</dependency>
1.2 Configuring sub-databases and tables and separation of reading and writing (core)
spring:
  shardingsphere:
    datasource:
      names: "search-center-00,search-center-00-slave,search-center-01,search-center-01-slave"
      search-center-00:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/search_center_00?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
        username: root
        password: 123456
      search-center-00-slave:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3307/search_center_00?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
        username: root
        password: 123456
      search-center-01:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/search_center_01?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
        username: root
        password: 123456
      search-center-01-slave:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3307/search_center_01?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
        username: root
        password: 123456
    sharding:
      master-slave-rules:
        search-center-00:
          master-data-source-name: search-center-00
          slave-data-source-names: search-center-00-slave
        search-center-01:
          master-data-source-name: search-center-01
          slave-data-source-names: search-center-01-slave
      tables:
        user:
          actual-data-nodes: search-center-$->{(0..1).collect{t ->t.toString().padLeft(2,'0')}}.user_$->{(0..1).collect{t ->t.toString().padLeft(2,'0')}}
          database-strategy:
            standard:
              shardingColumn: id
              precise-algorithm-class-name: com.gory.searchcenter.config.DatabaseAlgorithm
          table-strategy:
            standard:
              sharding-column: id
              precise-algorithm-class-name: com.gory.searchcenter.config.TableAlgorithm
    props:
      sql:
        show: true

names: Customize database names, and then configure these names specifically, such as: url, password, username, etc.

tables: Divide each table into databases and tables. For example, here the user table is divided into databases and tables.

actual-data-nodes: It is the node allocation of user table sub-database sub-table.

database-strategy: It is the strategy of sub-library. Here I configure the sub-library rules more flexibly based on the ID and through beans.

table-strategy: It is the strategy of sharding tables, and the configuration is roughly the same as that of sharding databases.

master-slave-rules: Set master-slave to achieve read-write separation.

DatabaseAlgorithm
public class DatabaseAlgorithm implements PreciseShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> databaseNames, PreciseShardingValue<Long> shardingValue) {
        Long shardResult = Math.abs((long) shardingValue.getValue().hashCode()) % 2;
        for (String databaseName : databaseNames) {
            String shardValue = String.format("%02d", shardResult);
            if (databaseName.endsWith(shardValue)){
                System.out.println("shardingValue:"+shardingValue+", databaseName:" + databaseName);
                return databaseName;
            }
        }
        throw new UnsupportedOperationException();
    }

}
TableAlgorithm
public class TableAlgorithm implements PreciseShardingAlgorithm<Long> {

    /**
     * @param tableNames 有效的数据源 或者 表 的名字  tableNames 就为配置文件中的 配置的数据源信息 -> ds0 , ds1
     * @param shardingValue SQL 分片列 对应的实际值
     * @return 返回相应的表名
     */
    @Override
    public String doSharding(Collection<String> tableNames, PreciseShardingValue<Long> shardingValue) {
        Long shardResult = Math.abs((long) shardingValue.getValue().hashCode()) / 2 % 2;
        for (String tableName : tableNames) {
            String shardValue = String.format("%02d", shardResult);
            if (tableName.endsWith(shardValue)){
                System.out.println("shardingValue:"+shardingValue+", tableName:" + tableName);
                return tableName;
            }
        }
        throw new UnsupportedOperationException();
    }

}

By the way, the shard primary key id here is set using the snowflake algorithm. It is best to assign values ​​to your sharding keys that correspond to your sharding rules.

2. Integrate mybatis-plus

As mentioned in the previous section, introducing the latest maven dependency will result in an error, so I still do not recommend using the latest one.

2.1 Introduce maven dependencies
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.1</version>
</dependency>
2.2 Configuration of mybatis-plus
mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Then start the class and add the package scan table annotation

@MapperScan({"com.gory.searchcenter.mapper"})

3. Database master-slave configuration

In the previous two sections, we have been able to complete database and table partitioning, but because the database does not have a master-slave setting, the slave table does not have a database, resulting in no data in the query. Because this article is mainly for learning, the configuration of mysql is in the window environment.

3.1my.ini configuration
main library

#The main library id cannot be repeated

server_id=1

#Open bin-log

log_bin=E:\lib\mysql-5.7.30-winx64\master-bin.log

#mysql The time to clear expired logs is consistent with the slave database setting
expire_logs_days = 1

#Set the database to be synchronized
binlog-do-db=search_center_00
binlog-do-db=search_center_01
--------------------------------- ---------------------------------- |

From the library

server_id=2

#Open bin-log

log_bin=E:\lib\mysql-5.7.30-winx64-slave\slave-bin.log

expire_logs_days = 1

#Set the database to be synchronized
binlog-do-db=search_center_00
binlog-do-db=search_center_01
--------------------------------- ---------------------------------- |

It is worth mentioning that when setting multiple synchronization tables, they cannot be separated by commas.

3.1 sql configuration
main library

Enable the slave server (Slave) to access the master server (Master)

grant replication slave on . to ‘root’@‘127.0.0.1’ identified by ‘123456’;

View authorization information

show master status ;

From the library

Stop sync

stop slave;

Set the information of the main library and link the slave library with the main library

change master to
master_host='127.0.0.1',
master_user='root',
master_password='123456',
master_log_file='log-bin.000008',
master_log_pos=107;
The two configurations master_log_file and master_log_pos are show master with the master server corresponding to status

Start sync

start salve;

Check slave startup status

show slave status;

If it appears, it means the startup is successful.

Slave_IO_Running:Yes

Slave_SQL_Running:Yes

4. Complete project code

Complete code: https://github.com/gorylee/SearchCenter

Supongo que te gusta

Origin blog.csdn.net/qq798867485/article/details/120604453
Recomendado
Clasificación