springboot2.0中用sharding-jdbc实现读写分离,集成Druid

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/vipbupafeng/article/details/80256958

1、文档

官方链接:http://shardingjdbc.io/docs_en/02-guide/configuration/

2、依赖

<dependency>
    <groupId>io.shardingjdbc</groupId>
    <artifactId>sharding-jdbc-core</artifactId>
    <version>2.0.3</version>
</dependency>

3、配置注入

由于官网的sharding-jdbc-core-spring-boot-starter依赖是基于springboot1.5.+,其中使用了RelaxedPropertyResolver类来解析yml文件,而2.0中移除了该类(2.0中使用Binder类来读取yml文件),所以这里自己读取yml

yml文件映射类:使用DruidDataSource数据源,提供监控

@Data
@ConfigurationProperties(prefix = "sharding.jdbc")
public class ShardingMastSlaveConfig {

    private Map<String, DruidDataSource> dataSources = new HashMap<>();

    private MasterSlaveRuleConfiguration masterSlaveRule;
}

yml文件:

sharding.jdbc:
    data-sources:      # 注意命名格式
      ds_master:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
        username: root
        password: root
      ds_slave_0:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test_slave?useUnicode=true&characterEncoding=utf-8&useSSL=true
        username: root
        password: root
    master-slave-rule:
      name: ds_ms
      master-data-source-name: ds_master
      slave-data-source-names: ds_slave_0
      load-balance-algorithm-type: round_robin

注入MasterSlaveDataSource:

@Slf4j
@Configuration
@EnableConfigurationProperties(ShardingMastSlaveConfig.class)
@ConditionalOnProperty({"sharding.jdbc.data-sources.ds_master.url", "sharding.jdbc.master-slave-rule.master-data-source-name"})
public class ShardingDataSourceConfig {

    @Autowired
    private ShardingMastSlaveConfig shardingMastSlaveConfig;

    @Bean("masterSlaveDataSource")
    public DataSource masterSlaveDataSource() throws SQLException {
        shardingMastSlaveConfig.getDataSources().forEach((k, v) -> configDataSource(v));
        Map<String, DataSource> dataSourceMap = Maps.newHashMap();
        dataSourceMap.putAll(shardingMastSlaveConfig.getDataSources());
        DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, shardingMastSlaveConfig.getMasterSlaveRule(), Maps.newHashMap());
        log.info("masterSlaveDataSource config complete");
        return dataSource;
    }

    private void configDataSource(DruidDataSource druidDataSource) {
        druidDataSource.setMaxActive(20);
        druidDataSource.setInitialSize(1);
        druidDataSource.setMaxWait(60000);
        druidDataSource.setMinIdle(1);
        druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
        druidDataSource.setMinEvictableIdleTimeMillis(300000);
        druidDataSource.setValidationQuery("select 'x'");
        druidDataSource.setTestWhileIdle(true);
        druidDataSource.setTestOnBorrow(false);
        druidDataSource.setTestOnReturn(false);
        druidDataSource.setPoolPreparedStatements(true);
        druidDataSource.setMaxOpenPreparedStatements(20);
        druidDataSource.setUseGlobalDataSourceStat(true);
        try {
            druidDataSource.setFilters("stat,wall,slf4j");
        } catch (SQLException e) {
            log.error("druid configuration initialization filter", e);
        }
    }
}

4、创建数据库

创建两个库test和test_slave,两个库中任意建两个相同的表,测试查询和插入即可。

持久层集成了mybatis-plus查询数据库,其中的分页插件配置需要注意:一定要指定方言DialectType,否则分页查询会只到master中去查询,而正常查询操作应该到slave中查询。

参考:https://gitee.com/hanjunchen/sharding-jdbc-demo.git

mysql主从同步参考:https://blog.csdn.net/vipbupafeng/article/details/80272946

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