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/30 07:33:56 UTC

[GitHub] [shardingsphere] haiy1026 opened a new issue, #21849: Question:ShardingSphereAlgorithm sharding strategy configuration is not executed

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

   Question:ShardingSphereAlgorithm sharding strategy configuration is not executed and different between .yaml configuration and DataSourceConfiguration
   
   In My Springboot project.
   springboot-version:2.3.7,
   mysql:8.0
   shardingsphere-jdbc-core:5.1.1
   
   my pom as fellow:
         ```
    <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>
           </dependency>
   
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-test</artifactId>
               <scope>test</scope>
           </dependency>
   
           <dependency>
               <groupId>mysql</groupId>
               <artifactId>mysql-connector-java</artifactId>
               <scope>runtime</scope>
           </dependency>
           <dependency>
               <groupId>org.projectlombok</groupId>
               <artifactId>lombok</artifactId>
               <optional>true</optional>
           </dependency>
   
           <dependency>
               <groupId>com.baomidou</groupId>
               <artifactId>mybatis-plus-boot-starter</artifactId>
               <version>3.3.2</version>
           </dependency>
   
           <dependency>
               <groupId>org.apache.shardingsphere</groupId>
               <artifactId>shardingsphere-jdbc-core</artifactId>
               <version>${shardingsphere.version}</version>
           </dependency>
           <dependency>
               <groupId>org.apache.shardingsphere</groupId>
               <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
               <version>${shardingsphere.version}</version>
           </dependency>
   
           <dependency>
               <groupId>com.alibaba</groupId>
               <artifactId>druid</artifactId>
               <version>1.2.14</version>
           </dependency>
   
   ```
   Started by .yaml setting
   
   ```
   spring:
       shardingsphere:
           mode:
               type: Memory
           datasource:
               names: db1
               db1:
                   driverClassName: com.mysql.jdbc.Driver
                   url: jdbc:mysql://localhost:23306/db1?allowPublicKeyRetrieval=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
                   username: root
                   password: root
                   type: com.alibaba.druid.pool.DruidDataSource
           rules:
               sharding:
                   tables:
                       counter_assetdebts:
                           actualDataNodes: db1.his_employee_${20221018..20221018}
                           tableStrategy:
                               complex:
                                   shardingColumns: biz_date
                                   shardingAlgorithmName: his-inline
                   shardingAlgorithms:
                       his-inline:
                           type: CLASS_BASED
                           props:
                               strategy: COMPLEX
                               algorithmClassName: com.xx.data.config.MyComplexShardingAlgo
           props:
               sql-show: true
       datasource:
           url: jdbc:mysql://localhost:23306/db1?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
           username: root
           password: root
   ```
   and my ComplexShardingAlgo as fellow:
   ```
   @Component
   public class MyComplexShardingAlgo implements ComplexKeysShardingAlgorithm {
   
       private static final String SHARDING_COL = "biz_date";
       private static final String HIS = "his";
   
       @Override
       public Collection<String> doSharding(Collection collection, ComplexKeysShardingValue complexKeysShardingValue) {
           /* this sharding rule and insert into newCollection*/
           return newCollection;
       }
   
       @Override
       public String getType() {
           return null;
       }
   
       @Override
       public void init() {
   
       }
   
   ```
   In this case, my sharding strategies normal(MyComplexShardingAlgo.doShare() (return)a newCollection).
   However, when I don't use the yaml file format(deleted application.yaml) and started as fellows:
   ```
   @Configuration
   public class DataSourceConfig {
       @Bean
       @Primary
       public DataSource dataSource() throws SQLException {
           Map<String,DataSource> dataSourceMap=new HashMap<>(2);
   
           DruidDataSource dataSource = new DruidDataSource();
           dataSource.setDriverClassName("com.mysql.jdbc.Driver");
           dataSource.setUrl("jdbc:mysql://localhost:23306/db1?allowPublicKeyRetrieval=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8");
           dataSource.setUsername("root");
           dataSource.setPassword("root");
           dataSourceMap.put("db1", dataSource);
           Properties prop=new Properties();
           prop.setProperty("sql-show","true");
           return ShardingSphereDataSourceFactory.createDataSource("db1",dataSourceMap, Collections.singleton(createShardingRuleConfiguration()), prop);
   
       }
   
       private ShardingRuleConfiguration createShardingRuleConfiguration() {
           ShardingRuleConfiguration result = new ShardingRuleConfiguration();
           result.getTables().add(getTableRuleConfiguration());
           result.getBindingTableGroups().add("employee");
           result.setDefaultTableShardingStrategy(new ComplexShardingStrategyConfiguration("biz_date", "hisAlgo"));
   
           Properties propsComplex = new Properties();
           propsComplex.setProperty("strategy", "COMPLEX");
           propsComplex.setProperty("algorithmClassName","com.xx.datasync.config.MyComplexShardingAlgo");
           result.getShardingAlgorithms().put("hisAlgo", new ShardingSphereAlgorithmConfiguration("CLASS_BASED", propsComplex));
   
           return result;
       }
   
       private ShardingTableRuleConfiguration getTableRuleConfiguration() {
           ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("employee", "db1.his_employee_$->{20221018..20221019}");
           result.setTableShardingStrategy(new ComplexShardingStrategyConfiguration("biz_date","hisAlgo"));
           return result;
       }
   }
   
   ```
   
   In this case, the project initialized without any problems, however, the method of  MyComplexShardingAlgo.doShare() not executed.
   
   =====================================================================
   
   **Started by .yaml MyComplexShardingAlgo.doShare() will be worked, started by DataSourceConfigution MyComplexShardingAlgo.doShare() not worked.**
   
   


