Closed MrXionGe closed 3 years ago
What is your config?
@terrymanu
spring.profiles.active=replica-query
spring.shardingsphere.datasource.names=primary_ds,replica_ds_0,replica_ds_1
spring.shardingsphere.datasource.primary_ds.jdbc-url=jdbc:mysql://127.0.0.1:3306/db_master?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.primary_ds.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.primary_ds.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.primary_ds.username=web_app
spring.shardingsphere.datasource.primary_ds.password=123456
spring.shardingsphere.datasource.replica_ds_0.jdbc-url=jdbc:mysql://127.0.0.1:3306/db_read_0?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.replica_ds_0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.replica_ds_0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.replica_ds_0.username=web_app
spring.shardingsphere.datasource.replica_ds_0.password=123456
spring.shardingsphere.datasource.replica_ds_1.jdbc-url=jdbc:mysql://127.0.0.1:3306/db_read_1?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.replica_ds_1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.replica_ds_1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.replica_ds_1.username=web_app
spring.shardingsphere.datasource.replica_ds_1.password=123456
spring.shardingsphere.rules.replica-query.load-balancers.round-robin.type=ROUND_ROBIN
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.primary-data-source-name=primary_ds
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.replica-data-source-names=replica_ds_0,replica_ds_1
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.load-balancer-name=round-robin
@MrXionGe
Hi, you can config this :
spring.shardingsphere.datasource.common.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.common.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.common.username=root
spring.shardingsphere.datasource.common.password= 123456
spring.shardingsphere.datasource.ds_0.jdbc-url=jdbc:mysql://192.169.2.29:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
spring.shardingsphere.datasource.ds_1.jdbc-url=jdbc:mysql://192.169.2.29:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
why: if you not config datasource common , in springboot 2.4 May be NullpointExcepiton
you also see #8299
@yu199195
spring.shardingsphere.datasource.names=ds_0,ds_1
spring.shardingsphere.datasource.common.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.common.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.common.username=web_app
spring.shardingsphere.datasource.common.password= 123456
spring.shardingsphere.datasource.ds_0.jdbc-url=jdbc:mysql://127.0.0.1:3306/db_master?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.ds_1.jdbc-url=jdbc:mysql://127.0.0.1:3306/db_read?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.rules.replica-query.load-balancers.round-robin.type=ROUND_ROBIN
spring.shardingsphere.rules.replica-query.data-sources.ds.primary-data-source-name=ds_0
spring.shardingsphere.rules.replica-query.data-sources.ds.replica-data-source-names=ds_1
spring.shardingsphere.rules.replica-query.data-sources.ds.load-balancer-name=round-robin
I tried your solution. It's the same exception.
<?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.4.0</version>
<relativePath/>
</parent>
<groupId>cn.mrxionge</groupId>
<artifactId>replica-query</artifactId>
<version>1.0-SNAPSHOT</version>
<name>replica-query</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!-- spring -->
<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>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<!-- DAO -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- sharding-jdbc -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.0.0-alpha</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring.shardingsphere.datasource.names=ds_0,ds_1
spring.shardingsphere.enabled=true
spring.shardingsphere.datasource.common.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.common.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.common.username=web_app
spring.shardingsphere.datasource.common.password= 123456
spring.shardingsphere.datasource.ds_0.jdbc-url=jdbc:mysql://127.0.0.1:3306/db_master?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.ds_1.jdbc-url=jdbc:mysql://127.0.0.1:3306/db_read?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.rules.replica-query.load-balancers.round-robin.type=ROUND_ROBIN
spring.shardingsphere.rules.replica-query.data-sources.ds.primary-data-source-name=ds_0
spring.shardingsphere.rules.replica-query.data-sources.ds.replica-data-source-names=ds_1
spring.shardingsphere.rules.replica-query.data-sources.ds.load-balancer-name=round-robin
package cn.mrxionge.replicaquery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ReplicaQueryApplication {
public static void main(String[] args) {
SpringApplication.run(ReplicaQueryApplication.class, args);
}
}
@MrXionGe hi, you also config this:
spring.shardingsphere.rules.replica-query.load-balancers.round-robin.type=ROUND_ROBIN
spring.shardingsphere.rules.replica-query.load-balancers.round-robin.props.workid=123
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.primary-data-source-name=primary_ds
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.replica-data-source-names=replica_ds_0,replica_ds_1
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.load-balancer-name=round-robin
@yu199195 Thanks a lot!
spring.shardingsphere.datasource.names=ds_0,ds_1
spring.shardingsphere.datasource.ds_0.jdbc-url=jdbc:mysql://127.0.0.1:3306/db_master?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.ds_0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds_0.username=web_app
spring.shardingsphere.datasource.ds_0.password= 123456
spring.shardingsphere.datasource.ds_1.jdbc-url=jdbc:mysql://127.0.0.1:3306/db_read?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.ds_1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds_1.username=web_app
spring.shardingsphere.datasource.ds_1.password= 123456
spring.shardingsphere.rules.replica-query.load-balancers.round-robin.type=ROUND_ROBIN
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.primary-data-source-name=ds_0
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.replica-data-source-names=ds_01
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.load-balancer-name=round-robin
spring.shardingsphere.datasource.names=ds_0,ds_1
spring.shardingsphere.datasource.common.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.common.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.common.username=web_app
spring.shardingsphere.datasource.common.password= 123456
spring.shardingsphere.datasource.ds_0.jdbc-url=jdbc:mysql://127.0.0.1:3306/db_master?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.ds_1.jdbc-url=jdbc:mysql://127.0.0.1:3306/db_read?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.rules.replica-query.load-balancers.round-robin.type=ROUND_ROBIN
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.primary-data-source-name=ds_0
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.replica-data-source-names=ds_01
spring.shardingsphere.rules.replica-query.data-sources.pr_ds.load-balancer-name=round-robin
I tried the above two examples and failed. The same 'Exception Message'
<!-- sharding-jdbc -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.0.0-alpha</version>
</dependency>
@MrXionGe because you use springboot 2.X, if not config this, will NullPointEx.
spring.shardingsphere.rules.replica-query.load-balancers.round-robin.props.workid=123
@MrXionGe There are no other questions, and we'll close it~
@yu199195 Thank you so much! And. Can version 5.0.0-alpha be used in a production environment? I hope the documents and examples can be more complete about 5.x.
@MrXionGe documents is right, but examples vesion with springboot be later... we will close this issue!
Version
OpenJDK 11 spring-boot 2.3.4 & 2.4.0 shardingsphere-jdbc-core-spring-boot-starter 5.0.0-alpha
Question
It does not seem to read the configuration correctly(yml or properties). When I start, an exception is thrown. (message = No value bound)
When spring-boot is downgraded to 1.5.X. Run successfully!