0. 排除数据源的自动装配
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class})
1. yml配置多个数据源信息
spring:
datasource:
master:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.124.200:3306/monitor
username: root
password: root
salve:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.124.201:3306/monitor
username: root
password: root
2. 使用ThreadLocal,保存数据源名称供下游业务使用
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
public static void setDataSource(String dbName) {
contextHolder.set(dbName);
}
public static String getDataSource() {
return (contextHolder.get(