MrXionGe opened a new issue, #21650:
URL: https://github.com/apache/shardingsphere/issues/21650

   ## Bug Report
   
   ### Which version of ShardingSphere did you use?
   5.2.0
   
   ### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
   shardingsphere-jdbc-core
   
   ### Code
   
   ##### pom.xml(dependencies)
   
   ```xml
           <dependency>
               <groupId>mysql</groupId>
               <artifactId>mysql-connector-java</artifactId>
               <version>8.0.30</version>
           </dependency>
           <dependency>
               <groupId>com.zaxxer</groupId>
               <artifactId>HikariCP</artifactId>
               <version>5.0.1</version>
           </dependency>
           <dependency>
               <groupId>org.apache.shardingsphere</groupId>
               <artifactId>shardingsphere-jdbc-core</artifactId>
               <version>5.2.0</version>
           </dependency>
   ```
   
   ##### JavaConfig
   
   ```java
   package cn.mrxionge.webtemplatemysql.config;
   
   import com.zaxxer.hikari.HikariDataSource;
   import lombok.extern.slf4j.Slf4j;
   import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
   import 
org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
   import 
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
   import 
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
   import 
org.apache.shardingsphere.readwritesplitting.api.strategy.StaticReadwriteSplittingStrategyConfiguration;
   import org.springframework.beans.factory.annotation.Value;
   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.Configuration;
   
   import javax.sql.DataSource;
   import java.sql.SQLException;
   import java.util.HashMap;
   import java.util.List;
   import java.util.Map;
   import java.util.Properties;
   
   /**
    * MySQL读写分离数据源
    */
   @Slf4j
   @Configuration
   public class MySQLConfig {
   
       @Value("${mysql-ds.master.jdbc-url}")
       private String masterJdbcUrl;
   
       @Value("${mysql-ds.master.username}")
       private String masterUsername;
   
       @Value("${mysql-ds.master.password}")
       private String masterPassword;
   
       @Value("${mysql-ds.replica-1.jdbc-url}")
       private String replica1JdbcUrl;
   
       @Value("${mysql-ds.replica-1.username}")
       private String replica1Username;
   
       @Value("${mysql-ds.replica-1.password}")
       private String replica1Password;
   
       @Value("${mysql-ds.replica-2.jdbc-url}")
       private String replica2JdbcUrl;
   
       @Value("${mysql-ds.replica-2.username}")
       private String replica2Username;
   
       @Value("${mysql-ds.replica-2.password}")
       private String replica2Password;
   
       @Bean
       public DataSource dataSource() throws SQLException {
           //数据源Map
           Map<String, DataSource> dsMap = new HashMap<>();
           //配置主库
           HikariDataSource masterDs = new HikariDataSource();
           masterDs.setDriverClassName("com.mysql.cj.jdbc.Driver");
           masterDs.setJdbcUrl(masterJdbcUrl);
           masterDs.setMinimumIdle(16);
           masterDs.setMaximumPoolSize(32);
           masterDs.setConnectionInitSql("SET NAMES utf8mb4");
           masterDs.setUsername(masterUsername);
           masterDs.setPassword(masterPassword);
           dsMap.put("master_ds", masterDs);
           //配置读库1
           HikariDataSource replicaDs1 = new HikariDataSource();
           replicaDs1.setDriverClassName("com.mysql.cj.jdbc.Driver");
           replicaDs1.setJdbcUrl(replica1JdbcUrl);
           replicaDs1.setMinimumIdle(16);
           replicaDs1.setMaximumPoolSize(32);
           replicaDs1.setConnectionInitSql("SET NAMES utf8mb4");
           replicaDs1.setUsername(replica1Username);
           replicaDs1.setPassword(replica1Password);
           dsMap.put("replica_ds_1", replicaDs1);
           //配置读库2
           HikariDataSource replicaDs2 = new HikariDataSource();
           replicaDs2.setDriverClassName("com.mysql.cj.jdbc.Driver");
           replicaDs2.setJdbcUrl(replica2JdbcUrl);
           replicaDs2.setMinimumIdle(16);
           replicaDs2.setMaximumPoolSize(32);
           replicaDs2.setConnectionInitSql("SET NAMES utf8mb4");
           replicaDs2.setUsername(replica2Username);
           replicaDs2.setPassword(replica2Password);
           dsMap.put("replica_ds_2", replicaDs2);
           //主从数据源配置
           ReadwriteSplittingDataSourceRuleConfiguration dsConfig = new 
ReadwriteSplittingDataSourceRuleConfiguration("ds",
                   new 
StaticReadwriteSplittingStrategyConfiguration("master_ds", 
List.of("replica_ds_1", "replica_ds_2")),
                   null, "load_balancer");
           //负载均衡算法配置
           Map<String, AlgorithmConfiguration> loadBalanceMap = new HashMap<>();
           loadBalanceMap.put("load_balancer", new 
AlgorithmConfiguration("ROUND_ROBIN", new Properties()));
           ReadwriteSplittingRuleConfiguration ruleConfiguration = new 
ReadwriteSplittingRuleConfiguration(List.of(dsConfig), loadBalanceMap);
           //创建DS
           log.info("创建 ShardingSphere 读写分离数据源");
           return ShardingSphereDataSourceFactory.createDataSource(dsMap, 
List.of(ruleConfiguration), new Properties());
       }
   }
   
   ```
   
   ### Description
   I created three data sources. A write data source(master_ds) and two read 
data sources(replica_ds_1&replica_ds_2).
   
   ### Expected behavior
   Initialize the three data sources.
   I will see the log for 'hikari' in the console with 3 data sources created.
   
   ### Actual behavior
   There are four data sources created.
   
![image](https://user-images.githubusercontent.com/23261935/196748994-6df357b3-6be4-4709-a92e-207f039bdb58.png)
   
   
   ### Reason analyze (If you can)
   I'm not sure what the mistake was.
   This is correct in version 5.1.2. However, this is incorrect in version 
5.2.0.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: 
[email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to