Loading... ## 主从复制: ### 首先我们需要有两个不同的数据库 **参考 :** https://blog.csdn.net/m0_55948982/article/details/125328452 ## 读写分离(springboot程序配置) ### 导入坐标: ```java <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>4.0.0-RC1</version> </dependency> ``` ### application.yml配置: ```java server: port: 8080 mybatis-plus: configuration: #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射 map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: id-type: ASSIGN_ID spring: shardingsphere: datasource: names: master,slave # 主数据源 master: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.231.128:3306/rw?characterEncoding=utf-8 username: root password: Abc12345678@ # 从数据源 slave: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.231.129:3306/rw?characterEncoding=utf-8 username: root password: Abc12345678@ masterslave: # 读写分离配置 load-balance-algorithm-type: round_robin #轮询 # 最终的数据源名称 name: dataSource # 主库数据源名称 master-data-source-name: master # 从库数据源名称列表,多个逗号分隔 slave-data-source-names: slave props: sql: show: true #开启SQL显示,默认false main: allow-bean-definition-overriding: true # 解决Druid冲突 ``` ### 然后: 然后就没了,哈哈,两个字,方便。 > springboot自动识别,假如是**增删改操作**就使用**主数据源**,**查询操作**就使用**从数据源**(可以配置多个从数据源) 最后修改:2022 年 08 月 14 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果文章有用,请随意打赏。