【Spring】使用Spring的AbstractRoutingDataSource實現多資料來源切換,spring動態切換資料來源

來源:互聯網
上載者:User

【Spring】使用Spring的AbstractRoutingDataSource實現多資料來源切換,spring動態切換資料來源

最近因為項目需要在做兩個項目間資料同步的需求,具體是項目1的資料通過訊息佇列同步到項目2中,因為這個更新操作還涉及到更新多個庫的資料,所以就需要多資料來源切換的操作。下面就講講在Spring中如何進行資料來源切換。這裡是使用AbstractRoutingDataSource類來完成具體的操作,AbstractRoutingDataSource是Spring2.0後增加的。

實現資料來源切換的功能就是自訂一個類擴充AbstractRoutingDataSource抽象類別,其實該相當於資料來源DataSourcer的路由中介,可以實現在項目運行時根據相應key值切換到對應的資料來源DataSource上。先看看AbstractRoutingDataSource的源碼:

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {    /* 只列出部分代碼 */    private Map<Object, Object> targetDataSources;    private Object defaultTargetDataSource;    private boolean lenientFallback = true;    private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();    private Map<Object, DataSource> resolvedDataSources;    private DataSource resolvedDefaultDataSource;    @Override    public Connection getConnection() throws SQLException {        return determineTargetDataSource().getConnection();    }    @Override    public Connection getConnection(String username, String password) throws SQLException {        return determineTargetDataSource().getConnection(username, password);    }    protected DataSource determineTargetDataSource() {        Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");        Object lookupKey = determineCurrentLookupKey();        DataSource dataSource = this.resolvedDataSources.get(lookupKey);        if (dataSource == null && (this.lenientFallback || lookupKey == null)) {            dataSource = this.resolvedDefaultDataSource;        }        if (dataSource == null) {            throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");        }        return dataSource;    }    protected abstract Object determineCurrentLookupKey();}

從源碼可以看出AbstractRoutingDataSource繼承了AbstractDataSource並實現了InitializingBean,AbstractRoutingDataSource的getConnection()方法調用了determineTargetDataSource()的該方法,這裡重點看determineTargetDataSource()方法代碼,方法裡使用到了determineCurrentLookupKey()方法,它是AbstractRoutingDataSource類的抽象方法,也是實現資料來源切換要擴充的方法,該方法的傳回值就是項目中所要用的DataSource的key值,拿到該key後就可以在resolvedDataSource中取出對應的DataSource,如果key找不到對應的DataSource就使用預設的資料來源。

自訂類擴充AbstractRoutingDataSource類時就是要重寫determineCurrentLookupKey()方法來實現資料來源切換功能。下面是自訂的擴充AbstractRoutingDataSource類的實現:

/** * 獲得資料來源 */public class MultipleDataSource extends AbstractRoutingDataSource{    @Override    protected Object determineCurrentLookupKey() {         return DynamicDataSourceHolder.getRouteKey();    }}

DynamicDataSourceHolder類如下,實現對資料來源的操作功能:

/** * 資料來源操作類 */public class DynamicDataSourceHolder {    private static ThreadLocal<String> routeKey = new ThreadLocal<String>();    /**     * 擷取當前線程的資料來源路由的key     */    public static String getRouteKey()    {        String key = routeKey.get();        return key;    }    /**     * 綁定當前線程資料來源路由的key     * 使用完成後必須調用removeRouteKey()方法刪除     */    public static void  setRouteKey(String key)    {        routeKey.set(key);    }    /**     * 刪除與當前線程繫結資料源路由的key     */    public static void removeRouteKey()    {        routeKey.remove();    }}

下面在xml檔案中配置多個資料來源:

