Sharding-Proxy use

Sharding-Proxy

**Concept: ** Sharding-Proxy and Sharding-JDBC do basically the same thing. The main difference is that sharding-proxy can extract the database sharding strategy and create an independent virtual database. There is no need for any sharding configuration in our project. , Directly connect to the virtual database generated by sharding-proxy to complete the sub-database sub-table strategy (the sub-database sub-table logic is extracted independently and becomes an independent middleware, providing database operation services externally)

Download: Sharding-Proxy is an independent service that needs to be downloaded, configured and run

Download: Sharding-Proxy Download go to download sharding-proxy

Hello World

Realize horizontal sub-table through Sharding-Proxy

Scenario: The user1 and user2 databases are used in the xiaomu_1 database to store data with odd and even ids, which are implemented through Sharding-Proxy

1 Configure Sharding-Proxy

System configuration : modify conf/server.yaml to configure the Sharding-Proxy system configuration

authentication:
  users:
    root:
      password: root  #root用户密码
    sharding:	#其他用户的配置
      password: sharding 
      authorizedSchemas: sharding_db  #该用户能够访问的数据库

props:
  max.connections.size.per.query: 1
  acceptor.size: 16  # The default value is available processors count * 2.
  executor.size: 16  # Infinite by default.
  proxy.frontend.flush.threshold: 128  # The default value is 128.
    # LOCAL: 代理将与本地事务一起运行
    # XA: 代理将与XA事务一起运行。
    # BASE: 代理将与B.A.S.E交易一起运行。
  proxy.transaction.type: LOCAL
  proxy.opentracing.enabled: false
  proxy.hint.enabled: false
  query.with.cipher.column: true
  sql.show: false
  allow.range.query.with.inline.sharding: false

**Sharding configuration: ** Sharding-Proxy extracts the sharding configuration into config-sharding.yaml, the configuration is basically the same as sharding-jdbc

schemaName: db1

dataSources:
  db1: # 数据库配置 生成 db1虚拟库
    url: jdbc:mysql://localhost:3306/xiaomu_1?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50

#配置表分片策略 
shardingRule:
  tables:
    user:
      actualDataNodes: db1.user${
    
    1..2}
      tableStrategy:
        inline:
          shardingColumn: id
          algorithmExpression: user${
    
     id % 2 ==0 ? 2:1}
      keyGenerator:
        type: userKeyGen
        column: id

2 Start Sharding-Proxy

windows: bin/start.bat

linux: start.sh

Note: The default port for Sharding-Proxy startup is 3307, which can be specified at startup

windows: start.bat 3308

linux : start.sh 3308

3 test

The database connection of the project is changed to connect to Sharding-Proxy and insert data, and the data can be added normally

The configuration of Sharding-Proxy is basically the same as Sharding-JDBC, so this article will not introduce too much.

Sharding-Proxy custom primary key generation strategy and sharding strategy

Sharding-Proxy custom primary key generation strategy and sharding strategy are basically the same as Sharding-JDBC, except that there is one more packaging operation. Pack the written custom class into a jar package and put it into the lib.

1 Create a maven project

Create a maven project to introduce dependencies:

<!-- packaging 打包方式改为 jar包 -->

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-core</artifactId>
    <version>4.1.1</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.10</version>
    <scope>provided</scope>
</dependency>

2 Write the primary key generation class

public class UserKeyGen implements ShardingKeyGenerator {
    
    

    private static AtomicInteger atomicInteger=new AtomicInteger(0);

    @Override
    public Comparable<?> generateKey() {
    
    
        return atomicInteger.incrementAndGet();
    }

    @Override
    public String getType() {
    
    
        return "userKeyGen";
    }



    private Properties properties;

    @Override
    public Properties getProperties() {
    
    
        return properties;
    }

    @Override
    public void setProperties(Properties properties) {
    
    
        this.properties = properties;
    }
}

3 Package and put into the lib directory of Sharding-Proxy

mvn clean package

Put it in the lib directory and use it in the configuration file

schemaName: db1

dataSources:
  db1:
    url: jdbc:mysql://localhost:3306/xiaomu_1?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50


shardingRule:
  tables:
    user:
      actualDataNodes: db1.user${
    
    1..2}
      tableStrategy:
        inline:
          shardingColumn: id
          algorithmExpression: user${
    
     id % 2 ==0 ? 2:1}
      keyGenerator:
        type: userKeyGen  #这里使用了我们自定义的主键生成策略
        column: id

Restart Sharding-Proxy

Guess you like

Origin blog.csdn.net/dndndnnffj/article/details/110133383