ShardingSphere使用(三)(ShardingProxy)

ShardingProxyShardingJDBC不同,ShardingJDBC相当于是一个虚拟数据库,应用开发采用直接连接的方式

  1. 配置文件说明

    # 服务配置
    server.yaml
    # 分片配置
    config-sharding.yaml
    # 影子库(多用于压力测试)
    config-shadow.yaml
    # 主从(读写分离)配置
    config-master_slave.yaml
    # 加密配置
    config-encrypt.yaml
    
  2. server.yaml

    # 数据治理(ShardingProxy 支持将运行信息注册到zookeeper)
    # 其他的ShardingProxy实例可以仅保留此配置并运行,实现扩展出多个数据库代理层
    orchestration:
      orchestration_ds:
        orchestrationType: registry_center,config_center,distributed_lock_manager
        instanceType: zookeeper
        serverLists: localhost:2181
        namespace: orchestration
        props:
          overwrite: false
          retryIntervalMilliseconds: 500
          timeToLiveSeconds: 60
          maxRetries: 3
          operationTimeoutMilliseconds: 500
    
    # ShardingProxy的认证信息
    authentication:
      users:
        # ShardingProxy默认用户root
        root:
          # 连接时密码不是这个(mysql -P 3317 -u root -p)
          password: root
        sharding:
          password: sharding
          # 虚拟数据库,与config-sharding.ymal中的schemaName保持一致
          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: Proxy will run with LOCAL transaction.
        # XA: Proxy will run with XA transaction.
        # BASE: Proxy will run with B.A.S.E transaction.
      proxy.transaction.type: LOCAL
      proxy.opentracing.enabled: false
      proxy.hint.enabled: false
      query.with.cipher.column: true
      sql.show: true
      allow.range.query.with.inline.sharding: false
    
  3. ShardingProxy扩展使用SPI

    编写好对应SPI(含class文件及META-INF/services目录下文件)后,打包放到ShardingProxylib目录中即可

    推荐使用maven-jar-plugin插件指定对应文件打包

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
            <execution>
                <id>ShardingSPIDemo</id>
                <phase>package</phase>
                <goals>
                    <goal>jar</goal>
                </goals>
                <configuration>
                    <!--jar包文件名称后缀(非扩展名)-->
                    <classifier>spiextension</classifier>
                    <!--指定打包内容-->
                    <includes>
                        <include>org/example/ss/spiextension/*</include>
                        <include>META-INF/services/*</include>
                    </includes>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

以上仅为典型配置文件的简要说明,详细配置请查阅官方文档

要在Spring Boot中使用ShardingProxy,需要进行以下步骤: 1. 添加ShardingProxy的依赖,可以在Maven中添加以下配置: ``` <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-proxy</artifactId> <version>${shardingsphere.version}</version> </dependency> ``` 其中`${shardingsphere.version}`表示ShardingSphere的版本号。 2. 在Spring Boot的配置文件中添加ShardingProxy的配置,可以参考以下配置示例: ``` spring: shardingsphere: datasource: names: ds_0, ds_1 ds_0: url: jdbc:mysql://localhost:3306/db_0 username: root password: root driver-class-name: com.mysql.jdbc.Driver ds_1: url: jdbc:mysql://localhost:3306/db_1 username: root password: root driver-class-name: com.mysql.jdbc.Driver sharding: binding-tables: user tables: user: actual-data-nodes: ds_${0..1}.user_${0..1} table-strategy: inline: sharding-column: id algorithm-expression: user_${id % 2} key-generator: column: id type: SNOWFLAKE ``` 其中,`datasource`节点下配置数据源的信息,`sharding`节点下配置分片的策略、数据表的信息和ID生成器的类型等。 3. 在Java代码中使用ShardingProxy连接数据库,示例代码如下: ``` @Autowired private DataSource dataSource; public void queryData() { try (Connection conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement("SELECT * FROM user WHERE id = ?")) { ps.setLong(1, 1); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { System.out.println(rs.getLong("id")); System.out.println(rs.getString("name")); } } } catch (SQLException e) { e.printStackTrace(); } } ``` 这样就可以在Spring Boot中使用ShardingProxy了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值