方便更快捷的说明问题,可以按需填写(可删除)
使用环境:
shardingSphere 5.1.0
以下是配置,因为测试环境没有主从环境所以数据源就配置了一个,读写也都是同一个数据源
spring:
autoconfigure:
exclude: org.springframework.cloud.gateway.config.GatewayAutoConfiguration,org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
# username: ${MYSQL_USER:root}
# password: ${MYSQL_PWD:Gydev123#}
username: ${MYSQL_USER:gydev}
password: ${MYSQL_PWD:Gy123!@#}
url: jdbc:mysql://${MYSQL_HOST:gyscada-mysql}:${MYSQL_PORT:3306}/${MYSQL_DB:gyscadax_internet}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true
stat-view-servlet:
enabled: true
allow: “”
url-pattern: /druid/*
#login-username: admin
#login-password: admin
filter:
stat:
enabled: true
log-slow-sql: true
slow-sql-millis: 10000
merge-sql: false
wall:
config:
multi-statement-allow: true
#shardingjdbc主要配置
shardingsphere:
# 是否启用sharding
enabled: true
props:
# 是否显示sql
sql-show: true
sql-federation-enabled : true
datasource:
names: master
master:
# type: com.zaxxer.hikari.HikariDataSource
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://${MYSQL_HOST:gyscada-mysql}:${MYSQL_PORT:3306}/${MYSQL_DB:gyscadax_internet}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true
username: ${MYSQL_USER:gydev}
password: ${MYSQL_PWD:Gy123!@#}
# slave1:
# # type: com.zaxxer.hikari.HikariDataSource
# type: com.alibaba.druid.pool.DruidDataSource
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://${MYSQL_HOST:gyscada-mysql-slave}:${MYSQL_PORT:3306}/${MYSQL_DB:gyscadax_internet}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true
# username: ${MYSQL_USER:root}
# password: ${MYSQL_PWD:Gydev123#}
rules:
readwrite-splitting:
data-sources:
ms:
# 读写分离类型,比如:Static,Dynamic,动态方式需要配合高可用功能
type: Static
loadBalancerName: round-robin
props:
# 注意,如果接口有事务,读写分离不生效,默认全部使用主库,为了保证数据一致性
write-data-source-name: master
read-data-source-names: master
load-balancers:
#名称自定义,跟上边的loadBalancerName配置的值保持一致
round-robin:
type: ROUND_ROBIN
sharding:
tables:
machine_collect_item_history: # 分表,逻辑表名
# 节点数据
actual-data-nodes: master.machine_collect_item_history
table-strategy: # 配置分表策略
standard: # 用于单分片键的标准分片场景
sharding-column: collection_time
sharding-algorithm-name: tb-score-inline
# 分片算法配置
sharding-algorithms:
tb-score-inline: # 分片算法名称 不能使用下划线
type: CLASS_BASED
props:
strategy: standard
algorithmClassName: com.gyzh.gyscada.internet.algorithm.YearMonthShardingAlgorithm
场景、问题:
数据分片与读写分离单个功能是正常的,一起配置后进入策略类已经返回真实表名进行查询后报错如图