springboot+shardingsphere+druid实现数据库读写分离

1.引入依赖

 <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.1</version>
  </dependency>
  <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.4</version>
   </dependency>

2.yml文件配置

spring:
  shardingsphere:
    enabled: true # 是否启用sharding,不启用时使用datasource配置的数据源
    datasource:
      names: master,slave0 # 节点名称,多个时使用逗号隔开
      master:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name:  com.mysql.jdbc.Driver
        url: jdbc:mysql://172.16.200.106:3306/password_admin?useUnicode=true&characterEncoding=utf-8
        username: root
        password: root@123
        # 以下为druid配置,可以共用datasource中的druid配置,需要覆盖时再重新配置
        filters: stat,wall,logback
        initial-size: 2
        max-active: 45
        min-idle: 6
      slave0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://172.16.200.107:3306/password_admin?useUnicode=true&characterEncoding=utf-8
        username: root
        password: 123456
        # 以下为druid配置,可以共用datasource中的druid配置,需要覆盖时再重新配置
        filters: stat,wall,logback
        initial-size: 3
        max-active: 20
        min-idle: 8
    masterslave:
      name: ms
      master-data-source-name: master
      slave-data-source-names: slave0
    props:
      sql:
        show: true

3.启动失败:需要忽略druid连接池的默认数据源配置(@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})

4.测试插入数据使用主库

查询使用从库

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以通过创建一个配置类来配置springboot+DruidDataSource+shardingsphere的数据库配置,而不是将配置信息放在yml文件中。以下是一个示例配置类的代码: ```java @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DruidDataSourceBuilder.create().build(); } @Bean public DataSourceProxy dataSourceProxy(DataSource dataSource) { return new DataSourceProxy(dataSource); } @Bean public DataSourceRule dataSourceRule(DataSourceProxy dataSourceProxy) { DataSourceRule dataSourceRule = new DataSourceRule(dataSourceProxy); // 配置分库分表规则 // dataSourceRule.addDataSourceRule(...) // dataSourceRule.addTableRule(...) return dataSourceRule; } @Bean public ShardingRule shardingRule(DataSourceRule dataSourceRule) { ShardingRule shardingRule = ShardingRule.builder() .dataSourceRule(dataSourceRule) // 配置分片规则 // shardingRule.getTableRuleConfigs().add(...) .build(); return shardingRule; } @Bean public DataSource dataSource(ShardingRule shardingRule) throws SQLException { return ShardingDataSourceFactory.createDataSource(shardingRule); } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); // 配置其他属性 // sessionFactory.setMapperLocations(...) return sessionFactory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 您可以根据自己的需求在配置类中添加适当的配置,比如配置数据源、分库分表规则、分片规则等。这样,您就可以将数据库配置信息集中在一个配置类中,而不是放在yml文件中。 #### 引用[.reference_title] - *1* *2* [springboot+druid连接池+mybatisplus+shardingsphere5.0alpha实现分库分表实战案例](https://blog.csdn.net/qq_40726812/article/details/120594172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Springboot+mybatis+shardingsphere 分库分表](https://blog.csdn.net/l_ian/article/details/120547449)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值