要实现MySQL读写分离,我们需要用到SpringBoot框架中的多数据源配置。
首先,在SpringBoot的application.properties文件中添加连接主库的配置:
#主数据源配置
spring.datasource.url=jdbc:mysql://主库地址:3306/数据库名称
spring.datasource.username=主库用户名
spring.datasource.password=主库密码
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#连接池配置
#初始化时建立物理连接的个数
spring.datasource.initialSize=5
#最小连接池数量
spring.datasource.minIdle=5
#最大连接池数量
spring.datasource.maxActive=20
#获取连接时最大等待时间,单位毫秒
spring.datasource.maxWait=60000
#druid监控配置
spring.datasource.filters=stat,wall
spring.datasource.stat-view-servlet.allow=true
spring.datasource.web-stat-filter.enabled=true
#开启Druid的监控统计功能
spring.datasource.druid.monitoring.enable=true
# 合并多个DruidDataSource的监控数据
spring.datasource.druid.stat.mergeSql=true
spring.datasource.druid.stat.slowSqlMillis=5000
spring.datasource.druid.filter.stat.log-slow-sql=true
然后,在application.properties文件中添加连接从库的配置:
#从数据源1,即读数据库配置
spring.datasource.slave1.url=jdbc:mysql://从库1地址:3306/数据库名称
spring.datasource.slave1.username=从库1用户名
spring.datasource.slave1.password=从库1密码
spring.datasource.slave1.driverClassName=com.mysql.jdbc.Driver
spring.datasource.slave1.type=com.alibaba.druid.pool.DruidDataSource
#从数据源2,即读数据库配置
spring.datasource.slave2.url=jdbc:mysql://从库2地址:3306/数据库名称
spring.datasource.slave2.username=从库2用户名
spring.datasource.slave2.password=从库2密码
spring.datasource.slave2.driverClassName=com.mysql.jdbc.Driver
spring.datasource.slave2.type=com.alibaba.druid.pool.DruidDataSource
#设置mybatis的mapper文件地址
mybatis.mapper-locations=classpath:mapper/**/*.xml
#JPA自动创建数据表
spring.jpa.hibernate.ddl-auto=update
#开启Druid的监控统计功能
spring.datasource.slave1.druid.stat-view-servlet.allow=true
spring.datasource.slave1.druid.web-stat-filter.enabled=true
spring.datasource.slave1.druid.filters=stat,wall
spring.datasource.slave1.druid.initialSize=1
spring.datasource.slave1.druid.minIdle=1
spring.datasource.slave1.druid.maxActive=20
spring.datasource.slave1.druid.maxWait=60000
spring.datasource.slave1.druid.stat.mergeSql=true
spring.datasource.slave1.druid.stat.slowSqlMillis=5000
spring.datasource.slave1.druid.filter.stat.log-slow-sql=true
#开启Druid的监控统计功能
spring.datasource.slave2.druid.stat-view-servlet.allow=true
spring.datasource.slave2.druid.web-stat-filter.enabled=true
spring.datasource.slave2.druid.filters=stat,wall
spring.datasource.slave2.druid.initialSize=1
spring.datasource.slave2.druid.minIdle=1
spring.datasource.slave2.druid.maxActive=20
spring.datasource.slave2.druid.maxWait=60000
spring.datasource.slave2.druid.stat.mergeSql=true
spring.datasource.slave2.druid.stat.slowSqlMillis=5000
spring.datasource.slave2.druid.filter.stat.log-slow-sql=true
然后,我们需要添加一个数据源配置类,来配置主库与从库的动态切换:
@Configuration
public class DataSourceConfiguration {
/**
* 配置主库数据源
* @return
* @throws SQLException
*/
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource masterDataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
/**
* 配置第一个从库数据源
* @return
* @throws SQLException
*/
@Bean(name = "slave1DataSource")
@ConfigurationProperties(prefix="spring.datasource.slave1")
public DruidDataSource slave1DataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
/**
* 配置第二个从库数据源
* @return
* @throws SQLException
*/
@Bean(name = "slave2DataSource")
@ConfigurationProperties(prefix="spring.datasource.slave2")
public DruidDataSource slave2DataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
@Bean(name = "dynamicDataSource")
public DataSource dynamicDataSource() throws SQLException {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
//配置主库数据源
DruidDataSource masterDataSource = masterDataSource();
dynamicDataSource.setDefaultTargetDataSource(masterDataSource);
dynamicDataSource.setMasterDataSource(masterDataSource);
//配置从库数据源
DruidDataSource slave1DataSource = slave1DataSource();
DruidDataSource slave2DataSource = slave2DataSource();
Map<Object,Object> map=new HashMap<Object, Object>();
map.put(DynamicDataSourceGlobal.SLAVE1, slave1DataSource);
map.put(DynamicDataSourceGlobal.SLAVE2, slave2DataSource);
dynamicDataSource.setTargetDataSources(map);
return dynamicDataSource;
}
}
最后,在服务层或dao层中可以使用注解来指定使用哪个数据源。例如:
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@DataSource(DynamicDataSourceGlobal.MASTER)
public void addUser(User user){
userMapper.addUser(user);
}
@DataSource(DynamicDataSourceGlobal.SLAVE1)
public User getUserById(String id){
return userMapper.getUserById(id);
}
@DataSource(DynamicDataSourceGlobal.SLAVE2)
public List<User> getUserList(){
return userMapper.getUserList();
}
}
这样一来,就完成了SpringBoot项目中的MySQL读写分离搭建。
示例:在主库和两个从库的数据表中各建立一个users表,然后在服务层的方法中添加、查询用户数据,使其分别使用主库和两个从库,查看数据库的连接情况和执行结果,验证读写分离配置的有效性。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot项目中如何实现MySQL读写分离详解 - Python技术站