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].