在业务对数据库是读多写少,且单台服务器或者单个数据库的性能已经不能满足当前业务对数据库大量读取请求的时候,我们可能需要对数据库做读写分离

常见的可以做读写分离的工具有mycat、ShardingSphere等。本文使用ShardingSphere-proxy实现了PostgreSQL的读写分离场景,如果对于PostgreSQL数据库除读写分离外还需要连接池等需求,还可以了解一下PostgreSQL的pgpool-II。

一、本文相关ip地址

角色 版本 IP地址
主库 PostgreSQL 13.4 172.20.10.9
从库 PostgreSQL 13.4 172.20.10.10
ShardingSphere-proxy 5.1.2 172.20.10.3
用于测试连接ShardingSphere-Proxy(有psql客户端) psql版本 12.9 172.20.10.6

本次安装部署是在CentOS Linux release 7.4上完成,开始已经部署好了PostgreSQL的主从流复制环境。在此不做赘述。

[postgres@primary ~]$ psql -c "select * from pg_stat_replication;"pid  | usesysid | usename | application_name | client_addr  | client_hostname | client_port |         backend_start         | backend_xmin |   state   |  sent_lsn  | write_lsn  | flush_lsn  | replay_lsn |   write_lag    | flush_lag | replay_lag | sync_priority | sync_state |          reply_time
-------+----------+---------+------------------+--------------+-----------------+-------------+-------------------------------+--------------+-----------+------------+------------+------------+------------+---
--------------+-----------+------------+---------------+------------+------------------------------56107 |    16385 | replica | walreceiver      | 172.20.10.10 |                 |       46262 | 2022-08-02 12:02:49.504343+08 |              | streaming | 0/FB841B40 | 0/FB841B40 | 0/FB841B40 | 0/FB841B40 | 00
:00:00.037914 |           |            |             0 | async      | 2022-08-02 12:02:49.65561+08
(1 row)

二、数据库配置

1.postgresql.conf里配置

listen_addresses = '*'

2.pg_hba.conf白名单配置(这里是我测试为了方便,如果实际使用,最好用范围最小的实际ip段来约束)

host    all             all             0.0.0.0/0            md5

重启数据库

3.数据库里创建供测试ShardingSphere-proxy读写分离的用户和数据库(权限在使用时候也要进行约束,可以给数据库的owner,或根据需求调整)

create user ssp_user_test password 'Enmo@123' superuser;
create database ssp_db_test;

三、安装部署ShardingSphere-proxy

在172.20.10.3节点安装proxy

需要安装jdk(must have Java JRE 8 or higher),我本地的jdk版本为

[root@primary conf]# java -version
openjdk version "1.8.0_342"
OpenJDK Runtime Environment (build 1.8.0_342-b07)
OpenJDK 64-Bit Server VM (build 25.342-b07, mixed mode)

1.获取软件包并安装ShardingSphere-proxy

--下载地址
https://shardingsphere.apache.org/document/current/en/downloads/--解压
tar -xvf apache-shardingsphere-5.1.2-shardingsphere-proxy-bin.tar.gz
--做软链
ln -s /root/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin /opt/ssp

2.修改server.yaml,配置虚拟帐号密码

配置虚拟帐号密码,使用默认的root用户及密码root,且允许外网连接,同时权限指定为“所有权限无限制”,sql-show是用于在日志中记录下每条sql及执行的节点,其它为默认值

rules:- !AUTHORITYusers:- root@%:root
#      - sharding@:shardingprovider:type: type: ALL_PERMITTEDprops:
#  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: falsesql-show: on

如图所示

3.编辑读写分离文件 config-readwrite-splitting.yaml

databaseName是我映射的数据库名字,primary_ds和replica_ds_0分别为主库/写库的相关配置和从库/读库的相关被配置。