<!-- 資料來源 --><bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">     <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver">     </property>     <property name="url" value="jdbc:jtds:sqlserver://127.0.0.1;databaseName=test">     </property>     <property name="username" value="***"></property>     <property name="password" value="***"></property> </bean> <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">     <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver">     </property>     <property name="url" value="jdbc:jtds:sqlserver://127.0.0.2:1433;databaseName=test">     </property>     <property name="username" value="***"></property>     <property name="password" value="***"></property></bean><!-- 配置多資料來源映射 --><bean id="multipleDataSource" class="MultipleDataSource" >     <property name="targetDataSources">         <map key-type="java.lang.String">             <entry value-ref="dataSource1" key="dataSource1"></entry>             <entry value-ref="dataSource2" key="dataSource2"></entry>         </map>     </property>     <!-- 預設資料來源 -->     <property name="defaultTargetDataSource" ref="dataSource1" >     </property></bean>

到這裡基本的配置就完成了,下面只要在需要切換資料來源的地方調用方法就行了,一般是在dao層操作資料庫前進行切換的,只需在資料庫操作前加上如下代碼即可:

DynamicDataSourceHolder.setRouteKey("dataSource2");

上面介紹的是在dao層當需要切換資料來源時手動加上切換資料來源的代碼,也可以使用AOP的方式,把配置的資料來源類型都設定成註解標籤,在dao層中需要切換資料來源操作的方法或類上寫上註解標籤,這樣實現起來可操作性也更強。

@DataSourceKey("dataSource1")public interface TestEntityMapper extends MSSQLMapper<TestEntity> {    public void insertTest(TestEntity testEntity);}

DataSourceKey註解代碼如下:

@Target({ElementType.TYPE,ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface DataSourceKey {    String value() default "";}

註解配置完後就要寫一個實現資料來源切換的類,如下:

public class MultipleDataSourceExchange {    /**      * 攔截目標方法,擷取由@DataSource指定的資料來源標識,設定到線程儲存中以便切換資料來源      */      public void beforeDaoMethod(JoinPoint point) throws Exception {          Class<?> target = point.getTarget().getClass();          MethodSignature signature = (MethodSignature) point.getSignature();          // 預設使用目標類型的註解,如果沒有則使用其實現介面的註解類          for (Class<?> cls : target.getInterfaces()) {              resetDataSource(cls, signature.getMethod());          }          resetDataSource(target, signature.getMethod());      }      /**      * 提取目標對象方法註解和類註解中的資料來源標識      */      private void resetDataSource(Class<?> cls, Method method) {          try {              Class<?>[] types = method.getParameterTypes();              // 預設使用類註解              if (cls.isAnnotationPresent(DataSourceKey.class)) {                  DataSourceKey source = cls.getAnnotation(DataSourceKey.class);                  DynamicDataSourceHolder.setRouteKey(source.value());              }              // 方法註解可以覆蓋類註解              Method m = cls.getMethod(method.getName(), types);              if (m != null && m.isAnnotationPresent(DataSourceKey.class)) {                  DataSourceKey source = m.getAnnotation(DataSourceKey.class);                   DynamicDataSourceHolder.setRouteKey(source.value());              }          } catch (Exception e) {              System.out.println(cls + ":" + e.getMessage());          }      }  }

代碼寫完後就要在xml設定檔上添加配置了(只列出部分配置):

<bean id="multipleDataSourceExchange" class="MultipleDataSourceExchange "/><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <property name="dataSource" ref="multipleDataSource" /></bean><tx:advice id="txAdvice" transaction-manager="txManager">    <tx:attributes>       <tx:method name="insert*" propagation="NESTED" rollback-for="Exception"/>       <tx:method name="add*" propagation="NESTED" rollback-for="Exception"/>       ...    </tx:attributes></tx:advice><aop:config>    <aop:pointcut id="service" expression="execution(* com.datasource..*.service.*.*(..))"/>    <!-- 注意切換資料來源操作要比持久層代碼先執行 -->    <aop:advisor advice-ref="multipleDataSourceExchange" pointcut-ref="service" order="1"/>    <aop:advisor advice-ref="txAdvice" pointcut-ref="service" order="2"/></aop:config>

到此就完成使用AOP的方式實現多資料來源的動態切換了。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.