在使用dubbo作为分布式服务治理框架时,遇到了一个问题,像优惠码这样的大批量数据查询和导出接口,是不适合提供dubbo服务的。
所以要讲优惠码查询接口迁移到cms里来。这样的话需要配置两个数据源,具体做法如下:
一、配置多数据源:
<!-- DBCP数据源配置 -->
<bean id="komectbackend" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="url" value="${jdbc.komectbackend.url}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="username" value="${jdbc.komectbackend.username}" />
<property name="password" value="${jdbc.komectbackend.password}" />
<!-- 超过即回收,默认值:8 -->
<property name="maxIdle" value="20"/>
<!-- minIdle要与timeBetweenEvictionRunsMillis配合使用才有用,单独使用minIdle不会起作用.默认值:0-->
<property name="minIdle" value="5"/>
<property name="initialSize" value="5"/>
<!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
<property name="testOnBorrow" value="false"/>
<!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
<property name="testOnReturn" value="false"/>
<!-- 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于
timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 -->
<property name="testWhileIdle" value="true"/>
<!-- 每60秒运行一次空闲连接回收器 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 用来检测连接是否有效的sql,要求是一个查询语句,如果validationQuery为
null,testOnBorrow、testOnReturn、testWhileIdle都不起其作用。 -->
<property name="validationQuery" value="select user()"/>
</bean>
<!-- DBCP数据源配置 -->
<bean id="komecthealth" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="url" value="${jdbc.komecthealth.url}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="username" value="${jdbc.komecthealth.username}" />
<property name="password" value="${jdbc.komecthealth.password}" />
<!-- 超过即回收,默认值:8 -->
<property name="maxIdle" value="20"/>
<!-- minIdle要与timeBetweenEvictionRunsMillis配合使用才有用,单独使用minIdle不会起作用.默认值:0-->
<property name="minIdle" value="5"/>
<property name="initialSize" value="5"/>
<!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
<property name="testOnBorrow" value="false"/>
<!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
<property name="testOnReturn" value="false"/>
<!-- 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于
timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 -->
<property name="testWhileIdle" value="true"/>
<!-- 每60秒运行一次空闲连接回收器 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 用来检测连接是否有效的sql,要求是一个查询语句,如果validationQuery为
null,testOnBorrow、testOnReturn、testWhileIdle都不起其作用。 -->
<property name="validationQuery" value="select user()"/>
</bean>
<bean id="dynamicDataSource" class="com.cmcc.komectcms.common.util.DynamicDataSource" >
<!-- 通过key-value的形式来关联数据源 -->
<property name="targetDataSources">
<map>
<entry value-ref="komectbackend" key="komectbackend"></entry>
<entry value-ref="komecthealth" key="komecthealth"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="komectbackend" />
</bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dynamicDataSource" />
<property name="configLocation" value="classpath:mybatis.xml" />
<property name="mapperLocations" value="classpath*:sqlmap/**/*.xml" />
<property name="plugins">
<bean
class="com.cmcc.akso.plugin.database.page.PaginationInterceptor">
<property name="properties">
<props>
<prop key="dialect">${jdbc.type}</prop>
</props>
</property>
</bean>
</property>
</bean>
<!-- mybatis mapper scan -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cmcc.komectcms.dao" />
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
</bean>
<!-- 事务管理器配置,多数据源事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dynamicDataSource" />
</bean>
<aop:config>
<aop:aspect id="dsAspect" ref="dataSourceInterceptor">
<aop:pointcut id="dskommecthealth" expression="execution(* com.cmcc.komectcms.dao.coupon..*(..))" />
<aop:before method="setKomecthealth" pointcut-ref="dskommecthealth"/>
</aop:aspect>
</aop:config>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
二、因为cms配置了komectend默认为数据源,这里只需将komecthealth作为备用的数据源,拦截优惠券相关接口,使用该数据源,配置拦截器如上
三、配置好sqlSessionFactory和事务管理器
四、添加相应的拦截器DataSourceInterceptor,并声明为spring组件
@Component
public class DataSourceInterceptor {
public void setKomectbackend(JoinPoint jp) {
DatabaseContextHolder.setCustomerType("komectbackend");
}
public void setKomecthealth(JoinPoint jp) {
DatabaseContextHolder.setCustomerType("komecthealth");
}
}
拦截器的方法名要与配置中的一致,在dao.coupon包下的文件执行之前加入切面dsAspect,设置切点为dskomecthealth,即数据源为komecthealth。
五、添加一个线程池:
public class DatabaseContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setCustomerType(String customerType) {
contextHolder.set(customerType);
}
public static String getCustomerType() {
return contextHolder.get();
}
public static void clearCustomerType() {
contextHolder.remove();
}
}
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return DatabaseContextHolder.getCustomerType();
}
}
该功能提交一段时间后,居然诡异的在网站上出现了错误代码,说是调不到komecthealth.t_sys_users等表,经排查将aop切点修改为如下就好了,可能是这样更精确点吧
<aop:pointcut id="dskommecthealth" expression="execution(* com.cmcc.komectcms.dao.coupon.CouponCmmsDao.*(..))" />