您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。