下面我将详细讲解“springboot配置多数据源的一款框架(dynamic-datasource-spring-boot-starter)”的完整攻略。
什么是dynamic-datasource-spring-boot-starter
dynamic-datasource-spring-boot-starter(以下简称DDSS)是一款基于Spring Boot的多数据源管理框架,它也是一个Spring Boot的starter,能够自动化地配置多数据源,并支持在代码中切换数据源。
如何使用dynamic-datasource-spring-boot-starter
下面,我将简单介绍如何使用dynamic-datasource-spring-boot-starter。
首先,在pom.xml文件中引入以下依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.7.0</version>
</dependency>
引入依赖之后,我们需要在application.yml或application.properties文件中进行如下配置:
spring:
datasource:
dynamic:
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
slave1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
slave2:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3308/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
这里我们配置了三个数据源,一个主数据源和两个从数据源。
接着,在项目的代码中,可以通过以下方式动态切换数据源:
@Service
public class UserServiceImpl implements UserService {
/**
* 多数据源注入
*
* @return
*/
@Autowired
private DataSource dataSource;
/**
* 注入JdbcTemplate
*
* @return
*/
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 根据id查询用户
*
* @param id
* @return
*/
@Override
public User getUserById(Long id) {
//获取主数据源
DynamicDataSourceContextHolder.setDataSourceKey(DynamicDataSourceContextHolder.MASTER);
String sql = "SELECT * FROM user WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(User.class));
}
/**
* 查询所有用户
*
* @return
*/
@Override
public List<User> getAllUser() {
//获取从数据源1
DynamicDataSourceContextHolder.setDataSourceKey("slave1");
String sql = "SELECT * FROM user";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
}
/**
* 根据用户名和密码查询用户
*
* @param userName
* @param password
* @return
*/
@Override
public User getUserByUserNameAndPassword(String userName, String password) {
//获取从数据源2
DynamicDataSourceContextHolder.setDataSourceKey("slave2");
String sql = "SELECT * FROM user WHERE user_name = ? and password = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{userName, password}, new BeanPropertyRowMapper<>(User.class));
}
}
这里我们通过使用DynamicDataSourceContextHolder.setDataSourceKey()方法,动态切换数据源。
dynamic-datasource-spring-boot-starter的示例
下面,我将给出两个dynamic-datasource-spring-boot-starter的示例。
示例一
在此示例中,我们首先需要在application.yml或application.properties文件中进行如下配置:
spring:
datasource:
dynamic:
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
slave1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
slave2:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3308/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
然后,在项目的代码中,通过以下方式动态切换数据源:
@Service
public class UserServiceImpl implements UserService {
/**
* 多数据源注入
*
* @return
*/
@Autowired
private DataSource dataSource;
/**
* 注入JdbcTemplate
*
* @return
*/
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 根据id查询用户
*
* @param id
* @return
*/
@Override
public User getUserById(Long id) {
//获取主数据源
DynamicDataSourceContextHolder.setDataSourceKey(DynamicDataSourceContextHolder.MASTER);
String sql = "SELECT * FROM user WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(User.class));
}
/**
* 查询所有用户
*
* @return
*/
@Override
public List<User> getAllUser() {
//获取从数据源1
DynamicDataSourceContextHolder.setDataSourceKey("slave1");
String sql = "SELECT * FROM user";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
}
/**
* 根据用户名和密码查询用户
*
* @param userName
* @param password
* @return
*/
@Override
public User getUserByUserNameAndPassword(String userName, String password) {
//获取从数据源2
DynamicDataSourceContextHolder.setDataSourceKey("slave2");
String sql = "SELECT * FROM user WHERE user_name = ? and password = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{userName, password}, new BeanPropertyRowMapper<>(User.class));
}
}
示例二
在此示例中,我们使用dynamic-datasource-spring-boot-starter来实现读写分离。
首先,我们需要在application.yml或application.properties文件中进行如下配置:
spring:
datasource:
dynamic:
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
slave1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
slave2:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3308/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
shardingsphere:
ds:
primary:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
replica1:
url: jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
replica2:
url: jdbc:mysql://localhost:3308/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
masterslave:
name: ms
load-balance-algorithm-type: round_robin
master-data-source-name: primary
slave-data-source-names: replica1, replica2
slave-data-source-selector: com.baomidou.dynamic.datasource.spring.boot.autoconfigure.master_slave.RandomSlaveDataSourceSelector
这里我们配置了一个主数据源和两个从数据源,并使用了ShardingSphere实现了读写分离。
然后,在项目的代码中,通过以下方式动态切换数据源:
@Service
public class UserServiceImpl implements UserService {
/**
* 多数据源注入
*
* @return
*/
@Autowired
private DataSource dataSource;
/**
* 注入JdbcTemplate
*
* @return
*/
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 根据id查询用户
*
* @param id
* @return
*/
@Override
public User getUserById(Long id) {
//获取主数据源
DynamicDataSourceContextHolder.setDataSourceKey(DynamicDataSourceContextHolder.MASTER);
String sql = "SELECT * FROM user WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(User.class));
}
/**
* 查询所有用户
*
* @return
*/
@Override
public List<User> getAllUser() {
RandomSlaveDataSourceSelector.setSlaveIndex(new Random().nextInt(2));
DynamicDataSourceContextHolder.setDataSourceKey("ms");
String sql = "SELECT * FROM user";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
}
/**
* 根据用户名和密码查询用户
*
* @param userName
* @param password
* @return
*/
@Override
public User getUserByUserNameAndPassword(String userName, String password) {
RandomSlaveDataSourceSelector.setSlaveIndex(new Random().nextInt(2));
DynamicDataSourceContextHolder.setDataSourceKey("ms");
String sql = "SELECT * FROM user WHERE user_name = ? and password = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{userName, password}, new BeanPropertyRowMapper<>(User.class));
}
}
这里我们通过使用DynamicDataSourceContextHolder.setDataSourceKey()方法和RandomSlaveDataSourceSelector实现了对数据源的动态切换。需要注意的是,在读写分离的场景下,需要使用ShardingSphere的配置方式来实现数据源的切换。
以上就是关于“springboot配置多数据源的一款框架(dynamic-datasource-spring-boot-starter)”的完整攻略和两个示例。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:springboot配置多数据源的一款框架(dynamic-datasource-spring-boot-starter) - Python技术站