Introduction of depend on

I am using the native version of SpringBoot 2, which references shardingSphere-JDBC 5.0.0-beta

 <dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
    <version>5.0.0 beta -</version>
</dependency>
Copy the code

Modifying a Configuration File

spring:
  profiles:
    include: common-local
  shardingsphere:
    datasource:
      names: write-ds,read-ds-0
      write-ds:
        jdbcUrl: jdbc:mysql://mysql.local.test.myapp.com:23306/test?allowPublicKeyRetrieval=true&useSSL=false&allowMultiQueries=true&serv erTimezone=Asia/Shanghai&useSSL=false&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=co nvertToNull
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: nicai
        connectionTimeoutMilliseconds: 3000
        idleTimeoutMilliseconds: 60000
        maxLifetimeMilliseconds: 1800000
        maxPoolSize: 50
        minPoolSize: 1
        maintenanceIntervalMilliseconds: 30000
      read-ds-0:
        jdbcUrl: jdbc:mysql://mysql.local.test.read1.myall.com:23306/test?allowPublicKeyRetrieval=true&useSSL=false&allowMultiQueries=tru e&serverTimezone=Asia/Shanghai&useSSL=false&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehav ior=convertToNull
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: nicai
        connectionTimeoutMilliseconds: 3000
        idleTimeoutMilliseconds: 60000
        maxLifetimeMilliseconds: 1800000
        maxPoolSize: 50
        minPoolSize: 1
        maintenanceIntervalMilliseconds: 30000

    rules:
      readwrite-splitting:
        data-sources:
          glapp:
            write-data-source-name: write-ds
            read-data-source-names:
              - read-ds-0
            load-balancer-name: roundRobin # Load balancing algorithm name
        load-balancers:
          roundRobin:
            type: ROUND_ROBIN ROUND_ROBIN ROUND_ROBIN ROUND_ROBIN

Copy the code

The yamL file is transferred from the property configuration file on the official website. Note the following points:

  • Type: com. Zaxxer. Hikari HikariDataSource I with hikari connection pool, according to your actual situation
  • Driver-class-name: com.mysql.cj.jdbc.driver Different versions of mysql vary. For your actual situation, I use mysql 8.0
  • JdbcUrl = jdbcUrl = jdbcUrl = jdbcUrl

Problems encountered

Description:

Configuration property name 'spring.shardingsphere.datasource.write_ds' is not valid:

    Invalid characters: '_'
    Bean: org.apache.shardingsphere.spring.boot.ShardingSphereAutoConfiguration
    Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter

Action:

Modify 'spring.shardingsphere.datasource.write_ds' so that it conforms to the canonical names requirements.

Copy the code

I underlined some of the names in the configuration file earlier, no, use the center line.

test

All the changes are only so much above, or relatively simple, the following read library request incoming monitoring, prove that the read request has come, write library did not.

This is for the library:

This is for library reading:

reference

  • Shardingsphere.apache.org/document/5….