Shardingsphere-Proxy-Install
一、Shardingsphere Proxy 安装 (Docker版)
# 下载镜像
docker pull apache/shardingsphere-proxy:5.2.1
# 创建文件夹
mkdir -p //d/docker/shardingsphere-proxy/{conf,data,logs,ext-lib}
# 获取配置文件
docker run -d --name shardingsphere_proxy_temp --entrypoint=bash apache/shardingsphere-proxy:5.2.1 \
&& docker cp shardingsphere_proxy_temp:/opt/shardingsphere-proxy/conf d:/docker/shardingsphere-proxy/ \
&& docker rm shardingsphere_proxy_temp
# 安装Mysql驱动
wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.11/mysql-connector-java-8.0.11.jar -O d:/docker/shardingsphere-proxy/ext-lib/mysql-connector-java-8.0.11.jar
# 运行容器
docker run -d \
--publish 13307:3307 \
--volume //d/docker/shardingsphere-proxy/ext-lib:/opt/shardingsphere-proxy/ext-lib \
--volume //d/docker/shardingsphere-proxy/conf:/opt/shardingsphere-proxy/conf \
--volume //d/docker/shardingsphere-proxy/logs:/opt/shardingsphere-proxy/logs \
--env PORT=3307 \
--net dev \
--ip 172.18.0.6 \
--restart=on-failure:3 \
--name shardingsphere-proxy \
apache/shardingsphere-proxy:5.2.1
docker exec -it -u root shardingsphere-proxy /bin/bash
注: 配置文件部分按照后续章节编写
二、Shardingsphere Proxy 安装 (二进制版)
1. 前置条件
运行需要Java环境,Jdk版本在 1.8 以上
2. 安装
# 下载安装包
wget https://dlcdn.apache.org/shardingsphere/5.4.1/apache-shardingsphere-5.4.1-shardingsphere-proxy-bin.tar.gz -O apache-shardingsphere-5.4.1-shardingsphere-proxy-bin.tar.gz
# 解压
tar -zxvf apache-shardingsphere-5.4.1-shardingsphere-proxy-bin.tar.gz -C /usr/local
mv apache-shardingsphere-5.4.1-shardingsphere-proxy-bin shardingsphere-proxy
# 下载Mysql依赖
mkdir -p /usr/local/shardingsphere-proxy/ext-lib
wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.11/mysql-connector-java-8.0.11.jar -O /usr/local/shardingsphere-proxy/ext-lib/mysql-connector-java-8.0.11.jar
操作指令
# 启动
/usr/local/shardingsphere-proxy/bin/start.sh
# 停止
/usr/local/shardingsphere-proxy/bin/stop.sh
# 查看日志
tail -f logs/stdout.log
# 使用 psql 命令行连接
psql -h 172.21.7.11 -p 3307 -U root -W root -d readwrite_pgsql
# 查看数据表
SELECT tablename FROM pg_tables WHERE schemaname='public';
注: 配置文件部分按照后续章节编写
三、配置 conf/server.yaml
配置ShardingSphere-Proxy 运行模式 认证信息及其他属性等
1. 配置 server.yaml (5.2.1 版本)
mode:
type: Standalone
repository:
type: JDBC
rules:
- !AUTHORITY
users:
- root@%:root
- sharding@:sharding
provider:
type: ALL_PRIVILEGES_PERMITTED
props:
# max-connections-size-per-query: 1
# kernel-executor-size: 16 # Infinite by default.
# proxy-frontend-flush-threshold: 128 # The default value is 128.
# proxy-opentracing-enabled: false
# proxy-hint-enabled: false
sql-show: true
2. 配置 server.yaml (5.4.1 版本)
mode:
type: Standalone
repository:
type: JDBC
authority:
users:
- user: root@%
password: root
- user: sharding
password: sharding
- user: postgres
password: postgres
privilege:
type: ALL_PERMITTED
props:
# max-connections-size-per-query: 1
# kernel-executor-size: 16 # Infinite by default.
# proxy-frontend-flush-threshold: 128 # The default value is 128.
# proxy-hint-enabled: false
sql-show: on
四、配置 conf/config-*.yaml
修改 conf 目录下以 config- 前缀开头的文件,如:conf/config-sharding.yaml 文件,进行分片规则、读写分离规则配置。配置方式请参考配置手册。config-*.yaml 文件的 * 部分可以任意命名。 ShardingSphere-Proxy 支持配置多个逻辑数据源,每个以 config- 前缀命名的 YAML 配置文件,即为一个逻辑数据源。
1. Mysql 读写分离配置 conf/config-mysql-rws.yaml (5.2.1 版本)
databaseName: readwrite_mysql
dataSources:
write_ds:
url: jdbc:mysql://192.168.3.49:3316/mes_technical?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: admin
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
read_ds_0:
url: jdbc:mysql://192.168.3.49:3317/mes_technical?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: admin
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
rules:
- !READWRITE_SPLITTING
dataSources:
readwrite_ds:
staticStrategy:
writeDataSourceName: write_ds
readDataSourceNames:
- read_ds_0
loadBalancerName: random
loadBalancers:
random:
type: RANDOM
2. Mysql 读写分离配置 conf/config-mysql-rws.yaml (5.4.1 版本)
databaseName: readwrite_mysql
dataSources:
write_ds:
url: jdbc:mysql://192.168.3.49:3316/mes_technical?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: admin
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
read_ds_0:
url: jdbc:mysql://192.168.3.49:3317/mes_technical?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: admin
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
rules:
- !READWRITE_SPLITTING
dataSources:
readwrite_ds:
writeDataSourceName: write_ds
readDataSourceNames:
- read_ds_0
loadBalancerName: random
loadBalancers:
random:
type: RANDOM
- !SINGLE
tables:
# MySQL 风格
# - ds_0.t_single # 加载指定单表
# - ds_1.* # 加载指定数据源中的全部单表
- "*.*" # 加载全部单表
3. Pgsql 读写分离配置 conf/config-pgsql-rws.yaml (5.2.1 版本)
databaseName: readwrite_pgsql
dataSources:
primary_ds:
url: jdbc:postgresql://192.168.3.49:5433/mes_technical
username: postgres
password: postgres
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
replica_ds_0:
url: jdbc:postgresql://192.168.3.49:5434/mes_technical
username: postgres
password: postgres
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
rules:
- !READWRITE_SPLITTING
dataSources:
readwrite_ds:
staticStrategy:
writeDataSourceName: primary_ds
readDataSourceNames:
- replica_ds_0
loadBalancerName: random
loadBalancers:
random:
type: RANDOM