The business layer mysql read write separation is implemented using spring aop

  • 2020-05-27 05:32:08
  • OfStack

spring aop, mysql master from the configuration to achieve read and write separation, and then their own configuration process, as well as problems recorded for the convenience of the next operation, also hope to give some friends to help.

1. Use spring aop to intercept the dynamic selection of existing data sources.


import java.lang.annotation.ElementType; 
import java.lang.annotation.Target; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
/** 
 * RUNTIME 
 *  The compiler will record the comments in a class file at run time  VM  Comments are retained so they can be read reflectively.  
 * @author yangGuang 
 * 
 */ 
@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface DataSource { 
  String value(); 
} 

3. Solve the problem of multiple data sources with AbstractRoutingDataSource of Spring


import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; 
 
 public class ChooseDataSource extends AbstractRoutingDataSource { 
 
   @Override 
   protected Object determineCurrentLookupKey() { 
     return HandleDataSource.getDataSource(); 
   } 
    
 } 

4. Solve the thread safety problem with ThreadLocal


public class HandleDataSource { 
  public static final ThreadLocal<String> holder = new ThreadLocal<String>(); 
  public static void putDataSource(String datasource) { 
    holder.set(datasource); 
  } 
   
  public static String getDataSource() { 
    return holder.get(); 
  }   
} 

5. Define a data source section class, accessed via aop and configured in the spring configuration file, so no aop annotations are used.


import java.lang.reflect.Method; 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 
import org.aspectj.lang.reflect.MethodSignature; 
import org.springframework.stereotype.Component; 
//@Aspect 
//@Component 
public class DataSourceAspect { 
  //@Pointcut("execution(* com.apc.cms.service.*.*(..))")  
  public void pointCut(){};  
   
 // @Before(value = "pointCut()") 
   public void before(JoinPoint point) 
    { 
      Object target = point.getTarget(); 
      System.out.println(target.toString()); 
      String method = point.getSignature().getName(); 
      System.out.println(method); 
      Class<?>[] classz = target.getClass().getInterfaces(); 
      Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()) 
          .getMethod().getParameterTypes(); 
      try { 
        Method m = classz[0].getMethod(method, parameterTypes); 
        System.out.println(m.getName()); 
        if (m != null && m.isAnnotationPresent(DataSource.class)) { 
          DataSource data = m.getAnnotation(DataSource.class); 
          HandleDataSource.putDataSource(data.value()); 
        } 
         
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
    } 
} 

6. Configuration applicationContext. xml


<!--  Master library data source  --> 
 <bean id="writeDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> 
  <property name="driverClass" value="com.mysql.jdbc.Driver"/> 
  <property name="jdbcUrl" value="jdbc:mysql://172.22.14.6:3306/cpp?autoReconnect=true"/> 
  <property name="username" value="root"/> 
  <property name="password" value="root"/> 
  <property name="partitionCount" value="4"/> 
  <property name="releaseHelperThreads" value="3"/> 
  <property name="acquireIncrement" value="2"/> 
  <property name="maxConnectionsPerPartition" value="40"/> 
  <property name="minConnectionsPerPartition" value="20"/> 
  <property name="idleMaxAgeInSeconds" value="60"/> 
  <property name="idleConnectionTestPeriodInSeconds" value="60"/> 
  <property name="poolAvailabilityThreshold" value="5"/> 
</bean> 
 
<!--  Slave data source  --> 
<bean id="readDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> 
  <property name="driverClass" value="com.mysql.jdbc.Driver"/> 
  <property name="jdbcUrl" value="jdbc:mysql://172.22.14.7:3306/cpp?autoReconnect=true"/> 
  <property name="username" value="root"/> 
  <property name="password" value="root"/> 
  <property name="partitionCount" value="4"/> 
  <property name="releaseHelperThreads" value="3"/> 
  <property name="acquireIncrement" value="2"/> 
  <property name="maxConnectionsPerPartition" value="40"/> 
  <property name="minConnectionsPerPartition" value="20"/> 
  <property name="idleMaxAgeInSeconds" value="60"/> 
  <property name="idleConnectionTestPeriodInSeconds" value="60"/> 
  <property name="poolAvailabilityThreshold" value="5"/> 
