create yml for springboot shardingsphere 5.11,use three datasource,use another database when this is read only on write
时间: 2024-04-29 11:26:03 浏览: 141
springboot3.2.3集成shardingsphere5.4.1及动态数据源demo项目
Sure, here's an example of a YML file for a Spring Boot application using Shardingsphere 5.11 with three data sources and switching to a read-only database when necessary:
```yml
spring:
shardingsphere:
datasource:
names: ds0, ds1, ds2
ds0:
url: jdbc:mysql://localhost:3306/db_master?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
ds1:
url: jdbc:mysql://localhost:3306/db_slave1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
ds2:
url: jdbc:mysql://localhost:3306/db_slave2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
props:
sql:
show: true
sharding:
tables:
user:
actual-data-nodes: ds$->{0..2}.user_$->{0..1}
table-strategy:
inline:
sharding-column: id
algorithm-expression: user_$->{id % 2}
key-generator:
column: id
type: SNOWFLAKE
worker-id: 123
default-database-strategy:
inline:
sharding-column: user_id
algorithm-expression: ds$->{user_id % 3}
master-slave-rules:
ms_ds:
master-data-source-name: ds0
slave-data-source-names: ds1, ds2
read-write-splitting-rules:
right_db:
write-data-source-name: ds0
read-data-source-names: ds1, ds2
load-balance-algorithm-type: ROUND_ROBIN
props:
sql:
show: true
# When in read-only mode, switch to using the read-only database.
# This assumes the read-only database is ds2.
read-only:
enabled: true
data-sources: ds1
replica-query:
enable: true
```
I hope this helps! Let me know if you have any more questions.
阅读全文