您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)SpringBoot中怎么通過(guò)自定義注解實(shí)現(xiàn)主備庫(kù)切換,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
一.通過(guò)AOP 自定義注解實(shí)現(xiàn)主庫(kù)到備庫(kù)的切換
1.1 自定義注解
自定義注解如下代碼所示
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 SwitchDataBase { boolean switch3Backup() default false;}
1.2 實(shí)現(xiàn)方法攔截器對(duì)自定義注解處理
import 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;/** * 處理走備庫(kù)邏輯的注解 */@Componentpublic class SwitchDataBaseInterceptor 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.switch3Backup()) { 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() + "方法調(diào)用失敗,arguments:" + Arrays.toString(invocation.getArguments())); throw new RuntimeException(e); } finally { GroupDataSourceRouteHelper.removeGroupDataSourceIndex(); } return val; } /** * 找方法上面聲明的注解 */ private SwitchDataBase getAnnotation(Method method) { if (method.isAnnotationPresent(SwitchDataBase.class)) { return method.getAnnotation(SwitchDataBase.class); } return null; }}
1.3 配置OverallQueryConfiguration
在Spring Boot中裝配AOP Bean,實(shí)現(xiàn)掃描特定目錄下的注解,實(shí)現(xiàn)切面變成形成通知處理。示例代碼如下
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;@Configurationpublic 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; } //設(shè)置默認(rèn)的aop配置對(duì)應(yīng)的是原來(lái)的<aop:advisor> @Bean public Advisor druidAdvisor() { DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor(); defaultPointcutAdvisor.setPointcut(jdkRegexpMethodPointcut()); defaultPointcutAdvisor.setAdvice(overallQueryInterceptor()); return defaultPointcutAdvisor; }}
1.4 如何使用注解從主庫(kù)到備庫(kù)的切換
@SwitchDataBase(switch3Backup = true)List<ConsumerNoticeMsg> listByTemplateOver3(@Param("templates") List<Integer> templates);
看完上述內(nèi)容,你們對(duì)SpringBoot中怎么通過(guò)自定義注解實(shí)現(xiàn)主備庫(kù)切換有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。