databaseName: xmaster_db
#
dataSources:primary_ds:url: jdbc:postgresql://172.20.10.9:5432/ssp_db_testusername: ssp_user_testpassword: Enmo@123
#    connectiontimeoutMilliseconds: 30000
#    idletimeoutmilliseconds: 60000
#    maxlifetimemilliseconds: 1800000
#    maxpoolsize: 50
#   minpoolsize: 1replica_ds_0:url: jdbc:postgresql://172.20.10.10:5432/ssp_db_testusername: ssp_user_testpassword: Enmo@123rules:
- !READWRITE_SPLITTINGdataSources:readwrite_ds:type: Staticprops:write-data-source-name: primary_dsread-data-source-names: replica_ds_0

四、启动ShardingSphere-proxy并验证

[root@localhost bin]# pwd
/root/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin/bin
[root@localhost bin]# sh start.sh
we find java version: java8, full_version=1.8.0_342
Starting the ShardingSphere-Proxy ...
The classpath is /root/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin/conf:.:/root/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin/lib/*:/root/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin/ext-lib/*
Please check the STDOUT file: /root/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin/logs/stdout.log

查看ShardingSphere-proxy输出的日志内容

[root@localhost bin]# cat /root/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin/logs/stdout.logThanks for using Atomikos! This installation is not registered yet.
REGISTER FOR FREE at http://www.atomikos.com/Main/RegisterYourDownload and receive:
- tips & advice
- working demos
- access to the full documentation
- special exclusive bonus offers not available to others
- everything you need to get the most out of using Atomikos!
[INFO ] 2022-07-07 20:18:07.629 [main] o.a.s.p.v.ShardingSphereProxyVersion - Database name is `PostgreSQL`, version is `13.4`, database name is `xmaster_db`
[INFO ] 2022-07-07 20:18:07.889 [main] o.a.s.p.frontend.ShardingSphereProxy - ShardingSphere-Proxy Memory mode started successfully
Thanks for using Atomikos! This installation is not registered yet.
REGISTER FOR FREE at http://www.atomikos.com/Main/RegisterYourDownload and receive:
- tips & advice
- working demos
- access to the full documentation
- special exclusive bonus offers not available to others
- everything you need to get the most out of using Atomikos!
[INFO ] 2022-07-07 20:19:27.088 [main] o.a.s.p.v.ShardingSphereProxyVersion - Database name is `PostgreSQL`, version is `13.4`, database name is `xmaster_db`
[INFO ] 2022-07-07 20:19:27.349 [main] o.a.s.p.frontend.ShardingSphereProxy - ShardingSphere-Proxy Memory mode started successfully

ShardingSphere-Proxy已经启动起来了。ShardingSphere-Proxy的默认端口3307也已经起来了

[root@localhost bin]# ss -tnl
State       Recv-Q Send-Q       Local Address:Port                      Peer Address:Port
LISTEN      0      128                      *:6000                                 *:*
LISTEN      0      128                      *:22                                   *:*
LISTEN      0      128                     :::3307                                :::*
LISTEN      0      128                     :::6000                                :::*
LISTEN      0      128                     :::22                                  :::*

这时候我们通过另外的终端,进行连接验证,即我们的第四台服务器172.20.10.6,通过server.yaml里的虚拟账号密码,可以连接进来

[postgres@localhost ~]$ ip a | grep ens33 | grep globalinet 172.20.10.6/28 brd 172.20.10.15 scope global ens33
[postgres@localhost ~]$ psql -h 172.20.10.3  -U root xmaster_db -p 3307
Password for user root:
psql (12.9, server 13.4-ShardingSphere-Proxy 5.1.2)
WARNING: psql major version 12, server major version 13.Some psql features might not work.
Type "help" for help.xmaster_db=> create table t1(id int);
CREATE TABLE
xmaster_db=> insert into t1 values(1);
INSERT 0 1
xmaster_db=> select * from t1;id
----1
(1 row)

插入数据期间可以去数据库的主库查看数据情况

ssp_db_test=# \dtList of relationsSchema | Name | Type  |     Owner
--------+------+-------+---------------public | t1   | table | ssp_user_test
(1 row)ssp_db_test=# select * from t1;id
----1
(1 row)
ssp_db_test=# select * from t1;
ERROR:  relation "t1" does not exist
LINE 1: select * from t1;^
ssp_db_test=# \dt
Did not find any relations.

去查看ShardingSphere-proxy的输出日志,发现create、insert、drop的操作都是在主节点进行的,而select在备机节点执行。

[root@localhost logs]# ll /root/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin/logs/stdout.log
-rw-r--r-- 1 root root 4926 Aug  2 14:46 /root/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin/logs/stdout.log

[INFO ] 2022-08-02 14:45:26.965 [Connection-2-ThreadExecutor] ShardingSphere-SQL - Logic SQL: create table t1(id int);
[INFO ] 2022-08-02 14:45:26.965 [Connection-2-ThreadExecutor] ShardingSphere-SQL - SQLStatement: PostgreSQLCreateTableStatement(containsNotExistClause=false)
[INFO ] 2022-08-02 14:45:26.965 [Connection-2-ThreadExecutor] ShardingSphere-SQL - Actual SQL: primary_ds ::: create table t1(id int);
[INFO ] 2022-08-02 14:45:29.655 [Connection-2-ThreadExecutor] ShardingSphere-SQL - Logic SQL: insert into t1 values(1);
[INFO ] 2022-08-02 14:45:29.655 [Connection-2-ThreadExecutor] ShardingSphere-SQL - SQLStatement: PostgreSQLInsertStatement(withSegment=Optional.empty)
[INFO ] 2022-08-02 14:45:29.655 [Connection-2-ThreadExecutor] ShardingSphere-SQL - Actual SQL: primary_ds ::: insert into t1 values(1);
[INFO ] 2022-08-02 14:45:31.953 [Connection-2-ThreadExecutor] ShardingSphere-SQL - Logic SQL: select * from t1;
[INFO ] 2022-08-02 14:45:31.953 [Connection-2-ThreadExecutor] ShardingSphere-SQL - SQLStatement: PostgreSQLSelectStatement(limit=Optional.empty, lock=Optional.empty, window=Optional.empty)
[INFO ] 2022-08-02 14:45:31.953 [Connection-2-ThreadExecutor] ShardingSphere-SQL - Actual SQL: replica_ds_0 ::: select * from t1;
[INFO ] 2022-08-02 14:46:00.073 [Connection-2-ThreadExecutor] ShardingSphere-SQL - Logic SQL: drop table t1;
[INFO ] 2022-08-02 14:46:00.073 [Connection-2-ThreadExecutor] ShardingSphere-SQL - SQLStatement: PostgreSQLDropTableStatement(containsExistClause=false)
[INFO ] 2022-08-02 14:46:00.073 [Connection-2-ThreadExecutor] ShardingSphere-SQL - Actual SQL: primary_ds ::: drop table t1;

【ShardingSphere-proxy +PostgreSQL实现读写分离(静态策略)】相关推荐

  1. 视频教程-ShardingSphere:SpringBoot2+MybatisPlus+Swagger读写分离-Java

    ShardingSphere:SpringBoot2+MybatisPlus+Swagger读写分离 10多年互联网一线实战经验,现就职于大型知名互联网企业,架构师, 有丰富实战经验和企业面试经验:曾 ...

  2. proxy实现 mysql 读写分离

    实现 mysql 读写分离 图解: 环境: iptables 和 selinux 关闭 proxy:test2 172.25.1.2 Master: test3 172.25.1.3 Slave:te ...

  3. MySQL Proxy实现数据库读写分离

    为什么80%的码农都做不了架构师?>>>    可以将master的数据复制分布到多个slave上,然后可以利用slave来分担master的读压力.那么对于前台应用来说,就要考虑如 ...

  4. MySQL Proxy 安装与读写分离体验

    一直想等到BETA版出来再试验的,可还是经不住诱惑阿,下午终于有时间测试一下了. (本文参考地址:http://blog.chinaunix.net/u/8111/showart.php?id=451 ...

  5. mysql proxy ro-pooling.lua_MySQL读写分离

    随着用户量的增多,数据库操作往往会成为一个系统的瓶颈所在,而且一般的系统"读"的压力远远大于"写",因此我们可以通过实现数据库的读写分离来提高系统的性能.通过设 ...

  6. mysql+读的负载均衡_MySQL Proxy快速实现读写分离以及负载均衡

    作/译者:叶金荣(Email: ),来源:http://imysql.cn,转载请注明作/译者和出处,并且不能用于商业用途,违者必究. 1. 安装 下载已经编译好的安装包,或者预编译安装包均可,在这里 ...

  7. Proxy实现MySQL读写分离

    MySQL读写分离是指让master处理写操作,让slave处理读操作,非常适用于读操作量比较大的场景,可减轻master的压力. 使用mysql-proxy实现mysql的读写分离,mysql-pr ...

  8. MySQL读写分离事务策略实现

    事务策略 之前的实现,是将通过方法名匹配 而不是使用事务策略中的定义 可以使用事务管理策略中的规则匹配 需要修改两个地方 定义切面 定义数据源的AOP切面 定义切面 <!-- 定义AOP切面处理 ...

  9. ShardingSphere(七) 读写分离配置,实现分库读写操作

    概述:本章通过介绍使用ShardingSphere实现数据库的读写分离操作.在实现读写分离之前,数据库的主从同步需要提前配置完成,主从同步实现不由Sharding提供.主从同步可参考上一章节<S ...

最新文章

  1. 我们不知道答案的125个科学问题(16)群体合作行为的演化
  2. openSUSE 11.2 中文输入法 SCIM 安装
  3. CMMI过程域-MA度量和分析
  4. android switch自定义样式,Android自定义Switch样式
  5. 华硕 WL-566gM之信号强度
  6. Web前端技术基础实验报告一之Web前端开发环境配置与HTML基础
  7. 关于word导出pdf时更新域出错的问题
  8. 三种方法求最大公约数
  9. java.sql.SQLException: Cannot create com.liqi.entry.User: com.li.entry.User Query: SELECT * from u
  10. love2d贪吃蛇---画方格
  11. 服务器装win10系统很卡,笔记本电脑安装win10系统后变很卡很慢的解决方法
  12. 广告大腕江南春:广告语是老板的责任
  13. 2016山东计算机春考试题型,2016年山东省春季高考技能考试信息技术类专业考试样题...
  14. 安卓前端开发!移动APP开发框架盘点,附带学习经验
  15. 苹果二代TWS无线耳机AirPods调研
  16. javascript基础复习之函数,定时器,erval函数
  17. 计算机科学与技术专业考数媒,数字媒体技术研究生院校排名
  18. 将电脑本地视频转成rtsp和rtmp视频流
  19. 过去十个月的总结 心情小记
  20. 山西将部署5万个5G基站,并对5G基站进行电价补贴

热门文章

  1. C语言编写生成无限长度的正/倒三角形
  2. Java下载Excel的模板文件
  3. 红光光浴真的有用吗?#大健康#红光光浴#红光#种光光学
  4. 在高温环境下利用金属钋、铍和锂制备氚化锂的方法
  5. 计算机软件与理论专业大学排名,2012年计算机软件与理论、计算机应用技术分专业全国大学排名...
  6. matlab step函数的用法,matlab中step用法
  7. 成都大学生接连遭遇购物网“被注册” 疑信息泄露
  8. 2010年架构社区回顾:悠长的一年
  9. springboot在线订餐系统、
  10. Spring2.5+Struts1.3.8+JPA(Hibernate实现)整合之三