当前位置:网站首页>ShardingSphere-proxy +PostgreSQL implements read-write separation (static strategy)
ShardingSphere-proxy +PostgreSQL implements read-write separation (static strategy)
2022-08-02 22:37:00 【Ink Sky Wheel】
In business, the database is read more and written less,And the performance of a single server or a single database can no longer satisfy the current business when a large number of database read requests are made,We may need to do it to the database读写分离.
Common tools that can do read-write separation aremycat、ShardingSphere等.本文使用ShardingSphere-proxy实现了PostgreSQL的读写分离场景,如果对于PostgreSQLIn addition to read-write separation, the database also requires connection pooling and other requirements,还可以了解一下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上完成,It has already been deployedPostgreSQLmaster-slave streaming replication environment.在此不做赘述.
[[email protected] ~]$ 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白名单配置(Here is my test for convenience,如果实际使用,It is best to use the smallest range practicalipsegment to constrain)
host all all 0.0.0.0/0 md5重启数据库
3.Created in the database for testingShardingSphere-proxyRead-write separation of user and database(Permissions are also constrained when used,can be given to the databaseowner,Or adjust as needed)
create user ssp_user_test password '[email protected]' superuser;create database ssp_db_test;三、安装部署ShardingSphere-proxy
在172.20.10.3节点安装proxy
需要安装jdk(must have Java JRE 8 or higher),我本地的jdk版本为
[[email protected] conf]# java -versionopenjdk 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.Get the package and install itShardingSphere-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/ssp2.修改server.yaml,Configure the virtual account password
Configure the virtual account password,使用默认的root用户及密码root,And allow external network connection,Also permissions are specified as “All permissions are unlimited”,sql-showis used to record each entry in the logsqland the executing node,Others are default values
rules: - !AUTHORITY users: - [email protected]%:root# - [email protected]:sharding provider: 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: false sql-show: on如图所示

3.Edit read-write separated files config-readwrite-splitting.yaml
databaseNameis my mapped database name,primary_ds和replica_ds_0respectively as the main library/Write the relevant configuration of the library and the slave library/Read library dependencies are configured.
databaseName: xmaster_db#dataSources: primary_ds: url: jdbc:postgresql://172.20.10.9:5432/ssp_db_test username: ssp_user_test password: [email protected]# connectiontimeoutMilliseconds: 30000# idletimeoutmilliseconds: 60000# maxlifetimemilliseconds: 1800000# maxpoolsize: 50# minpoolsize: 1 replica_ds_0: url: jdbc:postgresql://172.20.10.10:5432/ssp_db_test username: ssp_user_test password: [email protected]:- !READWRITE_SPLITTING dataSources: readwrite_ds: type: Static props: write-data-source-name: primary_ds read-data-source-names: replica_ds_0

四、启动ShardingSphere-proxy并验证
[[email protected] bin]# pwd/root/apache-shardingsphere-5.1.2-shardingsphere-proxy-bin/bin[[email protected] bin]# sh start.sh we find java version: java8, full_version=1.8.0_342Starting 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输出的日志内容
[[email protected] 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 successfullyThanks 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也已经起来了
[[email protected] bin]# ss -tnlState 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 :::* At this time we pass another terminal,进行连接验证,Namely our fourth server172.20.10.6,通过server.yamlvirtual account password,can be connected
[[email protected] ~]$ ip a | grep ens33 | grep global inet 172.20.10.6/28 brd 172.20.10.15 scope global ens33[[email protected] ~]$ psql -h 172.20.10.3 -U root xmaster_db -p 3307Password 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 TABLExmaster_db=> insert into t1 values(1);INSERT 0 1xmaster_db=> select * from t1; id ---- 1(1 row)During data insertion, you can go to the main database of the database to view the data status
ssp_db_test=# \dt List of relations Schema | 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 existLINE 1: select * from t1; ^ssp_db_test=# \dtDid not find any relations.去查看ShardingSphere-proxy的输出日志,发现create、insert、dropAll operations are performed on the master node,而selectExecute on the standby node.
[[email protected] 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;
边栏推荐
- Brain-computer interface 003 | Musk said that he has realized a virtual self-dialogue with the cloud, and related concept shares have risen sharply
- 汇编实例解析--利用tcb,tss,全局tss,ldt管理任务实现任务切换
- 软件测试分类
- Unity 打包和切换平台|Build Settings窗口介绍
- 当TIME_WAIT状态的TCP正常挥手,收到SYN后…
- Compose主题切换——让你的APP也能一键换肤
- 解析List接口中的常用的被实现子类重写的方法
- Electron使用指南之初体验
- NC | Structure and function of soil microbiome reveal N2O release from global wetlands
- 实现客户服务自助,打造产品知识库
猜你喜欢

如何正确地配置入口文件?

实战:10 种实现延迟任务的方法,附代码!

Therapy | How to Identify and Deal with Negative Thoughts

「面试必会」这应该是最有深度的TCP三次握手、四次挥手细节讲解

JVM内存和垃圾回收-03.运行时数据区概述及线程

MySQL安装配置教程(超级详细)

Brain-computer interface 003 | Musk said that he has realized a virtual self-dialogue with the cloud, and related concept shares have risen sharply

In action: 10 ways to implement delayed tasks, with code!

一款好用的FAQ搭建工具

Leetcode刷题——字符串相加相关题目(415. 字符串相加、面试题 02.05. 链表求和、2. 两数相加)
随机推荐
Leetcode刷题——单调栈问题(739每日温度问题、496下一个更大元素I、503下一个更大元素 II)
spack install reports an error /tmp/ccBDQNaB.s: Assembler message:
Leetcode刷题——字符串相加相关题目(415. 字符串相加、面试题 02.05. 链表求和、2. 两数相加)
元宇宙001 | 情绪无法自控?元宇宙助你一臂之力
当TIME_WAIT状态的TCP正常挥手,收到SYN后…
Leetcode刷题——23. 合并K个升序链表
移动跨端技术方案分析对比
MySQL安装配置教程(超级详细)
2022-07-27
「面试必会」这应该是最有深度的TCP三次握手、四次挥手细节讲解
SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)
es DELETE index 源码分析
我用这一招让团队的开发效率提升了 100%!
Detailed explanation of common examples of dynamic programming
thinkphp框架5.0.23安全更新问题-漏洞修复-/thinkphp/library/think/App.php具体怎么改以及为什么要这么改
动态规划常见实例详解
动态生成不同类型的订单,请问如何存放到Mongodb数据库?
Geoserver+mysql+openlayers
idea 配置resin
VMware虚拟机无法上网