Sharding-Proxy使用

Sharding-Proxy

**概念: ** Sharding-Proxy 和Sharding-JDBC所作的事情基本相同,主要的区别是sharding-proxy可以将数据库分片策略提取出来,创建一个独立的虚拟数据库,我们项目中无需任何分片配置,直接连接sharding-proxy生成的虚拟数据库即可完成分库分表策略(将分库分表逻辑独立提取出来,变为一个独立的中间件,对外提供数据库操作服务)

下载: Sharding-Proxy 是一个独立的服务,需要下载,配置和运行

下载地址:Sharding-Proxy下载地址 进去下载sharding-proxy 即可

Hello World

通过Sharding-Proxy 实现水平分表

场景: xiaomu_1数据库内用 user1和user2数据库,分别存放 id为奇数和偶数的数据,通过Sharding-Proxy 进行实现

1 配置 Sharding-Proxy

系统配置: 修改conf/server.yaml 配置Sharding-Proxy系统配置

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-Proxy 将 分片配置提取到了 config-sharding.yaml 里面,配置和 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 启动 Sharding-Proxy

windows: bin/start.bat

linux: start.sh

注意: Sharding-Proxy启动默认端口为 3307 启动时可指定

windows: start.bat 3308

linux : start.sh 3308

3 测试

项目的数据库连接改为连接Sharding-Proxy 后插入数据,数据能够正常添加

Sharding-Proxy 的配置和Sharding-JDBC基本一样,本文不做过多介绍.

Sharding-Proxy自定义主键生成策略和分片策略

Sharding-Proxy 自定义主键生成策略和分片策略 和Sharding-JDBC基本一样,只不过多了一个打包操作,将编写的自定义类打包为jar包后放入lib内即可

1 创建maven项目

创建maven项目引入依赖:

<!-- 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 编写主键生成类

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 打包放入Sharding-Proxy 的lib目录下

mvn clean package

放入lib目录,后在配置文件内使用

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

重启Sharding-Proxy 即可

猜你喜欢

转载自blog.csdn.net/dndndnnffj/article/details/110133383