apache / shardingsphere

Distributed SQL transaction & query engine for data sharding, scaling, encryption, and more - on any database.
Apache License 2.0
19.87k stars 6.73k forks source link

IT DOES NOT WORK WITH SPRING-BOOT 2.3.X ? #8306

Closed MrXionGe closed 3 years ago

MrXionGe commented 3 years ago

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)

java.lang.reflect.InvocationTargetException: null
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.apache.shardingsphere.spring.boot.util.PropertyUtil.v2(PropertyUtil.java:111) ~[shardingsphere-jdbc-spring-boot-starter-infra-5.0.0-alpha.jar:5.0.0-alpha]
    at org.apache.shardingsphere.spring.boot.util.PropertyUtil.handle(PropertyUtil.java:75) ~[shardingsphere-jdbc-spring-boot-starter-infra-5.0.0-alpha.jar:5.0.0-alpha]
    at org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry.lambda$registerBean$1(AbstractAlgorithmProvidedBeanRegistry.java:57) ~[shardingsphere-jdbc-spring-boot-starter-infra-5.0.0-alpha.jar:5.0.0-alpha]
    at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
    at org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry.registerBean(AbstractAlgorithmProvidedBeanRegistry.java:55) ~[shardingsphere-jdbc-spring-boot-starter-infra-5.0.0-alpha.jar:5.0.0-alpha]
    at org.apache.shardingsphere.replicaquery.spring.boot.algorithm.ReplicaQueryAlgorithmProvidedBeanRegistry.postProcessBeanDefinitionRegistry(ReplicaQueryAlgorithmProvidedBeanRegistry.java:43) ~[shardingsphere-replica-query-spring-boot-starter-5.0.0-alpha.jar:5.0.0-alpha]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:285) ~[spring-context-5.3.1.jar:5.3.1]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:129) ~[spring-context-5.3.1.jar:5.3.1]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:751) ~[spring-context-5.3.1.jar:5.3.1]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:569) ~[spring-context-5.3.1.jar:5.3.1]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-2.4.0.jar:2.4.0]
    at cn.mrxionge.replicaquery.ReplicaQueryApplication.main(ReplicaQueryApplication.java:10) ~[classes/:na]
Caused by: java.util.NoSuchElementException: No value bound
    at org.springframework.boot.context.properties.bind.BindResult.get(BindResult.java:55) ~[spring-boot-2.4.0.jar:2.4.0]
    ... 21 common frames omitted

When spring-boot is downgraded to 1.5.X. Run successfully!

terrymanu commented 3 years ago

What is your config?

MrXionGe commented 3 years ago

@terrymanu

Config File

application.properties
spring.profiles.active=replica-query
application-replica-query.properties
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 commented 3 years ago

Examples of reference https://github.com/apache/shardingsphere/blob/master/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-replica-query.properties

yu199195 commented 3 years ago

@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

MrXionGe commented 3 years ago

@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.

MrXionGe commented 3 years ago

pom.xml

<?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>

application.properties

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

ReplicaQueryApplication

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);
    }
}
yu199195 commented 3 years ago

@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
MrXionGe commented 3 years ago

@yu199195 Thanks a lot!

Config Example 1

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

Config Example 2

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'

pom.xml

        <!--    sharding-jdbc    -->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
            <version>5.0.0-alpha</version>
        </dependency>
yu199195 commented 3 years ago

@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
yu199195 commented 3 years ago

@MrXionGe There are no other questions, and we'll close it~

MrXionGe commented 3 years ago

@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.

yu199195 commented 3 years ago

@MrXionGe documents is right, but examples vesion with springboot be later... we will close this issue!