sharding jdbc读写分离配置和Demo

sharding jdbc读写分离配置和Demo

项目中使用的是sharding jdbc1.5.4快照版( 1.5.3有严重bug,1.5.4快照版已经修复,但还没有发布,是自己打的包)。连接池是HikariCP,具体项目和使用的其他依赖,下面有Demo下载地址,可以下载跑跑。

此处只贴出来主要配置和配置文件,其他都是正常项目中的,不需要改动,更多见demo.

sharding jdbc配置类

@Configuration
public class XbDataSource {
/**
*
主数据源 用来写入数据
*
* @return
*/
@Bean
@ConfigurationProperties(“spring.datasource.master”)
public DataSource masterDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
/**
*
从数据源01 用来读取数据
*
* @return
*/
@Bean
@ConfigurationProperties(“spring.datasource.slave1”)
public DataSource slaveDataSource_1() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
/**
*
从数据源02 用来读取数据
*
* @return
*/
@Bean
@ConfigurationProperties(“spring.datasource.slave2”)
public DataSource slaveDataSource_2() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
@Bean
@Primary //该注解表示在同一个接口有多个实现类可以注入的时候,默认选择哪一个,而不是让@autowire注解报错
@DependsOn({“masterDataSource”, “slaveDataSource_1”, “slaveDataSource_2”})
public DataSource dataSource() throws SQLException {
//引入从数据源
HashMap<String, DataSource> map = new HashMap<>();
map.put(“slave1”, this.slaveDataSource_1());
map.put(“slave2”, this.slaveDataSource_2());
//引入主数据源配置,其中MasterSlaveLoadBalanceStrategyType.RANDOM 是负载均衡规则
DataSource masterSlaveDataSource = MasterSlaveDataSourceFactory.createDataSource(“dataSource”,
“master”, this.masterDataSource(), map, MasterSlaveLoadBalanceStrategyType.RANDOM);HashMap<String, DataSource> masterSlaveMap = new HashMap<>();
masterSlaveMap.put(“masterSlaveDataSource”, masterSlaveDataSource);
//数据源配置  构造DataSourceRule对象,它是来描述数据源的分布规则的。
DataSourceRule dataSourceRule = new DataSourceRule(masterSlaveMap);

List<TableRule> tableRuleList = new ArrayList<>();
//添加分表规则,此处只添加一个,实际可能多个
tableRuleList.add(new TableRule.TableRuleBuilder(“t_order_0”)
.actualTables(Lists.newArrayList(“t_order_0”))
.dataSourceRule(dataSourceRule).build());

ShardingRule shardingRule = ShardingRule.builder()
.dataSourceRule(dataSourceRule)
.tableRules(tableRuleList)
.build();
//创建配置文件
Properties properties = new Properties();
properties.setProperty(“sql.show”, “true”);//设置打印sql
return ShardingDataSourceFactory.createDataSource(shardingRule, properties);
}
}

配置文件部分:

#主数据源配置
spring.datasource.master.jdbcUrl=jdbc:mysql://localhost:3306/ds_master?useUnicode=true&characterEncoding=UTF-8
spring.datasource.master.username=root
spring.datasource.master.password=root
spring.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.master.connection-timeout=5000
spring.datasource.master.idle-timeout=120000
spring.datasource.master.max-lifetime=300000
spring.datasource.master.minimum-idle=2
spring.datasource.master.maximum-pool-size=100
#从数据源01 配置
spring.datasource.slave1.jdbcUrl=jdbc:mysql://localhost:3306/ds_slave_0?useUnicode=true&characterEncoding=UTF-8
spring.datasource.slave1.username=root
spring.datasource.slave1.password=root
spring.datasource.slave1.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.slave1.connection-timeout=5000
spring.datasource.slave1.idle-timeout=120000
spring.datasource.slave1.max-lifetime=300000
spring.datasource.slave1.minimum-idle=2
spring.datasource.slave1.maximum-pool-size=100
#从数据源02 配置
spring.datasource.slave2.jdbcUrl=jdbc:mysql://localhost:3306/ds_slave_1?useUnicode=true&characterEncoding=UTF-8
spring.datasource.slave2.username=root
spring.datasource.slave2.password=root
spring.datasource.slave2.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.slave2.connection-timeout=5000
spring.datasource.slave2.idle-timeout=120000
spring.datasource.slave2.max-lifetime=300000
spring.datasource.slave2.minimum-idle=2
spring.datasource.slave2.maximum-pool-size=100

Demo: ShardingTest-master

本站的文章多是老王开发工作中问题的记录,一个字一个字敲的,切实可行,可以分享,需要留个原文链接,至少也意思意思吧!
vsalw技术博客 » sharding jdbc读写分离配置和Demo

你想下载这个主题吗?

沟通一下 扯会蛋