Commit b8d581c7 authored by 鲁鑫's avatar 鲁鑫

配置多数据源

parent 1463c908
...@@ -6,6 +6,10 @@ log.db.url=jdbc:mysql://192.168.222.122:3406/topsun?serverTimezone=GMT%2B8&useUn ...@@ -6,6 +6,10 @@ log.db.url=jdbc:mysql://192.168.222.122:3406/topsun?serverTimezone=GMT%2B8&useUn
log.db.user=root log.db.user=root
log.db.password=123456 log.db.password=123456
hana.db.url=jdbc:mysql://127.0.0.1:3306/topsun?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
hana.db.user=topsun
hana.db.password=topsun
#pub.db.url=jdbc:mysql://127.0.0.1:3306/topsun?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false #pub.db.url=jdbc:mysql://127.0.0.1:3306/topsun?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
#pub.db.user=topsun #pub.db.user=topsun
#pub.db.password=topsun #pub.db.password=topsun
......
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="mysqlDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${pub.db.url}"/>
<property name="username" value="${pub.db.user}"/>
<property name="password" value="${pub.db.password}"/>
<property name="filters" value="stat"/>
<property name="maxActive" value="20"/>
<property name="initialSize" value="1"/>
<!--<property name="maxWait" value="60000"/>-->
<property name="maxWait" value="20000"/>
<property name="minIdle" value="1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="false"/>
<property name="validationQuery" value="select 1"/>
<property name="poolPreparedStatements" value="true"/>
<property name="maxOpenPreparedStatements" value="20"/>
<property name="asyncInit" value="true"/>
<!--property name="keepAlive" value="true"/>-->
</bean>
<bean id="hanaDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${hana.db.url}"/>
<property name="username" value="${hana.db.user}"/>
<property name="password" value="${hana.db.password}"/>
<property name="filters" value="stat"/>
<property name="maxActive" value="20"/>
<property name="initialSize" value="1"/>
<!--<property name="maxWait" value="60000"/>-->
<property name="maxWait" value="20000"/>
<property name="minIdle" value="1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="false"/>
<property name="validationQuery" value="select 1"/>
<property name="poolPreparedStatements" value="true"/>
<property name="maxOpenPreparedStatements" value="20"/>
<property name="asyncInit" value="true"/>
<!--property name="keepAlive" value="true"/>-->
</bean>
<bean id="lobHandler" lazy-init="true" class="com.alibaba.druid.support.spring.DruidLobHandler">
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--
配置切面 需要扫描的@DataSource的位置
为业务逻辑层的方法解析@DataSource注解
为当前线程的HandleDataSource注入数据源
-->
<bean id="sourceAspect" class="com.huigou.topsun.dataSource.DataSourceAspect" />
<aop:config>
<aop:aspect id="dataSourceAspect" ref="sourceAspect" order="-999">
<!--<aop:pointcut id="ds" expression="execution(* com.huigou.topsun.technology.application..*.*(..)) and !execution(* com.huigou.topsun.technology.application.*Impl.*(..))"/>-->
<aop:pointcut id="ds" expression="execution(* com.huigou..application..*.*(..)) and !execution(* com.huigou..application.*Impl.*(..))"/>
<aop:before pointcut-ref="ds" method="before" />
<aop:after pointcut-ref="ds" method="after"/>
</aop:aspect>
</aop:config>
<!-- 动态数据源,根据service接口上的注解来决定取哪个数据源 -->
<bean id="dataSource" class="com.huigou.topsun.dataSource.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="hanaDataSource" value-ref="hanaDataSource"/>
<entry key="mysqlDataSource" value-ref="mysqlDataSource"/>
</map>
</property>
<!--默认用主库 -->
<property name="defaultTargetDataSource" ref="mysqlDataSource"/>
</bean>
</beans>
\ No newline at end of file
package com.huigou.topsun.dataSource;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 目标数据源注解,注解在方法上指定数据源的名称
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSource {
String value();//此处接收的是数据源的名称
}
\ No newline at end of file
package com.huigou.topsun.dataSource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import java.lang.reflect.Method;
@Slf4j
public class DataSourceAspect {
/**
* 在dao层方法获取datasource对象之前,在切面中指定当前线程数据源
*/
public void before(JoinPoint point) {
Class<?>[] parameterTypes = ((MethodSignature)point.getSignature()).getMethod().getParameterTypes();
Method method = null;
try {
method = point.getTarget().getClass().getDeclaredMethod(point.getSignature().getName(), parameterTypes);
if (method != null && method.isAnnotationPresent(DataSource.class)) {
DataSource data = method.getAnnotation(DataSource.class);
log.debug("用户选择数据库库类型: {}", data.value());
HandleDataSource.putDataSource(data.value()); // 数据源放到当前线程中
}
} catch (Exception e) {
log.error("aop分配数据源异常", e);
}
}
public void after(JoinPoint joinPoint) throws Throwable {
HandleDataSource.removeDataSource();
}
}
\ No newline at end of file
package com.huigou.topsun.dataSource;
import com.huigou.util.StringUtil;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
/**
* 获取与数据源相关的key 此key是Map<String,DataSource> resolvedDataSources 中与数据源绑定的key值
* 在通过determineTargetDataSource获取目标数据源时使用
*/
@Override
protected Object determineCurrentLookupKey() {
String dataSource = HandleDataSource.getDataSource();
if (StringUtil.isNotBlank(dataSource)){
System.out.println("——————————切换的数据源是:——————————"+dataSource);
}
return dataSource;
}
}
\ No newline at end of file
package com.huigou.topsun.dataSource;
public class HandleDataSource {
public static final ThreadLocal<String> holder = new ThreadLocal<String>();
/**
* 绑定当前线程数据源
*
* @param datasource
*/
public static void putDataSource(String datasource) {
holder.set(datasource);
}
/**
* 获取当前线程的数据源
*
* @return
*/
public static String getDataSource() {
return holder.get();
}
public static void removeDataSource(){
holder.remove();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment