You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/10/25 09:38:57 UTC

[GitHub] [shardingsphere] haiy1026 opened a new issue, #21743: 关于在Springboot的web项目中通过DataSourceConfig配置分片规则不生效的问题

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

   关于在Springboot的web项目中通过DataSourceConfig配置分片规则不生效的问题。
   场景环境:
       1. springboot版本:2.3.7.RELEASE
       2. shardingjdbc版本:4.0.0.RC1
       3.MySQL版本:8.0+
       4.mybatis-plus版本:3.3.2
       5. 项目类型:web
   问题描述:
   如果我通过application.yaml来配置分片规则(ComplexKeysShardingAlgorithm),那么我就可以根据分片规则来区分数据表,但是我不在application.yaml里做任何的配置,而是通过config目录下通过DataSourceConfig来配置分片规则,那么我的查询就没经过我自定义的分片规则。
   1. 其中我的application配置文件如下:
   `
   spring:
     main:
       allow-bean-definition-overriding: true
     sharding-sphere:
       datasource:
         db1:
           url: jdbc:mysql://localhost:23306/db1?serverTime=UTC&useUnicode=true&characterEncoding=utf-8
           username: root
           password: root
           driver-class-name: com.mysql.jdbc.Driver
           type: com.alibaba.druid.pool.DruidDataSource
         names: db1
       props:
         sql.show: true
       sharding:
         default-data-source-name: db1
         tables:
           employee:
             dynamic: true
             actualDataNodes: db1.his_employee_${20221018..20221019}
             databaseStrategy:
               none:
             tableStrategy:
               complex:
                 sharding-columns: biz_date
                 algorithm-class-name: com.lbc.split.config.ComplexShardingAlgo
   `
   然后我的分片规则:
   `
   @Component
   public class ComplexShardingAlgo implements ComplexKeysShardingAlgorithm<String> {
       /**
        *
        * @param collection 加载配置文件时的解析规则,逻辑表规则
        * @param complexKeysShardingValue SQL中对应的Value
        * @return
        */
       @Override
       public Collection<String> doSharding(Collection<String> collection, ComplexKeysShardingValue<String> complexKeysShardingValue) {
   
           log.info("获取集合{} 和复合分片的值序列:{}",collection,complexKeysShardingValue);
       }
   }
   `
   我通过查
   以上是没有任何问题的
   但是如果我注释applicaton.yaml里的话,那么我设定的分片规则就没执行到(可以初始化,但是我debug的时候没有经过分片规则)
   做法和代码如下:
   `
   @Configuration
   public class DataSourceConfig {
   
       public Map<String, DataSource> createDataSourceMap() {
           DruidDataSource dataSource1 = new DruidDataSource();
           dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
           dataSource1.setUrl("jdbc:mysql://localhost:23306/db1?serverTime=UTC&useUnicode=true&characterEncoding=utf-8");
           dataSource1.setUsername("root");
           dataSource1.setPassword("root");
           Map<String, DataSource> result = new HashMap<>(5);
           result.put("db1", dataSource1);
           return result;
       }
   
       TableRuleConfiguration getTableRuleConfiguration() {
           TableRuleConfiguration result = new TableRuleConfiguration("employee","db1.his_employee_$->{20221018..20221020}");
           // 通过注入的形式也不执行到
           result.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("biz_date",new ComplexShardingAlgo()));
           return result;
       }
   
       @Bean
       DataSource shardingDataSource() throws SQLException {
           ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
           shardingRuleConfig.getTableRuleConfigs().add(getTableRuleConfiguration());
   
           Properties properties = new Properties();
          // 这里生效了,但是分片规则不生效,debug的时候,也没有进入到ComplexShardingAlgo类里。
           properties.setProperty("sql.show","true");
           return ShardingDataSourceFactory.createDataSource(createDataSourceMap(),shardingRuleConfig,properties);
       }
   `
   请问是我在DataSourceConfig里配置不正确的原因导致吗?


-- 
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: notifications-unsubscribe@shardingsphere.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] RaigorJiang closed issue #21743: 关于在Springboot的web项目中通过DataSourceConfig配置分片规则不生效的问题

Posted by GitBox <gi...@apache.org>.
RaigorJiang closed issue #21743: 关于在Springboot的web项目中通过DataSourceConfig配置分片规则不生效的问题
URL: https://github.com/apache/shardingsphere/issues/21743


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] RaigorJiang commented on issue #21743: 关于在Springboot的web项目中通过DataSourceConfig配置分片规则不生效的问题

Posted by GitBox <gi...@apache.org>.
RaigorJiang commented on issue #21743:
URL: https://github.com/apache/shardingsphere/issues/21743#issuecomment-1291281428

   Hi @haiy1026 
   Welcome to the Apache ShardingSphere community, but only **English** is accepted here.
   I will close this issue, please submit a new one.


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org