</bean> 
 
<!-- transaction manager,  Transaction management  --> 
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
  <property name="dataSource" ref="dataSource" /> 
</bean> 
 
 
<!--  Comment auto-loading  --> 
<context:annotation-config /> 
 
<!--enale component scanning (beware that this does not enable mapper scanning!)--> 
<context:component-scan base-package="com.apc.cms.persistence.rdbms" /> 
<context:component-scan base-package="com.apc.cms.service"> 
 <context:include-filter type="annotation"  
    expression="org.springframework.stereotype.Component" />  
</context:component-scan>  
 
<context:component-scan base-package="com.apc.cms.auth" /> 
 
<!-- enable transaction demarcation with annotations --> 
<tx:annotation-driven /> 
 
 
<!-- define the SqlSessionFactory --> 
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
  <property name="dataSource" ref="dataSource" /> 
  <property name="typeAliasesPackage" value="com.apc.cms.model.domain" /> 
</bean> 
 
<!-- scan for mappers and let them be autowired --> 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
  <property name="basePackage" value="com.apc.cms.persistence" /> 
  <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
</bean> 
 
<bean id="dataSource" class="com.apc.cms.utils.ChooseDataSource"> 
  <property name="targetDataSources">  
     <map key-type="java.lang.String">  
       <!-- write --> 
       <entry key="write" value-ref="writeDataSource"/>  
       <!-- read --> 
       <entry key="read" value-ref="readDataSource"/>  
     </map>  
      
  </property>  
  <property name="defaultTargetDataSource" ref="writeDataSource"/>  
</bean> 
  
<!--  Activate the automatic proxy function  --> 
<aop:aspectj-autoproxy proxy-target-class="true"/> 
 
<!--  Configure database annotations aop --> 
<bean id="dataSourceAspect" class="com.apc.cms.utils.DataSourceAspect" /> 
<aop:config> 
  <aop:aspect id="c" ref="dataSourceAspect"> 
    <aop:pointcut id="tx" expression="execution(* com.apc.cms.service..*.*(..))"/> 
    <aop:before pointcut-ref="tx" method="before"/> 
  </aop:aspect> 
</aop:config> 
<!--  Configure database annotations aop --> 

7. Use annotations, dynamically select data sources, and respectively read library and write library.


@DataSource("write") 
public void update(User user) { 
  userMapper.update(user); 
} 
 
@DataSource("read") 
public Document getDocById(long id) { 
  return documentMapper.getById(id); 
} 

Test write operations: you can apply the modify data, modify the master library data, and find that the data from the slave library has been updated synchronously, so the write operations defined are all writes to the library

Test read operation: modify the slave data in the background, check that the data in the main library has not been modified, refresh in the application page, and find that what is read is the data from the library, indicating that read and write are separated ok.

Summary of problems encountered:

In addition to spring's core jar package, aspectj.jar, aspectjweaver.jar, aopalliance.jar check pom in the project and find that there is a lack of dependent package. Since jar is not available in the local warehouse, find maven central library which can provide jar package download.


<repository> 
   <id>nexus</id> 
   <name>nexus</name> 
   <url>http://repository.sonatype.org/content/groups/public/</url> 
   <layout>default</layout> 
 </repository> 

Configure jar for project dependencies, mainly due to the lack of these two.


  <dependency> 
    <groupId>aspectj</groupId> 
    <artifactId>aspectjrt</artifactId> 
    <version>1.5.4</version> 
</dependency> 
<dependency> 
    <groupId>aspectj</groupId> 
    <artifactId>aspectjweaver</artifactId> 
    <version>1.5.4</version> 
lt;/dependency> 

Related articles: