数据 分片 请参考这位同学的 :http://www.jfinal.com/share/793?p=2#reply_start
读写分离只要 略微修改下,超简单 config 配置:
/** * 配置插件 */ public void configPlugin(Plugins me) { DruidPlugin druidDb1Plugin = createDb1DruidPlugin(); DruidPlugin druidDb2PluginTest = createDb1DruidPluginTest(); Map<String,DruidPlugin> drudMap=new HashMap<>(); drudMap.put("ds_master",druidDb1Plugin); drudMap.put("ds_slave0",druidDb2PluginTest); //获得自定义的扩展 MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration(); masterSlaveRuleConfig.setName("ds_master_slave"); masterSlaveRuleConfig.setMasterDataSourceName("ds_master"); masterSlaveRuleConfig.setSlaveDataSourceNames(Arrays.asList("ds_slave0")); masterSlaveRuleConfig.setLoadBalanceAlgorithmType(MasterSlaveLoadBalanceAlgorithmType.getDefaultAlgorithmType()); //masterSlaveRuleConfig.setLoadBalanceAlgorithmClassName("com.cp.conf.test"); SlaveDrudPlugin drudPlugin=new SlaveDrudPlugin(masterSlaveRuleConfig,drudMap); me.add(drudPlugin); ActiveRecordPlugin targetARP = new ActiveRecordPlugin("aGroup",drudPlugin); // targetARP.setShowSql(true); targetARP.setDevMode(PropKit.getBoolean("devMode", false)); //配置数据库方言 // targetARP.setDialect(DatabaseUtil.getDialectByDriverName(targetDriverClass)); _MappingKit.mappingdb1(targetARP); me.add(targetARP); }
package com.cp.conf; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import javax.sql.DataSource; import com.jfinal.plugin.IPlugin; import com.jfinal.plugin.activerecord.IDataSourceProvider; import com.jfinal.plugin.druid.DruidPlugin; import io.shardingjdbc.core.api.MasterSlaveDataSourceFactory; import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration; /** * 读写分离扩展类 */ public class SlaveDrudPlugin implements IPlugin, IDataSourceProvider { //读写分离的rule MasterSlaveRuleConfiguration masterSlaveRuleConfiguration; //数据源map Map<String,DruidPlugin> druidPlugins; //原数据库连接源map Map<String,DataSource> dataSourceMap; //最终sharding-jdbc封装后的数据库连接源 DataSource dataSource; public SlaveDrudPlugin(MasterSlaveRuleConfiguration masterSlaveRuleConfiguration, Map<String, DruidPlugin> druidPlugins) { this.masterSlaveRuleConfiguration = masterSlaveRuleConfiguration; this.druidPlugins = druidPlugins; dataSourceMap=new HashMap<String, DataSource>(); } public boolean start() { //遍历数据源 ,将数据源加入sharding jdbc for(Map.Entry<String,DruidPlugin> entry:druidPlugins.entrySet()){ entry.getValue().start(); dataSourceMap.put(entry.getKey(),entry.getValue().getDataSource()); } try { dataSource = MasterSlaveDataSourceFactory.createDataSource(dataSourceMap,masterSlaveRuleConfiguration, new ConcurrentHashMap()); System.out.println(dataSource); } catch (SQLException e) { e.printStackTrace(); } return true; } public boolean stop() { for(Map.Entry<String,DruidPlugin> entry:druidPlugins.entrySet()){ entry.getValue().stop(); dataSourceMap.put(entry.getKey(),entry.getValue().getDataSource()); } return true; } public DataSource getDataSource() { return dataSource; } }
这就实现了 读写分离 !简单吧 @@@@@
我在业务中 有个需求!根据页面 切换数据源(sharding-jdbc 也是添加好了扩展)
masterSlaveRuleConfig.setLoadBalanceAlgorithmClassName("com.cp.conf.test");
一句代码搞定
test 类继承 MasterSlaveLoadBalanceAlgorithm
import java.util.List; import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm; public class test implements MasterSlaveLoadBalanceAlgorithm { @Override public String getDataSource(String name, String masterDataSourceName, List<String> slaveDataSourceNames) { 写业务逻辑 代码。。。。。。 System.out.println(name+" "+masterDataSourceName); return "ds_slave0"; } }
完美!!!!
此场景:如果是 没有java 代码植入 ,我还是推荐使用 proxysql 去使用,主要是项目已经形成了,只能通过sharding-jdbc 去植入规则,不去改以前的sql !
好了 有问题 一起交流!!!