From dceda7d5ade6f599a2a3bb2ebf371c455f5d9a2b Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Wed, 26 Apr 2023 11:37:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=201.1.0=201.=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A=202.=20X?= =?UTF-8?q?Retry=E5=90=8D=E7=A7=B0=E5=8F=98=E6=9B=B4EasyRetry=203.=20Retry?= =?UTF-8?q?Aspect=E6=B7=BB=E5=8A=A0Ordered=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=B0=83=E6=95=B4Aop=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/core/intercepter/RetryAspect.java | 14 ++++++- .../client/core/report/ReportHandler.java | 2 + .../core/serializer/HessianSerializer.java | 3 +- .../core/serializer/JacksonSerializer.java | 2 + ... => EasyRetryClientAutoConfiguration.java} | 2 +- ...ar.java => EasyRetryClientsRegistrar.java} | 9 +++-- .../retry/client/starter/EnableEasyRetry.java | 38 +++++++++++++++++++ .../retry/client/starter/EnableXRetry.java | 18 --------- .../main/resources/META-INF/spring.factories | 2 +- .../java/com/example/ExampleApplication.java | 8 ++-- .../com/example/demo/TestEventListener.java | 22 +++++++++++ .../TestExistsTransactionalRetryService.java | 5 +++ .../TestExistsTransactionalRetryService2.java | 5 +++ .../com/example/model/TransactionalEvent.java | 18 +++++++++ .../ExistsTransactionalRetryServiceTest.java | 8 ++-- .../com/example/NestMethodServiceTest.java | 2 +- 16 files changed, 123 insertions(+), 35 deletions(-) rename easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/{XRetryClientAutoConfiguration.java => EasyRetryClientAutoConfiguration.java} (89%) rename easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/{XRetryClientsRegistrar.java => EasyRetryClientsRegistrar.java} (78%) create mode 100644 easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EnableEasyRetry.java delete mode 100644 easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EnableXRetry.java create mode 100644 example/src/main/java/com/example/demo/TestEventListener.java create mode 100644 example/src/main/java/com/example/model/TransactionalEvent.java diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/RetryAspect.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/RetryAspect.java index df6088204..1a55f6588 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/RetryAspect.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/intercepter/RetryAspect.java @@ -22,8 +22,10 @@ import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.Ordered; import org.springframework.core.PriorityOrdered; import org.springframework.core.annotation.Order; +import org.springframework.core.env.StandardEnvironment; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @@ -39,8 +41,7 @@ import java.util.UUID; @Aspect @Component @Slf4j -@Order(PriorityOrdered.HIGHEST_PRECEDENCE + 2) -public class RetryAspect { +public class RetryAspect implements Ordered { private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); private static String retryErrorMoreThresholdTextMessageFormatter = @@ -55,6 +56,8 @@ public class RetryAspect { private RetryStrategy retryStrategy; @Autowired private AltinAlarmFactory altinAlarmFactory; + @Autowired + private StandardEnvironment standardEnvironment; @Around("@annotation(com.aizuda.easy.retry.client.core.annotation.Retryable)") public Object around(ProceedingJoinPoint point) throws Throwable { @@ -192,4 +195,11 @@ public class RetryAspect { } return objMethod.getAnnotation(Retryable.class); } + + @Override + public int getOrder() { + String order = standardEnvironment + .getProperty("easy-retry.aop.order", String.valueOf(Ordered.HIGHEST_PRECEDENCE)); + return Integer.parseInt(order); + } } diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/ReportHandler.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/ReportHandler.java index d83a6ab12..187bd8dd9 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/ReportHandler.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/ReportHandler.java @@ -25,6 +25,8 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; /** + * 上报服务端 + * * @author: www.byteblogs.com * @date : 2022-03-08 09:24 */ diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/serializer/HessianSerializer.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/serializer/HessianSerializer.java index 603e63001..e8304f5ed 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/serializer/HessianSerializer.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/serializer/HessianSerializer.java @@ -15,9 +15,10 @@ import java.util.Base64; import java.util.Objects; /** + * Hessian序列化 + * * @author: www.byteblogs.com * @date : 2022-03-07 15:08 - * @date 2022/1/5 11:14 上午 */ @Component("XRetryHessianSerializer") public class HessianSerializer implements RetryArgSerializer { diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/serializer/JacksonSerializer.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/serializer/JacksonSerializer.java index 7f7b306c7..d54cba175 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/serializer/JacksonSerializer.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/serializer/JacksonSerializer.java @@ -11,6 +11,8 @@ import java.lang.reflect.Method; import java.lang.reflect.Type; /** + * Jackson序列化 + * * @author: www.byteblogs.com * @date : 2022-03-07 15:08 */ diff --git a/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/XRetryClientAutoConfiguration.java b/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EasyRetryClientAutoConfiguration.java similarity index 89% rename from easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/XRetryClientAutoConfiguration.java rename to easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EasyRetryClientAutoConfiguration.java index ffd687697..4b90cf711 100644 --- a/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/XRetryClientAutoConfiguration.java +++ b/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EasyRetryClientAutoConfiguration.java @@ -7,6 +7,6 @@ import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("com.aizuda.easy.retry.client.core") @ConditionalOnProperty(prefix = "easy-retry", name = "enabled", havingValue = "true") -public class XRetryClientAutoConfiguration { +public class EasyRetryClientAutoConfiguration { } diff --git a/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/XRetryClientsRegistrar.java b/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EasyRetryClientsRegistrar.java similarity index 78% rename from easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/XRetryClientsRegistrar.java rename to easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EasyRetryClientsRegistrar.java index fc12f7e82..713f8e33e 100644 --- a/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/XRetryClientsRegistrar.java +++ b/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EasyRetryClientsRegistrar.java @@ -10,18 +10,21 @@ import org.springframework.core.type.AnnotationMetadata; import java.util.Map; /** + * Easy Retry 客户端注册器 + * * @author: www.byteblogs.com * @date : 2022-03-04 18:44 */ -public class XRetryClientsRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware { +public class EasyRetryClientsRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware { private StandardEnvironment standardEnvironment; @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { - Map attrs = importingClassMetadata.getAnnotationAttributes(EnableXRetry.class.getName()); + Map attrs = importingClassMetadata.getAnnotationAttributes(EnableEasyRetry.class.getName()); Map systemEnvironment = standardEnvironment.getSystemProperties(); - systemEnvironment.put("easy-retry.group", (String) attrs.get("group")); + systemEnvironment.put("easy-retry.group", attrs.get("group")); + systemEnvironment.put("easy-retry.aop.order", attrs.get("order")); } @Override diff --git a/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EnableEasyRetry.java b/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EnableEasyRetry.java new file mode 100644 index 000000000..2f0cd1b66 --- /dev/null +++ b/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EnableEasyRetry.java @@ -0,0 +1,38 @@ +package com.aizuda.easy.retry.client.starter; + +import org.springframework.context.annotation.Import; +import org.springframework.core.Ordered; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import java.lang.annotation.*; + +/** + * 在启动类上添加EnableEasyRetry注解开启Easy Retry功能 + * + * @author: www.byteblogs.com + * @date : 2021-12-31 18:45 + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Import(EasyRetryClientsRegistrar.class) +public @interface EnableEasyRetry { + + /** + * 表示该重试数据属于哪个系统并且全局唯一 + * + * @return + */ + String group(); + + /** + * 控制多个Aop的执行顺序, + * 需要注意的是这里顺序要比事务的Aop要提前 + * + * see {@link EnableTransactionManagement#order()} + * 默认值: Ordered.HIGHEST_PRECEDENCE + */ + int order() default Ordered.HIGHEST_PRECEDENCE; + + +} diff --git a/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EnableXRetry.java b/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EnableXRetry.java deleted file mode 100644 index f7eb7f450..000000000 --- a/easy-retry-client-starter/src/main/java/com/aizuda/easy/retry/client/starter/EnableXRetry.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.aizuda.easy.retry.client.starter; - -import org.springframework.context.annotation.Import; - -import java.lang.annotation.*; - -/** - * @author: www.byteblogs.com - * @date : 2021-12-31 18:45 - */ -@Target({ElementType.TYPE}) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@Import(XRetryClientsRegistrar.class) -public @interface EnableXRetry { - - String group(); -} diff --git a/easy-retry-client-starter/src/main/resources/META-INF/spring.factories b/easy-retry-client-starter/src/main/resources/META-INF/spring.factories index feaa140b5..b65e43ff4 100644 --- a/easy-retry-client-starter/src/main/resources/META-INF/spring.factories +++ b/easy-retry-client-starter/src/main/resources/META-INF/spring.factories @@ -1,2 +1,2 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.aizuda.easy.retry.client.starter.XRetryClientAutoConfiguration + com.aizuda.easy.retry.client.starter.EasyRetryClientAutoConfiguration diff --git a/example/src/main/java/com/example/ExampleApplication.java b/example/src/main/java/com/example/ExampleApplication.java index 21337e444..cc0fc962f 100644 --- a/example/src/main/java/com/example/ExampleApplication.java +++ b/example/src/main/java/com/example/ExampleApplication.java @@ -1,19 +1,19 @@ package com.example; -import com.aizuda.easy.retry.client.starter.EnableXRetry; +import com.aizuda.easy.retry.client.starter.EnableEasyRetry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; -import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; //import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.core.Ordered; import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication @EnableFeignClients(basePackages = "com.example.client") -@EnableXRetry(group = "example_group") +@EnableEasyRetry(group = "example_group") @EnableAspectJAutoProxy +//@EnableTransactionManagement(order = Ordered.LOWEST_PRECEDENCE - 10) @EnableTransactionManagement public class ExampleApplication { diff --git a/example/src/main/java/com/example/demo/TestEventListener.java b/example/src/main/java/com/example/demo/TestEventListener.java new file mode 100644 index 000000000..a05fc79ab --- /dev/null +++ b/example/src/main/java/com/example/demo/TestEventListener.java @@ -0,0 +1,22 @@ +package com.example.demo; + +import com.example.model.TransactionalEvent; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionPhase; +import org.springframework.transaction.event.TransactionalEventListener; + +/** + * @author: shuguang.zhang + * @date : 2023-04-25 22:46 + */ +@Component +@Slf4j +public class TestEventListener { + + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true) + public void beforeConfirmSell(TransactionalEvent event) { + log.info("触发beforeConfirmSell事件 [{}]", event); + + } +} diff --git a/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService.java b/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService.java index 98c48c10e..1d1411609 100644 --- a/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService.java +++ b/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService.java @@ -1,9 +1,11 @@ package com.example.demo; import com.aizuda.easy.retry.client.core.annotation.Retryable; +import com.aizuda.easy.retry.common.core.context.SpringContext; import com.aizuda.easy.retry.common.core.model.Result; import com.example.mapper.SchoolMapper; import com.example.mapper.StudentMapper; +import com.example.model.TransactionalEvent; import com.example.po.School; import com.example.po.Student; import org.springframework.beans.factory.annotation.Autowired; @@ -53,6 +55,9 @@ public class TestExistsTransactionalRetryService { throw new UnsupportedOperationException("调用远程失败" + school.getAddress()); } + TransactionalEvent event = new TransactionalEvent<>("123"); + SpringContext.applicationContext.publishEvent(event); + return "testSimpleInsert"+school.getAddress(); } diff --git a/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService2.java b/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService2.java index 6b8be3a2f..7d77db604 100644 --- a/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService2.java +++ b/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService2.java @@ -1,10 +1,12 @@ package com.example.demo; import com.aizuda.easy.retry.client.core.annotation.Retryable; +import com.aizuda.easy.retry.common.core.context.SpringContext; import com.aizuda.easy.retry.common.core.model.Result; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.example.mapper.SchoolMapper; import com.example.mapper.StudentMapper; +import com.example.model.TransactionalEvent; import com.example.po.School; import com.example.po.Student; import org.springframework.beans.factory.annotation.Autowired; @@ -48,6 +50,9 @@ public class TestExistsTransactionalRetryService2 { throw new UnsupportedOperationException("调用远程失败"); } + TransactionalEvent event = new TransactionalEvent<>("123"); + SpringContext.applicationContext.publishEvent(event); + return "testSimpleInsert"+school.getAddress(); } diff --git a/example/src/main/java/com/example/model/TransactionalEvent.java b/example/src/main/java/com/example/model/TransactionalEvent.java new file mode 100644 index 000000000..aa9400934 --- /dev/null +++ b/example/src/main/java/com/example/model/TransactionalEvent.java @@ -0,0 +1,18 @@ +package com.example.model; + +import lombok.Getter; +import org.springframework.context.ApplicationEvent; + +/** + * @author: shuguang.zhang + * @date : 2023-04-25 22:48 + */ +@Getter +public class TransactionalEvent extends ApplicationEvent { + + private T data; + + public TransactionalEvent(final T source) { + super(source); + } +} diff --git a/example/src/test/java/com/example/ExistsTransactionalRetryServiceTest.java b/example/src/test/java/com/example/ExistsTransactionalRetryServiceTest.java index 1f4acc3a1..4f3b5ef27 100644 --- a/example/src/test/java/com/example/ExistsTransactionalRetryServiceTest.java +++ b/example/src/test/java/com/example/ExistsTransactionalRetryServiceTest.java @@ -39,7 +39,7 @@ public class ExistsTransactionalRetryServiceTest { Mockito.when(remoteService.call()) .thenReturn(new Result(0, "1")) .thenReturn(new Result(0, "2")) - .thenReturn(new Result(0, "3")) + .thenReturn(new Result(3, "3")) .thenReturn(new Result(0, "4")) .thenReturn(new Result(0, "5")) ; @@ -63,10 +63,10 @@ public class ExistsTransactionalRetryServiceTest { .thenReturn(new Result(0, "2")) .thenReturn(new Result(0, "3")) .thenReturn(new Result(0, "4")) - .thenReturn(new Result(0, "5")) + .thenReturn(new Result(1, "5")) ; try { - String s = testExistsTransactionalRetryService2.testSimpleUpdate(243L); + String s = testExistsTransactionalRetryService2.testSimpleUpdate(5651L); System.out.println(s); } catch (Exception e) { log.error("", e); @@ -86,7 +86,7 @@ public class ExistsTransactionalRetryServiceTest { .thenReturn(new Result(0, "2")) .thenReturn(new Result(0, "3")) .thenReturn(new Result(0, "4")) - .thenReturn(new Result(0, "5")) + .thenReturn(new Result(5, "5")) ; try { for (int i = 0; i < 1000; i++) { diff --git a/example/src/test/java/com/example/NestMethodServiceTest.java b/example/src/test/java/com/example/NestMethodServiceTest.java index a846aeee0..0b21971b5 100644 --- a/example/src/test/java/com/example/NestMethodServiceTest.java +++ b/example/src/test/java/com/example/NestMethodServiceTest.java @@ -30,7 +30,7 @@ public class NestMethodServiceTest { .thenReturn(new Result(0, "2")) .thenReturn(new Result(0, "3")) .thenReturn(new Result(0, "4")) - .thenReturn(new Result(0, "5")) + .thenReturn(new Result(1, "5")) ; try { nestMethodService.testNestMethod();