您好,登錄后才能下訂單哦!
本篇內容介紹了“AbstractRoutingDataSource AOP如何實現動態數據源切換”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
## datasource master # spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/master?characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 ## datasource slave # spring.datasource-slave.type=com.alibaba.druid.pool.DruidDataSource spring.datasource-slave.driver-class-name=com.mysql.jdbc.Driver spring.datasource-slave.url=jdbc:mysql://localhost:3306/slave?characterEncoding=UTF-8&serverTimezone=UTC spring.datasource-slave.username=root spring.datasource-slave.password=123456
public interface Datasources { String MASTER_DB = "masterDB"; String SLAVE_DB = "slaveDB"; }
@Configuration public class DataSourceConfig { //destroy-method="close"的作用是當數據庫連接不使用的時候,就把該連接重新放到數據池中,方便下次使用調用. @Bean(destroyMethod = "close", name = Datasources.MASTER_DB) @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } @Bean(destroyMethod = "close", name = Datasources.SLAVE_DB) @ConfigurationProperties(prefix = "spring.datasource-slave") public DataSource dataSourceSlave() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } }
@Configuration @MapperScan(basePackages = {"com.example.dao"}) public class MybatisConfig { @Autowired @Qualifier(Datasources.MASTER_DB) private DataSource masterDB; @Autowired @Qualifier(Datasources.SLAVE_DB) private DataSource slaveDB; /** * 動態數據源 */ @Bean(name = "dynamicDataSource") public DataSource dynamicDataSource() { DynamicDataSource dynamicDataSource = new DynamicDataSource(); // 默認數據源 dynamicDataSource.setDefaultTargetDataSource(masterDB); // 配置多數據源 Map<Object, Object> dsMap = new HashMap<>(); dsMap.put(Datasources.MASTER_DB, masterDB); dsMap.put(Datasources.SLAVE_DB, slaveDB); dynamicDataSource.setTargetDataSources(dsMap); return dynamicDataSource; } @Bean @ConfigurationProperties(prefix = "mybatis") public SqlSessionFactoryBean sqlSessionFactoryBean() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); // 配置數據源,此處配置為關鍵配置,如果沒有將 dynamicDataSource 作為數據源則不能實現切換 sqlSessionFactoryBean.setDataSource(dynamicDataSource()); return sqlSessionFactoryBean; } }
public class DataSourceContextHolder { /** * 默認數據源 */ public static final String DEFAULT_DATASOURCE = Datasources.MASTER_DB; private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); // 設置數據源名 public static void setDB(String dbType) { System.out.println("切換到{}數據源:" + dbType); contextHolder.set(dbType); } // 獲取數據源名 public static String getDB() { return (contextHolder.get()); } // 清除數據源名 public static void clearDB() { contextHolder.remove(); } }
@Aspect @Component public class DynamicDataSourceAspect { @Before("@annotation(com.example.util.RoutingDataSource)") public void beforeSwitchDS(JoinPoint joinPoint) { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); String dataSource = DataSourceContextHolder.DEFAULT_DATASOURCE; if (method.isAnnotationPresent(RoutingDataSource.class)) { RoutingDataSource routingDataSource = method.getDeclaredAnnotation(RoutingDataSource.class); dataSource = routingDataSource.value(); } DataSourceContextHolder.setDB(dataSource); } @After("@annotation(com.example.util.RoutingDataSource)") public void afterSwitchDS(JoinPoint point) { DataSourceContextHolder.clearDB(); } }
public class DynamicDataSource extends AbstractRoutingDataSource{ @Override protected Object determineCurrentLookupKey() { System.out.println("數據源為{}:" + DataSourceContextHolder.getDB()); return DataSourceContextHolder.getDB(); } }
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) public class CutDataBaseApplication { public static void main(String[] args) { SpringApplication.run(CutDataBaseApplication.class, args); } }
/** * @author aYong * @version 1.0 * @date 2019/7/24 */ @RestController @RequestMapping("/route") public class SysUserController { @Autowired private SysUserService sysUserService; @GetMapping("/test1") public SysUser test1(long id) { return sysUserService.test1(id); } @GetMapping("/test2") public Integer test2(long id, String name) { return sysUserService.test2(id, name); } }
“AbstractRoutingDataSource AOP如何實現動態數據源切換”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。