Spring 引导处理中的 Oracle DB 故障转移

Oracle DB failover in Spring Boot handling

我想创建基于注解的拦截器,它将在主数据库无法为请求提供服务时记录当前数据库的详细信息,而辅助数据库将开始为应用程序提供支持。

我从提到的 link 中找到了下面的代码,但是我找不到特定的 orcl 注释,它必须用于下面的 @Aspect for aop 注释,请帮助找到这个。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:orcl="http://www.springframework.org/schema/data/orcl"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/data/orcl
       http://www.springframework.org/schema/data/orcl/spring-data-orcl-1.0.xsd">

    <aop:config>
        <aop:advisor pointcut="execution(* *..PetStoreFacade.insertOrder(..))" 1 
            advice-ref="racFailoverInterceptor" order="1"/>
        <aop:advisor pointcut="execution(* *..PetStoreFacade.*(..))" 2 
            advice-ref="txAdvice"/>
    </aop:config>

    <orcl:rac-failover-interceptor id="racFailoverInterceptor"/> 3

    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="insert*"/>
            <tx:method name="update*"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

</beans>

https://docs.spring.io/spring-data/jdbc/old-docs/2.0.0.BUILD-SNAPSHOT/reference/html/orcl.failover.html

基本上,没有开箱即用的 "Oracle" 注释可用于使 rac 故障转移拦截器工作。但是添加新的自定义注释很容易,它会为您完成这项工作。

前提是 sprig data oracle 在你的类路径中

Maven pom.xml

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-oracle</artifactId>
    <version>1.2.1.RELEASE</version>
</dependency>

只需创建一个您要使用的标记注释即可。

package org.example;
public @interface OracleFailover {
// just a marker annotation, no body
}

为其配置顾问

<aop:config>
    <aop:advisor 
       pointcut="@annotation(org.example.OracleFailover)" 
        advice-ref="racFailoverInterceptor" order="1"/>
</aop:config>

<orcl:rac-failover-interceptor id="racFailoverInterceptor"/>

然后将其用于您的业务方法

package org.example;

@Service
public class SomeBusinessService {
    @OracleFailover
    void doSomethingWithOracle(){
        // body goes here
    }
}

请记住,Rac 故障转移拦截器应该在您的跨国拦截器之前运行,也就是说,如果在事务已经处于活动状态时进行故障转移拦截,故障转移将不会按预期工作。