-- 
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] haiy1026 commented on issue #21849: Question:ShardingSphereAlgorithm sharding strategy configuration is not executed

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

   In DataSourceConfig.class, i forget add sqlsession
   The complete code is as follows
   ```
   @Configuration
   @MapperScan(basePackages = "com.xx.datasync.mapper",sqlSessionFactoryRef ="sqlSessionFactoryForShardingJdbc")
   public class DataSourceConfig {
       @Bean("shardingDataSource")
       @Primary
       public DataSource dataSource() throws SQLException {
           Map<String,DataSource> dataSourceMap=new HashMap<>(2);
   
           DruidDataSource dataSource = new DruidDataSource();
           dataSource.setDriverClassName("com.mysql.jdbc.Driver");
           dataSource.setUrl("jdbc:mysql://localhost:23306/db1?allowPublicKeyRetrieval=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8");
           dataSource.setUsername("root");
           dataSource.setPassword("root");
           dataSourceMap.put("db1", dataSource);
           Properties prop=new Properties();
           prop.setProperty("sql-show","true");
           return ShardingSphereDataSourceFactory.createDataSource("db1",dataSourceMap, Collections.singleton(createShardingRuleConfiguration()), prop);
   
       }
   
       private ShardingRuleConfiguration createShardingRuleConfiguration() {
           ShardingRuleConfiguration result = new ShardingRuleConfiguration();
           result.getTables().add(getTableRuleConfiguration());
           result.getBindingTableGroups().add("employee");
           result.setDefaultTableShardingStrategy(new ComplexShardingStrategyConfiguration("biz_date", "hisAlgo"));
           Properties propsComplex = new Properties();
           propsComplex.setProperty("strategy", "COMPLEX");
           propsComplex.setProperty("algorithmClassName","com.xx.datasync.config.MyComplexShardingAlgo");
           result.getShardingAlgorithms().put("hisAlgo", new ShardingSphereAlgorithmConfiguration("CLASS_BASED", propsComplex));
   
           return result;
       }
   
       private ShardingTableRuleConfiguration getTableRuleConfiguration() {
           ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("employee", "db1.his_employee_$->{20221018..20221019}");
           result.setTableShardingStrategy(new ComplexShardingStrategyConfiguration("biz_date","hisAlgo"));
           return result;
       }
   
       @Bean("sqlSessionFactoryForShardingJdbc")
       public SqlSessionFactory sqlSessionFactoryForShardingJdbc(@Qualifier("shardingDataSource") DataSource dataSource) throws Exception {
   
           MybatisConfiguration configuration = new MybatisConfiguration();
           configuration.setMapUnderscoreToCamelCase(true);
           configuration.setCacheEnabled(false);
           configuration.setCallSettersOnNulls(true);
           configuration.setJdbcTypeForNull(JdbcType.NULL);
           configuration.addInterceptor(new PaginationInterceptor());
   
           MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
           sqlSessionFactoryBean.setConfiguration(configuration);
           sqlSessionFactoryBean.setDataSource(dataSource);
   
           return sqlSessionFactoryBean.getObject();
       }
   }
   ```
   


-- 
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] haiy1026 closed issue #21849: Question:ShardingSphereAlgorithm sharding strategy configuration is not executed

Posted by GitBox <gi...@apache.org>.
haiy1026 closed issue #21849: Question:ShardingSphereAlgorithm sharding strategy configuration is not executed
URL: https://github.com/apache/shardingsphere/issues/21849


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