shardingsphere5.3.0读写分离

使用环境:

springboot 2.7.5
shardingsphere-jdbc-core 5.3.0
snakeyaml 1.33

场景、问题:

application.yml

server:
  port: 20010
spring:
  application:
    name: announce_service
  datasource:
    driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
    url: jdbc:shardingsphere:classpath:sharding.yaml

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

sharding.yaml

dataSources:
  ds_1:
    dataSourceClassName: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: xxxxx
    username: xxxxx
    password: xxxxx
    initial-size: 5
    max-active: 20
    min-idle: 5
    max-wait: 60000
    test-while-idle: true
    validation-query: SELECT 1
    db-type: mysql
    test-on-borrow: true
    time-between-eviction-runs-millis: 60000
    min-evictable-idle-time-millis: 30000
    max-pool-prepared-statement-per-connection-size: 20
    pool-prepared-statements: true
    connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    use-global-data-source-stat: true
    keep-alive: true
    filter:
      wall:
        db-type: mysql
        enable:
          true
  ds_2:
    dataSourceClassName: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: xxxxx
    username: xxxxx
    password: xxxxx
    initial-size: 5
    max-active: 20
    min-idle: 5
    max-wait: 60000
    test-while-idle: true
    validation-query: SELECT 1
    db-type: mysql
    test-on-borrow: true
    time-between-eviction-runs-millis: 60000
    min-evictable-idle-time-millis: 30000
    max-pool-prepared-statement-per-connection-size: 20
    pool-prepared-statements: true
    connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    use-global-data-source-stat: true
    keep-alive: true
    filter:
      wall:
        db-type: mysql
        enable:
          true
  ds_3:
    dataSourceClassName: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: xxxxx
    username: xxxxx
    password: xxxxx
    initial-size: 5
    max-active: 20
    min-idle: 5
    max-wait: 60000
    test-while-idle: true
    validation-query: SELECT 1
    db-type: mysql
    test-on-borrow: true
    time-between-eviction-runs-millis: 60000
    min-evictable-idle-time-millis: 30000
    max-pool-prepared-statement-per-connection-size: 20
    pool-prepared-statements: true
    connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    use-global-data-source-stat: true
    keep-alive: true
    filter:
      wall:
        db-type: mysql
        enable:
          true

rules:
  - !READWRITE_SPLITTING
    dataSources:
      readwrite_ds:
        staticStrategy:
          writeDataSourceName: ds_1
          readDataSourceNames:
            - ds_2
            - ds_3
        loadBalancerName: random
    loadBalancers:
      random:
        type: RANDOM

props:
  sql-show: true

已进行操作:

@SpringBootTest
public class DataSourceTest {

@Autowired
private DataSource dataSource;

@Test
void dataSourceTest(){
    System.out.println(dataSource.getClass());
    DruidDataSource druid = (DruidDataSource) dataSource;
    System.out.println(druid.getUsername());
}

}

打印
class com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper
null

现状:

Druid配置没有生效请问我该如何更改?

你好,可以提供一个 demo project 来重现此问题吗?
springboot 项目涉及到一些 pom 依赖,自己造的话会花较多时间,各组件版本还不一定匹配。

sorry,读写分离好像没有问题了,但是有点其他的问题,当我启动程序时发现错误日志(我已经配置了validationQuery)

ERROR 95196 --- [           main] com.alibaba.druid.pool.DruidDataSource   : testWhileIdle is true, validationQuery not set

连接池不是启动时创建而是在收到请求后创建连接池我该如何更改
pom如下

   <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.15</version>
        </dependency>

        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core</artifactId>
            <version>5.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.33</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

demo GitHub - thgcrush/shardingsphere-config

京ICP备2021015875号