Spring?多數據源方法級別注解實現過程
Spring框架提供了多種數據源管理方式,其中多數據源管理是其中之一。多數據源管理允許應用程序使用多個數據源,而不是只使用一個數據源,從而提高了應用程序的靈活性和可靠性。
多數據源管理的主要目的是讓應用程序能夠在不同的數據庫之間切換,以滿足不同的業(yè)務需求。例如,如果應用程序需要訪問一個高可用性的數據庫,但是該數據庫當前不可用,那么應用程序可以自動切換到備用數據庫,以保證業(yè)務的連續(xù)性。
在多數據源管理中,Spring框架通過使用JNDI(Java命名和目錄接口)來管理數據源。JNDI允許應用程序在運行時動態(tài)查找數據源,并且可以使用不同的數據源來訪問不同的數據庫。
在多數據源管理中,還需要考慮數據源的配置和管理。這包括創(chuàng)建數據源、配置數據源、管理連接池和處理連接泄漏等問題。Spring框架提供了多種數據源配置和管理的工具和技術,例如Spring JDBC Template、Spring Data JPA和Spring Boot等。
總之,多數據源管理是Spring框架中非常重要的一部分,它可以提高應用程序的靈活性和可靠性,從而更好地滿足業(yè)務需求。
廢話少說,直接開干!
下面詳細地介紹一下多數據源的實現過程,同時實現一個方法級別的注解,便于靈活使用。
1, Pom配置
引入mysql,spring-boot-starter-jdbc
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>2.4.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency>
2,配置數據源
可以在項目里,或者在apollo,nacos等配置中心上
#數據源master spring.dynamic.datasource.master.url = jdbc:mysql://127.0.0.1:3306/aaa?characterEncoding=utf-8&rewriteBatchedStatements=true&tinyInt1isBit=false&allowMultiQueries=true spring.dynamic.datasource.master.username = root spring.dynamic.datasource.master.password = xxxxxxxx spring.dynamic.datasource.master.driver-class-name = com.mysql.cj.jdbc.Driver spring.dynamic.datasource.master.type = com.zaxxer.hikari.HikariDataSource #數據源slave spring.dynamic.datasource.slave.url = jdbc:mysql://127.0.0.1:3306/bbb?characterEncoding=utf-8&rewriteBatchedStatements=true&tinyInt1isBit=false&allowMultiQueries=true spring.dynamic.datasource.slave.username = root spring.dynamic.datasource.slave.password = yyyyyyyy spring.dynamic.datasource.slave.driver-class-name = com.mysql.cj.jdbc.Driver spring.dynamic.datasource.slave.type = com.zaxxer.hikari.HikariDataSource
3,讀取數據源配置
3.1 先看整個項目結構
3.2 新增讀取數據源配置類
import lombok.Data; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import java.util.Map; @Data @ConfigurationProperties(prefix = "spring.dynamic") public class DSProperties { private Map<String, DataSourceProperties> datasource; }
3.3 新增線程和數據源mapping的類
import com.xxx.gateway.common.Constant; import lombok.extern.slf4j.Slf4j; @Slf4j public class DynamicDataSourceHolder { /** * 保存動態(tài)數據源名稱 */ private static final ThreadLocal<String> DYNAMIC_DATASOURCE_KEY = new ThreadLocal<>(); /** * 設置/切換數據源,決定當前線程使用哪個數據源 */ public static void setDynamicDataSourceKey(String key){ log.info("數據源切換為:{}",key); DYNAMIC_DATASOURCE_KEY.set(key); } /** * 獲取動態(tài)數據源名稱,默認使用mater數據源 */ public static String getDynamicDataSourceKey(){ String key = DYNAMIC_DATASOURCE_KEY.get(); return key == null ? Constant.MASTER_DATASOURCE : key; } /** * 移除當前數據源 */ public static void removeDynamicDataSourceKey(){ log.info("移除數據源:{}",DYNAMIC_DATASOURCE_KEY.get()); DYNAMIC_DATASOURCE_KEY.remove(); } }
3.4 繼承路由多數據源的抽象類,并實現determineCurrentLookupKey方法
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { String dataBaseType = DynamicDataSourceHolder.getDynamicDataSourceKey(); return dataBaseType; } }
4,根據配置重寫數據源和事務管理器
注意: 下面這個注解里的basePackages是你項目的路徑
@MapperScan(basePackages = {"com.xxx.gateway.mapper"},
sqlSessionFactoryRef = "SqlSessionFactory")
import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import java.util.HashMap; import java.util.Map; @Configuration @EnableConfigurationProperties(DSProperties.class) @MapperScan(basePackages = {"com.xxx.gateway.mapper"}, sqlSessionFactoryRef = "SqlSessionFactory") public class DynamicDataSourceConfig { @Bean(name = "dynamicDataSource") public DynamicDataSource DataSource(DSProperties dsProperties) { Map targetDataSource = new HashMap<>(8); dsProperties.getDatasource().forEach((k, v) -> { targetDataSource.put(k, v.initializeDataSourceBuilder().build()); }); DynamicDataSource dataSource = new DynamicDataSource(); dataSource.setTargetDataSources(targetDataSource); return dataSource; } /** * 創(chuàng)建動態(tài)數據源的SqlSessionFactory,傳入的是動態(tài)數據源 * @Primary這個注解很重要,如果項目中存在多個SqlSessionFactory,這個注解一定要加上 */ @Primary @Bean("SqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean(DynamicDataSource dynamicDataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dynamicDataSource); org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); configuration.setMapUnderscoreToCamelCase(true); configuration.setDefaultFetchSize(100); configuration.setDefaultStatementTimeout(30); sqlSessionFactoryBean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml")); sqlSessionFactoryBean.setConfiguration(configuration); return sqlSessionFactoryBean.getObject(); } /** * 重寫事務管理器,管理動態(tài)數據源 */ @Primary @Bean(value = "transactionManager") public PlatformTransactionManager annotationDrivenTransactionManager(DynamicDataSource dataSource) { return new DataSourceTransactionManager(dataSource); } }
5,注解實現
5.1 新增注解定義
import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface DS { String value() default Constant.MASTER_DATASOURCE; }
5.2 新增注解實現
import com.xxx.gateway.config.DynamicDataSourceHolder; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.util.Objects; @Aspect @Order(1) @Component @Slf4j public class DsAspect { @Pointcut("@annotation(com.xgd.gateway.aspect.DS)") public void dsPointCut() { } //環(huán)繞通知 @Around("dsPointCut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ String key = getDefineAnnotation(joinPoint).value(); DynamicDataSourceHolder.setDynamicDataSourceKey(key); try { return joinPoint.proceed(); } finally { DynamicDataSourceHolder.removeDynamicDataSourceKey(); } } /** * 先判斷方法的注解,后判斷類的注解,以方法的注解為準 * @param joinPoint * @return */ private DS getDefineAnnotation(ProceedingJoinPoint joinPoint){ MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); DS dataSourceAnnotation = methodSignature.getMethod().getAnnotation(DS.class); if (Objects.nonNull(methodSignature)) { return dataSourceAnnotation; } else { Class<?> dsClass = joinPoint.getTarget().getClass(); return dsClass.getAnnotation(DS.class); } } }
6, 通過注解使用多數據源
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.xxx.gateway.aspect.DS; import com.xxx.gateway.domain.User; import org.apache.ibatis.annotations.Param; import java.util.List; public interface UserMapper extends BaseMapper<User> { public User queryByCode(@Param("code") String code); @DS(value="master") public User queryByCodeNoPass(@Param("code") String code); @DS(value="slave1") public List<String> queryBanks(); }
注意:
1,@DS注解里的value值是可以隨意的,只要和配置中心里spring.dynamic.datasource.后面的一致就可以。
2,如果方法上沒有配置數據源,那么就是走默認的master數據源,所以項目里必須至少配置了master數據源。
3,各個數據源可以重復,因為不會校驗是否重復。
到此這篇關于Spring 多數據源方法級別注解實現的文章就介紹到這了,更多相關Spring 多數據源內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot集成easy-rules規(guī)則引擎流程詳解
這篇文章主要介紹了SpringBoot集成easy-rules規(guī)則引擎流程,合理的使用規(guī)則引擎可以極大的減少代碼復雜度,提升代碼可維護性。業(yè)界知名的開源規(guī)則引擎有Drools,功能豐富,但也比較龐大2023-03-03解讀StringBuilder為何比String節(jié)省效率
StringBuilder比String節(jié)省效率的原因主要在于其可變性和性能開銷的降低,StringBuilder在內部維護一個字符數組,可以直接在原有基礎上修改,避免了每次拼接時的額外復制操作2024-12-12Spring容器刷新obtainFreshBeanFactory示例詳解
這篇文章主要為大家介紹了Spring容器刷新obtainFreshBeanFactory示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03使用JDBC連接Mysql 8.0.11出現了各種錯誤的解決
這篇文章主要介紹了使用JDBC連接Mysql 8.0.11出現了各種錯誤的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08SpringBoot3整合EasyExcel動態(tài)實現表頭重命名
這篇文章主要為大家詳細介紹了SpringBoot3整合EasyExcel如何通過WriteHandler動態(tài)實現表頭重命名,文中的示例代碼講解詳細,有需要的可以了解下2025-03-03Java反射 JavaBean對象自動生成插入,更新,刪除,查詢sql語句操作
這篇文章主要介紹了Java反射 JavaBean對象自動生成插入,更新,刪除,查詢sql語句操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08