Springboot 配置多個數據源(AOP實現分庫)
//因為DynamicDataSource是繼承與AbstractRoutingDataSource,而AbstractRoutingDataSource
又是繼承於AbstractDataSource,AbstractDataSource實現了統一
的DataSource介面,所以DynamicDataSource也可以當做DataSource使用
public class DynamicDataSource extends AbstractRoutingDataSource { private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); public DynamicDataSource(DataSource defaultTargetDataSource, Map<String, DataSource> targetDataSources) { super.setDefaultTargetDataSource(defaultTargetDataSource); super.setTargetDataSources(new HashMap<>(targetDataSources)); super.afterPropertiesSet(); } @Override protected Object determineCurrentLookupKey() { return getDataSource(); } public static void setDataSource(String dataSource) { contextHolder.set(dataSource); } public static String getDataSource() { return contextHolder.get(); } public static void clearDataSource() { contextHolder.remove(); } }
//配置多個數據源
@Configuration public class DynamicDataSourceConfig { @Bean @ConfigurationProperties("spring.datasource.druid.first") public DataSource firstDataSource(){ return DruidDataSourceBuilder.create().build(); } @Bean @ConfigurationProperties("spring.datasource.druid.second") public DataSource secondDataSource(){ return DruidDataSourceBuilder.create().build(); } @Bean @Primary public DynamicDataSource dataSource(DataSource firstDataSource, DataSource secondDataSource) { Map<String, DataSource> targetDataSources = new HashMap<>(); targetDataSources.put(DataSourceNames.FIRST, firstDataSource); targetDataSources.put(DataSourceNames.SECOND, secondDataSource); return new DynamicDataSource(firstDataSource, targetDataSources); } }
//切面實現資料來源的選擇
@Aspect @Component public class DataSourceAspect implements Ordered { protected Logger logger = LoggerFactory.getLogger(getClass()); @Pointcut("@annotation(io.renren.datasources.annotation.DataSource)") public void dataSourcePointCut() { } @Around("dataSourcePointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); DataSource ds = method.getAnnotation(DataSource.class); if(ds == null){ DynamicDataSource.setDataSource(DataSourceNames.FIRST); logger.debug("set datasource is " + DataSourceNames.FIRST); }else { DynamicDataSource.setDataSource(ds.name()); logger.debug("set datasource is " + ds.name()); } try { return point.proceed(); } finally { DynamicDataSource.clearDataSource(); logger.debug("clean datasource"); } } @Override public int getOrder() { return 1; } }
public interface DataSourceNames {
String FIRST = "first";
String SECOND = "second";
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
String name() default "";
}
//配置檔案spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
druid:
first: #資料來源1
url: jdbc:mysql://123.58.128.XXX:3306/db_fast1?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
username: admin
password: 123
second: #資料來源2
url: jdbc:mysql://123.58.128.BBB:3306/db_fast2?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
username: admin
password: qwe
initial-size: 10
max-active: 100
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
stat-view-servlet:
enabled: true
url-pattern: /druid/*
#login-username: admin
#login-password: admin
filter:
stat:
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
spring boot自帶的DataSourceAutoConfiguration禁掉
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@Import({DynamicDataSourceConfig.class})
public class RenrenApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(RenrenApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RenrenApplication.class);
}
}
相關推薦
Springboot 配置多個數據源(AOP實現分庫)
//因為DynamicDataSource是繼承與AbstractRoutingDataSource,而AbstractRoutingDataSource又是繼承於AbstractDataSource,AbstractDataSource實現了統一的DataSource介面,
mybatis+druid+springboot 註解方式配置多個數據源
pat nts println service ssp bsp manager 打開 iba 1\數據庫配置 #test數據源 spring.datasource.test.url=jdbc:mysql://*:3306/db?useUnicode=true&ch
Spring配置多個數據源,並實現資料來源的動態切換
1.首先在config.properties檔案中配置兩個資料庫連線的基本資料。這個省略了 2.在spring配置檔案中配置這兩個資料來源: 資料來源1 <!-- initialSize初始化時建立物理連線的個數0 maxActive最大
EJB 配置多個數據源
name XML cti kong 添加 local ima 不能 transacti 1.修改jboss-6.simple\server\default\deploy\transaction-jboss-beans.xml 配置文件 <bean name="Co
Spring cloud整合Reids 配置多個數據源
首先是連線池的選擇 一般有兩種 lettuce ,jedis Jedis 執行緒不安全,方法同步 Lettuce 基於Netty.nio, 方法非同步 執行緒 安全 letture通過引入spring-boot-starter-redis就可以使用 <
Mybatis配置多個數據源
1.編寫2個以上資料來源 <bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property n
hibernate配置多個數據源及事物(以兩個資料來源為例)
在ssh專案中,需要連線兩個不同ip下的資料庫,所以必須要給hibernate配置兩個或多個數據源 因為我只有一臺電腦,所以我配置的是sqlserver+mysql兩個資料來源 首先hibernate配置是直接從myeclipse中新增的 右鍵----myeclipse
一個程式配置多個數據源,並進行資料來源切換
1>.在資料庫連接配置檔案中配置,資料庫連線資訊,資料來源選擇器,多個數據源資訊,sql會話工廠 <!-- 在applicationContext-dao.xml引入資料庫資訊配置檔案db.properties --> <bean id="prope
tomcat配置多個數據源
應用場景: 公司tomcat伺服器中執行著多個工程,工程連結的mysql資料庫不同,所以每個工程的Spring總配置檔案中都配置了資料來源。 需求: 將資料來源統一拿到tomcat中配置。 本來指派給本人,由於開發
Spring Boot實現多個數據源教程收集(待實踐)
get shu 多個 href eos net -c smi tar 先收集,後續實踐。 http://blog.csdn.net/catoop/article/details/50575038 http://blog.csdn.net/neosmith/article
YII2 gii 配置多個數據庫, 或者多個配置文件的時候 gii 出現數據表不存在, 實現選擇數據庫 Table '****' does not exist. 解決 就在gii 頁面 選擇數據庫選項
TP 實現 分享 inf 技術 bsp 多個 ble png 用著用著就暈了 就在gii 頁面 選擇數據庫選項 (配置多個數據庫後制動就該有選項) YII2 gii 配置多個數據庫, 或者多個配置文件的時候 gii 出現數據表不存在, 實現選擇數據庫 Table
SpringCloud之基於SpringCloud+MybatisPlus+Config實現多個數據源的動態切換
一、首先,專案基於SpringCloud,配置檔案在Git上(包括資料來源的配置資訊)。 二、開始基於原有專案進行重構 1、寫一個動態資料來源上下文.程式碼如下: /** * 動態資料來源上下文 */ public class DbContextHolder { p
springboot 專案改進成多個數據源時遇到的問題
之前使用mybatis註解建立動態的sql語句,把相關jar包新增到springboot專案中使用,springboot中 @SpringBootApplication預設scanBasePackages是當前包 可以修改增加相關的多個包名 @SpringBootApp
mybatis如何配置使用多個數據源(environment)?
mybatis如何配置使用多個數據源? 一、資料庫連線properties配置檔案,兩個資料來源的地址: hd.jdbc.driverClassName=com.mysql.jdbc.Driver hd.jdbc.url=jdbc:mysql:/
Silverlight DataGrid內多個數據源繫結的實現辦法
我們在Silverlight開發過程中,很常見的情形是已經做好繫結的DataGrid內需要繫結不同資料來源的資料,比如一個來自不同資料來源的ComboBox。 下面舉個簡單的例子: 我們最終要顯示的結果如上圖所示,其中姓名,年齡來自 MainData 資料來源,籍貫來自
用Spring管理Hibernate連線多個數據源配置檔案
在這個配置中第一個property屬性配置目標資料來源,<map key-type="java.lang.String">中的key-type必須要和靜態鍵值對照類DataSourceMap中的值的型別相同;<entry key="Yxh" value-ref="yxhDataSource"
spring 如何實現注入多個數據源,並且可以同時使用這多個數據源!
source address: http://bbs.ibeifeng.com/simple/index.php?t16636.html 如何在spring框架中解決多資料來源的問題[轉貼] 在我們的專案中遇到這樣一個問題:我們的專案需要連線多個數據庫,而且不同的客戶在每次訪問中根據需要會去訪問不同的資
mybatis如何配置使用多個數據源(environment)
mybatis如何配置使用多個數據源? 一、資料庫連線properties配置檔案,兩個資料來源的地址: Java程式碼 hd.jdbc.driverClassName=com.mysql.jdbc.Driver hd.jdbc.url=jdbc:mysql
SpringBoot如何整合多個數據源,看這篇就夠了
SpringBoot現在是很多很多公司應用的後端框架,因為它搭建快,能更好、更快速的整合其他第三方。那麼隨著業務的不斷擴充套件,業
AngularJS $q 和 $q.all 單個數據源和多個數據源合並(promise的說明)
獲取 lar debug let index 被拒 可用 第一個 brush 這篇文章講的不錯, angular $q 和 promise!! -------------------------------------------------------------- 通