0101prox-shardingsphere-中间件

1 启动ShardingSphere-Proxy

1.1 获取

目前 ShardingSphere-Proxy 提供了 3 种获取方式:

这里我们使用Docker安装。

1.2 使用Docker安装

step1:启动Docker容器

shell 复制代码
docker run -d \
-v /Users/gaogzhen/data/docker/shardingsphere/proxy/proxy-a/conf:/opt/shardingsphere-proxy/conf \
-v /Users/gaogzhen/data/docker/shardingsphere/proxy/proxy-a/ext-lib:/opt/shardingsphere-proxy/ext-lib \
-e ES_JAVA_OPTS="-Xmx256m -Xms256m -Xmn128m" \
-p 3321:3307 \
--name server-proxy-a \
apache/shardingsphere-proxy:5.1.1

docker run -d -e ES_JAVA_OPTS="-Xmx256m -Xms256m -Xmn128m" -p 3322:3307 --name server-proxy-a1  apache/shardingsphere-proxy:5.1.1

step2:上传MySQL驱动

将MySQl驱动上传至/Users/gaogzhen/data/docker/shardingsphere/proxy/proxy-a/ext-lib目录

spte3:修改配置server.yaml

yaml 复制代码
rules:
  - !AUTHORITY
    users:
      - root@%:root
    provider:
      type: ALL_PRIVILEGES_PERMITTED

props:
  sql-show: true

将配置文件上传至/atguigu/server/proxy-a/conf目录

spte4:重启容器

shell 复制代码
docker restart server-proxy-a

step5:远程连接ShardingSphere-Proxy

ShardingSphere-Proxy容器中默认情况下没有mysql命令行客户端的安装,因此需要远程访问

shell 复制代码
mysql -h192.168.100.201 -P3321 -uroot -p

step6:访问测试

sql 复制代码
show databases;

常见问题:docker容器无法远程连接

容器可以成功的创建并启动,但是无法远程连接。排除防火墙和网络等问题后,看看是不是因为容器内存不足导致。

原因:容器可分配内存不足

查看办法:进入容器后查看ShardingSphere-Proxy的日志,如有有cannot allocate memory,则说明容器内存不足

shell 复制代码
docker exec -it server-proxy-a env LANG=C.UTF-8 /bin/bash
cd /opt/shardingsphere-proxy/logs
tail stdout.log 

解决方案:创建容器的时候使用JVM参数

shell 复制代码
-e ES_JAVA_OPTS="-Xmx256m -Xms256m -Xmn128m"

2 ShardingSphere-Proxy读写分离

2.1 修改配置文件

修改配置config-readwrite-splitting.yaml

yaml 复制代码
schemaName: readwrite_splitting_db

dataSources:
  write_ds:
    url: jdbc:mysql://172.17.0.2:3306/db_user?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  read_ds_0:
    url: jdbc:mysql://172.17.0.3:3306/db_user?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  read_ds_1:
    url: jdbc:mysql://172.17.0.4:3306/db_user?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1

rules:
- !READWRITE_SPLITTING
  dataSources:
    readwrite_ds:
      type: Static
      props:
        write-data-source-name: write_ds
        read-data-source-names: read_ds_0,read_ds_1
  • 这里我的数据源IP地址使用的docker默认网络分配的地址

将配置文件上传至/Users/gaogzhen/data/docker/shardingsphere/proxy/proxy-a/conf目录

重启容器

shell 复制代码
docker restart server-proxy-a

2.2 实时查看日志

可以通过这种方式查看服务器中输出的SQL语句

shell 复制代码
docker exec -it server-proxy-a env LANG=C.UTF-8 /bin/bash
tail -f /opt/shardingsphere-proxy/logs/stdout.log 

2.3 远程访问测试

sql 复制代码
mysql> show databases;
mysql> use readwrite_splitting_db;
mysql> show tables;
mysql> select * from t_user;
mysql> select * from t_user;
mysql> insert into t_user(name) values('李四');

如下图2.3-1所示:

2.4 应用程序访问Proxy

4.1、创建项目

项目类型:Spring Initializr

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

项目名:sharding-proxy-demo

SpringBoot版本:2.7.6

4.2、添加依赖

xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

4.3、创建实体类

java 复制代码
package com.gaogzhen.shardingsphereproxydemo.entity;

@TableName("t_user")
@Data
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String uname;
}

4.4、创建Mapper

java 复制代码
package com.gaogzhen.shardingsphereproxydemo.mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

4.5、配置数据源

properties 复制代码
# 应用名称
spring.application.name=sharding-proxy-demo
# 开发环境设置
spring.profiles.active=dev

#mysql数据库连接(proxy)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3321/readwrite_splitting_db?serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

4.6、测试

java 复制代码
package com.gaogzhen.shardingsphereproxydemo;

import com.gaogzhen.shardingsphereproxydemo.entity.User;
import com.gaogzhen.shardingsphereproxydemo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class ShardingsphereProxyDemoApplicationTests {

    @Autowired
    private UserMapper userMapper;

    /**
     * 读数据测试
     */
    @Test
    public void testSelectAll(){
        List<User> users = userMapper.selectList(null);
        users.forEach(System.out::println);
    }

}

