bitbxj commented on issue #24786:
URL: 
https://github.com/apache/shardingsphere/issues/24786#issuecomment-1483817676

       @Bean("dynamicDataSource")
       public DataSource dynamicDataSource(@Qualifier("dataSourceMaster") 
DataSource dataSourceMaster,
                                           @Qualifier("dataSourceSlave") 
DataSource dataSourceSlave) {
           DataSource dataSource = null;
           try {
               dataSource = buildDataSource( dataSourceMaster, dataSourceSlave 
);
           } catch (Exception e) {
               e.printStackTrace();
           }
           return dataSource;
   
       }
   
   
       private DataSource buildDataSource(DataSource dataSourceMaster, 
DataSource dataSourceSlave) throws SQLException {
           ShardingRuleConfiguration shardingRuleConfig = 
getShardingRuleConfiguration();
   
           shardingRuleConfig.setDefaultDataSourceName( "dataSource_01" );
           Properties properties = new Properties();
           properties.put( "sql.show", isShow );
        //MasterSlave config
           MasterSlaveRuleConfiguration masterSlaveRuleConfiguration = new 
MasterSlaveRuleConfiguration( "dynamicDataSource", "dataSource_01", 
Arrays.asList( "dataSource_02" ) );
           shardingRuleConfig.setMasterSlaveRuleConfigs( Arrays.asList( 
masterSlaveRuleConfiguration ) );
   
           return ShardingDataSourceFactory.createDataSource( getDataSourceMap( 
dataSourceMaster, dataSourceSlave ), masterSlaveRuleConfiguration, properties );
       }
       // ShardingRule
       private ShardingRuleConfiguration getShardingRuleConfiguration() {
           ShardingRuleConfiguration shardingRuleConfig = new 
ShardingRuleConfiguration();
           shardingRuleConfig.getTableRuleConfigs().add( 
getObjectAttributeTableRuleConfiguration() );
           shardingRuleConfig.getTableRuleConfigs().add( 
getObjectVersionTableRuleConfiguration() );
           return shardingRuleConfig;
       }
       //get config properties,inital TableRuleConfiguration
       
@Value("${spring.shardingsphere.sharding.tables.object_attribute.actualDataNodes}")
       private String objectAttributeDataNodes;
       
@Value("${spring.shardingsphere.sharding.tables.object_version.actualDataNodes}")
       private String objectVersionDataNodes;
   
       private String objectAttributeTable = "object_attribute";
       private String objectVersionTable = "object_version";
   
       
@Value("${spring.shardingsphere.sharding.tables.object_attribute.tableStrategy.standard.shardingColumn}")
       private String objectAttributeShardingColumn;
       
@Value("${spring.shardingsphere.sharding.tables.object_version.tableStrategy.standard.shardingColumn}")
       private String objectVersionShardingColumn;
   
       @Value("${spring.shardingsphere.props.sql.show}")
       private boolean isShow;
   
   
   
   
       private TableRuleConfiguration 
getObjectAttributeTableRuleConfiguration() {
           TableRuleConfiguration orderDetailTableRuleConfig = new 
TableRuleConfiguration( objectAttributeTable, objectAttributeDataNodes );
           orderDetailTableRuleConfig.setTableShardingStrategyConfig( new 
StandardShardingStrategyConfiguration( objectAttributeShardingColumn, new 
DColumnTableShardingAlgorithm() ) );
           return orderDetailTableRuleConfig;
       }
   
       private TableRuleConfiguration getObjectVersionTableRuleConfiguration() {
           TableRuleConfiguration objectVersionRuleConfig = new 
TableRuleConfiguration( objectVersionTable, objectVersionDataNodes );
           objectVersionRuleConfig.setTableShardingStrategyConfig( new 
StandardShardingStrategyConfiguration( objectVersionShardingColumn, new 
ColumnTableShardingAlgorithm() ) );
           return objectVersionRuleConfig;
       }
       // ShardingMasterSlave DataSource
       private Map<String, DataSource> getDataSourceMap(DataSource 
dataSourceMaster,
                                                        DataSource 
dataSourceSlave) {
           Map<String, DataSource> dataSourceMap = new HashMap<>( 2 );
          
   
           dataSourceMap.put( "dataSource_01", dataSourceMaster );
           dataSourceMap.put( "dataSource_02", dataSourceSlave );
           return dataSourceMap;
       }
   
       //Druid DataSurce
        @Bean("dataSourceMaster")
   
       public DataSource dataSourceMaster(@Qualifier("customDruidProperties") 
CustomDruidProperties customDruidProperties) {
           DataSource druidDataSource = customDruidProperties.initDataSource( 
new DruidDataSource() );
           return druidDataSource;
   
       }
   
       @Bean("dataSourceSlave")
   
       public DataSource dataSourceSlave (@Qualifier("slaveDruidProperties") 
SlaveDruidProperties slaveDruidProperties) {
           DataSource druidDataSource = slaveDruidProperties.initDataSource( 
new DruidDataSource() );
           return druidDataSource;
   
       }
   
   
       //yml config 
   spring:
     datasource:
       master:
         type: com.alibaba.druid.pool.DruidDataSource
         driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
         url: jdbc:sqlserver://localhost:1433;DatabaseName=20220522
         username: xxx
         password: xxx
         filters: wall,mergeStat
       slave:
         type: com.alibaba.druid.pool.DruidDataSource
         driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
         url: jdbc:sqlserver://localhost:1433;DatabaseName=20220522slave
         username: xxx
         password: xxx
       druid:
        
         initialSize: 20
    
         minIdle: 50
         ...
   
     shardingsphere:
       sharding:
         tables:
           object_attribute:
             actualDataNodes: dynamicDataSource.object_attribute_$->{0..1}  
             tableStrategy: 
               standard:
                 shardingColumn: version_id
           object_version:
             actualDataNodes: dynamicDataSource.object_version_$->{0..1} 
             tableStrategy: 
               standard:
                 shardingColumn: version_id
       props:
         sql:
          show: true


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