Spring Boot custom annotations + AOP achieve standby switching library

Abstract: This article is a scene needs to do when the dispatch center and monitoring center, backend TDDL to achieve sub-meter and warehouses, demand: implement the query monitor critical business when using Mybatis query data necessary to switch from the main library to the backup library, or directly connected to the standby database queries, thus reducing the pressure of the main bank, in the primary recording custom annotations Spring Boot binding AOP direct connection standby database query by this article.

A. AOP master library custom annotation achieved by switching to the backup repository

1.1 custom annotation

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SwitchDataBase {
    boolean switch2Backup() default false;
}

1.2 Implementation custom interceptor annotation process
Import the java.lang.reflect.Method;
Import java.util.Arrays;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/ **
 * go standby database processing logic notes
 * /
@Component
public class SwitchDataBaseInterceptor the implements {MethodInterceptor

    private final Logger log = LoggerFactory.getLogger(SwitchDataBaseInterceptor.class);

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Method method = invocation.getMethod();
        SwitchDataBase annotation = getAnnotation(method);
        if (annotation == null) {
            return invocation.proceed();
        }
        Object val = null;
        if(!ThreadLocalMap.containsKey(GroupDataSourceRouteHelper.DATASOURCE_INDEX)) {
            if (annotation.switch2Backup()) {
                log.info("query back up DB, method: " + method.getName());
                GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(1, true);
            } else {
                log.info("query primary DB, method: " + method.getName());
                GroupDataSourceRouteHelper.executeByGroupDataSourceIndex(0, true);
            }
        }
        try {
            val = invocation.proceed();
        } catch (Exception e) {
            log.info(method.getDeclaringClass().getName() + "." +
                    invocation.getMethod().getName() + "方法调用失败,arguments:" +
                    Arrays.toString(invocation.getArguments()));
            throw new RuntimeException(e);
        } finally {
            GroupDataSourceRouteHelper.removeGroupDataSourceIndex();
        }

        return val;
    }

    / **
    * Notes declared above method to find
    * /
    Private SwitchDataBase getAnnotation (Method, Method) {
        IF (method.isAnnotationPresent (SwitchDataBase.class)) {
            return method.getAnnotation (SwitchDataBase.class);
        }
        return null;
    }

}

1.3 Configuration OverallQueryConfiguration

Spring Boot fitted in the AOP Bean, Implementation Notes in a specific directory scan, is formed into cut to achieve notification process. The sample code below
Import com.wdk.wms.annotation.SwitchDataBaseInterceptor;
Import org.springframework.aop.Advisor;
Import org.springframework.aop.support.DefaultPointcutAdvisor;
Import org.springframework.aop.support.JdkRegexpMethodPointcut;
Import org.springframework. context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;

@Configuration
public class SwitchDataBaseConfiguration {

    @Bean(name = "overallQueryInterceptor")
    public SwitchDataBaseInterceptor overallQueryInterceptor() {
        return new SwitchDataBaseInterceptor();
    }


    //添加aop的pointcut
    @Bean(name = "jdkRegexpMethodPointcut")
    public JdkRegexpMethodPointcut jdkRegexpMethodPointcut() {
        JdkRegexpMethodPointcut jdkRegexpMethodPointcut = new JdkRegexpMethodPointcut();
        jdkRegexpMethodPointcut.setPatterns("com.wdk.wms.mapper.*");
        return jdkRegexpMethodPointcut;
    }

    Aop // set the default configuration corresponding to the original <aop: Advisor>
    @Bean
    public druidAdvisor Advisor () {
        DefaultPointcutAdvisor DefaultPointcutAdvisor new new DefaultPointcutAdvisor = ();
        defaultPointcutAdvisor.setPointcut (JdkRegexpMethodPointcut ());
        defaultPointcutAdvisor.setAdvice (overallQueryInterceptor ()) ;
        return DefaultPointcutAdvisor;
    }

}

1.4 How to use the annotations from the primary library switches to standby pool
@SwitchDataBase (= switch2Backup to true)
List <ConsumerNoticeMsg> listByTemplateOver3 (@Param ( "Templates") List <Integer> Templates);

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159830.htm