未能正确路由到正确的数据源

方便更快捷的说明问题,可以按需填写(可删除)

使用环境:Centos7+MySQL5.7+Proxy 5.1.2+java version “17.0.4”

场景、问题:

场景:不同的租户根据租户id分到独立的数据库实例中,即:用中间件根据租户id路由到不同的数据源
问题:根据租户id简单取模后,未能正确的路由到数据源

已进行操作:

server.xml配置:

mode:
type: Standalone
repository:
type: File
overwrite: true

rules:
- !AUTHORITY
users:
- root@%:root
- sharding@:sharding
provider:
type: ALL_PERMITTED
- !TRANSACTION
defaultType: XA
providerType: Atomikos
- !SQL_PARSER
sqlCommentParseEnabled: true
sqlStatementCache:
initialCapacity: 2000
maximumSize: 65535
parseTreeCache:
initialCapacity: 128
maximumSize: 1024

config-sharding.yaml配置:

databaseName: sharding_db

dataSources:
ds_0:
url: jdbc:mysql://aa:3306/sharding_db?serverTimezone=UTC&useSSL=false
username: sharding_db
password: xxxx
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 10
ds_1:
url: jdbc:mysql://bb:3306/sharding_db?serverTimezone=UTC&useSSL=false
username: sharding_db
password: xxxx
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 10

rules:
- !SHARDING
defaultDatabaseStrategy:
standard:
shardingColumn: organization_id
shardingAlgorithmName: database_inline
defaultTableStrategy:
none:

shardingAlgorithms:
database_inline:
type: INLINE
props:
algorithm-expression: ds_${organization_id % 2}

props:
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: true
check-table-metadata-enabled: false
show-process-list-enabled: false
Netty decide.

proxy-backend-driver-type: JDBC
proxy-mysql-default-version: 5.7.22 # In the absence of schema name, the default version will be used.
proxy-default-port: 3307 # Proxy default port.
proxy-netty-backlog: 1024 # Proxy netty backlog.

现状:正确的应该路由到ds_1数据源,但实际路由到ds_0,如图:

缺少 tables 配置,完整配置格式可以参考这个帖子中的例子

1 个赞
京ICP备2021015875号