tuwh commented on issue #28748:
URL: 
https://github.com/apache/shardingsphere/issues/28748#issuecomment-1769766580

   > @tuwh Please show your configuration.
   
   I configured it using java code. I did a packaging when the original 
datasource initialization of spring's beanPostProcessor was completed. A 
read-write separated database packaged as sharding. Sample code basic reference 
value official document. this is my code:
   
   @Component
   @ConditionalOnProperty(JDBC_SLAVE_NAMES)
   public class ShardingDataSourceProcessor implements BeanPostProcessor {
   
       public static final String DATA_SOURCE_NAME = "dataSource";
   
       @Nullable
       public Object postProcessAfterInitialization(Object bean, String 
beanName) throws BeansException {
           if (DATA_SOURCE_NAME.equals(beanName)){
               BempDataSourceUtils.setMasterDataSource((DataSource) bean);
               return warpMasterSlaveDataSource();
           }
           return bean;
       }
   
   
       /**
        * warp the sharding datasource
        * @return
        */
       public DataSource warpMasterSlaveDataSource() {
           // init slave datasource
           BempDataSourceUtils.initSlaveDataSources();
           List<String> slaveDataSources = BempDataSourceUtils.getSlaveNames();
           ReadwriteSplittingDataSourceRuleConfiguration dataSourceConfig = new 
ReadwriteSplittingDataSourceRuleConfiguration(
                   "ds_master_slave", "ds_master", slaveDataSources, 
"weight_lb");
           Properties algorithmProps = new Properties();
           for (String slaveDataSource : slaveDataSources) {
               algorithmProps.put(slaveDataSource, "1");
           }
           Map<String, AlgorithmConfiguration> algorithmConfigMap = new 
HashMap<>(1);
           algorithmConfigMap.put("weight_lb", new 
AlgorithmConfiguration("WEIGHT", algorithmProps));
           ReadwriteSplittingRuleConfiguration ruleConfig = new 
ReadwriteSplittingRuleConfiguration(Collections.singleton(dataSourceConfig), 
algorithmConfigMap);
           Properties props = new Properties();
           props.setProperty("sql-show", Boolean.TRUE.toString());
   
           // 数据源map
           Map<String, DataSource> dataSourceMap = new HashMap<>();
           dataSourceMap.put("ds_master", 
BempDataSourceUtils.getMasterDataSource());
           dataSourceMap.putAll(BempDataSourceUtils.getSlaveDataSourceMaps());
   
           ModeConfiguration modeConfiguration = new 
ModeConfiguration("Memory", null);
           try {
               return 
ShardingSphereDataSourceFactory.createDataSource("shadingDb", 
modeConfiguration, dataSourceMap, Collections.singleton(ruleConfig), props);
           } catch (SQLException e) {
               throw new BempRuntimeException(CommonErrorNoConst.FAILURE, 
"multi datasource inject failed!");
           }
       }
   
   }


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