3 ShardingSphere-Proxy垂直分片

3.1 修改配置文件

修改配置config-sharding.yaml

yaml 复制代码
schemaName: sharding_db

dataSources:
  ds_0:
    url: jdbc:mysql://172.17.0.2:3301/db_user?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  ds_1:
    url: jdbc:mysql://172.17.0.3:3302/db_order?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1

rules:
- !SHARDING
  tables:
    t_user:
      actualDataNodes: ds_0.t_user
    t_order:
      actualDataNodes: ds_1.t_order

3.2 实时查看日志

可以通过这种方式查看服务器中输出的SQL语句

shell 复制代码
docker exec -it server-proxy-a env LANG=C.UTF-8 /bin/bash
tail -f /opt/shardingsphere-proxy/logs/stdout.log 

3.3 远程访问测试

sql 复制代码
mysql> show databases;
mysql> use sharding_db;
mysql> show tables;
mysql> select * from t_order;
mysql> select * from t_user;

4 ShardingSphere-Proxy水平分片

4.1 修改配置文件

修改配置config-sharding.yaml

yaml 复制代码
schemaName: sharding_db

dataSources:
  ds_user:
    url: jdbc:mysql://172.17.0.2:3301/db_user?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  ds_order0:
    url: jdbc:mysql://172.17.0.3:3310/db_order?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  ds_order1:
    url: jdbc:mysql://172.17.0.4:3311/db_order?serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1

rules:
- !SHARDING
  tables:
    t_user:
      actualDataNodes: ds_user.t_user

    t_order:
      actualDataNodes: ds_order${0..1}.t_order${0..1}
      databaseStrategy:
        standard:
          shardingColumn: user_id
          shardingAlgorithmName: alg_mod
      tableStrategy:
        standard:
          shardingColumn: order_no
          shardingAlgorithmName: alg_hash_mod
      keyGenerateStrategy:
        column: id
        keyGeneratorName: snowflake
    t_order_item:
      actualDataNodes: ds_order${0..1}.t_order_item${0..1}
      databaseStrategy:
        standard:
          shardingColumn: user_id
          shardingAlgorithmName: alg_mod
      tableStrategy:
        standard:
          shardingColumn: order_no
          shardingAlgorithmName: alg_hash_mod
      keyGenerateStrategy:
        column: id
        keyGeneratorName: snowflake

  bindingTables:
    - t_order,t_order_item


  broadcastTables:
    - t_dict

  shardingAlgorithms:
    alg_inline_userid:
      type: INLINE
      props:
        algorithm-expression: server-order$->{user_id % 2}
    alg_mod:
      type: MOD
      props:
        sharding-count: 2
    alg_hash_mod:
      type: HASH_MOD
      props:
        sharding-count: 2
  
  keyGenerators:
    snowflake:
      type: SNOWFLAKE

4.2 实时查看日志

可以通过这种方式查看服务器中输出的SQL语句

shell 复制代码
docker exec -it server-proxy-a env LANG=C.UTF-8 /bin/bash
tail -f /opt/shardingsphere-proxy/logs/stdout.log 

4.3 远程访问测试

sql 复制代码
mysql> show databases;
mysql> use sharding_db;
mysql> show tables;
mysql> select * from t_order; --测试水平分片
mysql> select * from t_dict; --测试广播表

如下图4.3.-1所示:

结语

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

QQ:806797785

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

参考链接:

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

[2]0101读写分离测试-jdbc-shardingsphere-中间件[CP/OL].

[3]0102垂直分片-jdbc-shardingsphere[CP/OL].

[4]0103水平分片-jdbc-shardingsphere[CP/OL].

相关推荐
applebomb21 天前
vite server正则表达式
正则·vite·proxy·regexp·转发·server
路宇2 个月前
Android studio设置国内镜像代理(HTTP Proxy)教程详解
android·http·android studio·proxy·代理
wuweijie@apache.org3 个月前
IntelliJ IDEA 集成 ShardingSphere-JDBC 访问分库分表
ide·intellij-idea·shardingsphere·分库分表
命运之手3 个月前
【Gradle】Gradle指定JDK位置,JVM参数,网络代理配置
android·gradle·proxy·网络代理·properties
Hello-Brand3 个月前
数据库系列: 主流分库分表中间件介绍(图文总结)
mysql·shardingsphere·分库分表·mycat·数据库中间件·vitess
williamdsy4 个月前
【ubuntu】代理设置总结,最近在代理这里踩了很多坑,大致理解了linux的代理机制
linux·运维·ubuntu·proxy·unknow·7890
ZZDICT4 个月前
Zabbix分布式监控
zabbix·proxy·分布式监控
Revelation_of_Turing5 个月前
Michael.W基于Foundry精读Openzeppelin第59期——Proxy.sol
proxy·solidity·openzeppelin·foundry·evm·代理合约·delegatecall
wanmei0025 个月前
grpc代理服务的实现(二)
golang·proxy·grpc·代理