Sharding-jdbc 四主四从 8库24表水平拆分读写分离配置,及MyCat 四主四从8库8表水平拆分读写分离配置
【摘要】
一、Sharding-jdbc 四主四从 8库24表水平拆分读写分离配置
如果对Sharding-jdbc不了解,关于Sharding-jdbc的介绍和配置,请查看我的另一篇博客:
https:...
一、Sharding-jdbc 四主四从 8库24表水平拆分读写分离配置
如果对Sharding-jdbc不了解,关于Sharding-jdbc的介绍和配置,请查看我的另一篇博客:
https://blog.csdn.net/qq_43692950/article/details/110148077
Sharding-jdbc 配置四主四从 8库24表水平拆分读写分离:application.yml
整体架构:
主机 | 数据库 | 数据表 |
---|---|---|
192.168.1.1 | db(数据库1-主) | user_info_1,user_info_2,user_info_3 |
192.168.1.2 | db(数据库1-从) | user_info_1,user_info_2,user_info_3 |
192.168.1.3 | db(数据库2-主) | user_info_1,user_info_2,user_info_3 |
192.168.1.4 | db(数据库2-从) | user_info_1,user_info_2,user_info_3 |
192.168.1.5 | db(数据库3-主) | user_info_1,user_info_2,user_info_3 |
192.168.1.6 | db(数据库3-从) | user_info_1,user_info_2,user_info_3 |
192.168.1.7 | db(数据库4-主) | user_info_1,user_info_2,user_info_3 |
192.168.1.8 | db(数据库4-从) | user_info_1,user_info_2,user_info_3 |
当进行插入数据时根据id%4 确定主数据库id,然后根据id%3确定该库下的数据表进行填充数据。
当进行查询时,根据id%4确定从数据库id,,然后根据id%3确定该库下的数据表进而拿到数据,
在存储上实现多库多表水平拆分,在动作上实现读写分离。
具体配置方法:
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
#数据源配置
datasource:
names:
master1,master2,master3,master4,slave1,slave2,slave3,slave4
master1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.1:3306/db?characterEncoding=utf-8
username: root
password: root
slave1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.2:3306/db?characterEncoding=utf-8
username: root
password: root
master2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.3:3306/db?characterEncoding=utf-8
username: root
password: root
slave2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.4:3306/db?characterEncoding=utf-8
username: root
password: root
master3:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.5:3306/db?characterEncoding=utf-8
username: root
password: root
slave3:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.6:3306/db?characterEncoding=utf-8
username: root
password: root
master4:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.7:3306/db?characterEncoding=utf-8
username: root
password: root
slave4:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.8:3306/db?characterEncoding=utf-8
username: root
password: root
sharding:
tables:
user_info:
actual-data-nodes: ds$->{1..4}.user_info_$->{0..2}
table-strategy:
inline:
sharding-column: id
algorithm-expression: user_info_$->{id % 3 }
key-generator:
column: id
type: SNOWFLAKE
binding-tables: user_info
broadcast-tables: t_config
default-database-strategy:
inline:
sharding-column: id
algorithm-expression: master$->{id % 4+1}
master-slave-rules:
ds1:
master-data-source-name: master1
slave-data-source-names: slave1
ds2:
master-data-source-name: master2
slave-data-source-names: slave2
ds3:
master-data-source-name: master3
slave-data-source-names: slave3
ds4:
master-data-source-name: master4
slave-data-source-names: slave4
props:
sql:
show: true
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
二、MyCat 四主四从8库8表水平拆分读写分离配置
如果对MyCat 不了解,关于MyCat的介绍和配置,请看我的另两篇博客:
https://blog.csdn.net/qq_43692950/article/details/107731431
https://blog.csdn.net/qq_43692950/article/details/107768397
整体架构:
主机 | 数据库 | 数据表 |
---|---|---|
192.168.1.1 | db(数据库1-主) | user_info |
192.168.1.2 | db(数据库1-从) | user_info |
192.168.1.3 | db(数据库2-主) | user_info |
192.168.1.4 | db(数据库2-从) | user_info |
192.168.1.5 | db(数据库3-主) | user_info |
192.168.1.6 | db(数据库3-从) | user_info |
192.168.1.7 | db(数据库4-主) | user_info |
192.168.1.8 | db(数据库4-从) | user_info |
- conf/rule.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<tableRule name="role2">
<rule>
<columns>id</columns> <!--根据数据库的那个字段做依据-->
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!--指定分片数量(数据库的数量),不可以被更改-->
<property name="count">4</property>
</function>
</mycat:rule>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="mycat_testdb" checkSQLschema="false" sqlMaxLimit="100">
<table name="user_info" dataNode="dn0,dn1,dn2,dn3" rule="role2" />
</schema>
<dataNode name="dn0" dataHost="localhost1" database="db" />
<dataNode name="dn1" dataHost="localhost2" database="db" />
<dataNode name="dn2" dataHost="localhost3" database="db" />
<dataNode name="dn3" dataHost="localhost4" database="db" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.1.1:3306" user="root" password="root">
<readHost host="hostS2" url="192.168.1.2:3306" user="root" password="root" />
</writeHost>
</dataHost>
<dataHost name="localhost2" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.1.3:3306" user="root" password="root">
<readHost host="hostS2" url="192.168.1.4:3306" user="root" password="root" />
</writeHost>
</dataHost>
<dataHost name="localhost3" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.1.5:3306" user="root" password="root">
<readHost host="hostS2" url="192.168.1.6:3306" user="root" password="root" />
</writeHost>
</dataHost>
<dataHost name="localhost4" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.1.7:3306" user="root" password="root">
<readHost host="hostS2" url="192.168.1.8:3306" user="root" password="root" />
</writeHost>
</dataHost>
</mycat:schema>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">mycat_testdb</property>
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">mycat_testdb</property>
<property name="readOnly">true</property>
</user>
</mycat:server>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
文章来源: blog.csdn.net,作者:小毕超,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_43692950/article/details/110209714
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)