From 0a612cd01b2ebb7108d719a087a9345f80244eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?= <8569561+fuqiangma@user.noreply.gitee.com> Date: Fri, 17 Mar 2023 21:50:30 +0800 Subject: [PATCH] =?UTF-8?q?springRetry=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springRetry/pom.xml | 158 +++++++++++++++++ .../feign/TLogFeignConsumerRunner.java | 20 +++ .../feign/configuration/MyRedissonConfig.java | 45 +++++ .../feign/configuration/RedisConfig.java | 33 ++++ .../feign/controller/PayController.java | 26 +++ .../feign/service/TestRetryService.java | 35 ++++ .../src/main/resources/application.properties | 21 +++ .../src/main/resources/rateLimiter.lua | 16 ++ .../tlog-example-logback-feign-consumer.iml | 167 ++++++++++++++++++ 9 files changed, 521 insertions(+) create mode 100644 springRetry/pom.xml create mode 100644 springRetry/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java create mode 100644 springRetry/src/main/java/com/yomahub/tlog/example/feign/configuration/MyRedissonConfig.java create mode 100644 springRetry/src/main/java/com/yomahub/tlog/example/feign/configuration/RedisConfig.java create mode 100644 springRetry/src/main/java/com/yomahub/tlog/example/feign/controller/PayController.java create mode 100644 springRetry/src/main/java/com/yomahub/tlog/example/feign/service/TestRetryService.java create mode 100644 springRetry/src/main/resources/application.properties create mode 100644 springRetry/src/main/resources/rateLimiter.lua create mode 100644 springRetry/tlog-example-logback-feign-consumer.iml diff --git a/springRetry/pom.xml b/springRetry/pom.xml new file mode 100644 index 0000000..be5f52c --- /dev/null +++ b/springRetry/pom.xml @@ -0,0 +1,158 @@ + + + + + + 8 + 8 + 2.1.8.RELEASE + Greenwich.SR3 + 2.1.0.RELEASE + 1.8 + 1.7.25 + 2.12.0 + 1.2.17 + 1.6.1 + 2.7.6 + 2.8.4 + 4.0.1 + 3.4.6 + 0.1 + 1.0.0 + 1.5.0 + + + 4.0.0 + com.yomahub.tlog.example + tlog-example-logback-feign-consumer + 1.0 + + + + + org.springframework.retry + spring-retry + + + + org.springframework.boot + spring-boot-starter-aop + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-logging + + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + + + + org.redisson + redisson + 3.12.0 + + + + + com.google.guava + guava + 30.1-jre + + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + org.springframework.boot + spring-boot-starter-test + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + + + + + com.yomahub + tlog-feign-spring-boot-starter + ${tlog.version} + + + com.yomahub + tlog-web-spring-boot-starter + ${tlog.version} + + + org.projectlombok + lombok + + + junit + junit + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + + log4j + log4j + ${log4j.version} + + + org.slf4j + slf4j-api + ${slf4j.version} + + + + + + diff --git a/springRetry/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java b/springRetry/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java new file mode 100644 index 0000000..b0d5921 --- /dev/null +++ b/springRetry/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java @@ -0,0 +1,20 @@ +package com.yomahub.tlog.example.feign; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.retry.annotation.EnableRetry; +import org.springframework.scheduling.annotation.EnableAsync; + +@SpringBootApplication +@EnableFeignClients +@EnableAsync +@EnableRetry +public class TLogFeignConsumerRunner { + + public static void main(String[] args) { + SpringApplication.run(TLogFeignConsumerRunner.class, args); + + } +} diff --git a/springRetry/src/main/java/com/yomahub/tlog/example/feign/configuration/MyRedissonConfig.java b/springRetry/src/main/java/com/yomahub/tlog/example/feign/configuration/MyRedissonConfig.java new file mode 100644 index 0000000..134f451 --- /dev/null +++ b/springRetry/src/main/java/com/yomahub/tlog/example/feign/configuration/MyRedissonConfig.java @@ -0,0 +1,45 @@ +package com.yomahub.tlog.example.feign.configuration; + + +import org.redisson.Redisson; +import org.redisson.api.RBlockingQueue; +import org.redisson.api.RDelayedQueue; +import org.redisson.api.RedissonClient; +import org.redisson.config.Config; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.io.IOException; + +/** + * @Description: + * @Created: with IntelliJ IDEA. + * @author: woniu + * @createTime: 2023-03-11 09:39 + **/ + +@Configuration +public class MyRedissonConfig { + + @Bean(destroyMethod = "shutdown") + public RedissonClient redisson() throws IOException { + Config config = new Config(); + config.useSingleServer().setAddress("redis://192.168.56.10:6379").setPassword("123456"); + RedissonClient redissonClient = Redisson.create(config); + return redissonClient; + } + + + @Bean + public RBlockingQueue blockingQueue(RedissonClient redissonClient) { + return redissonClient.getBlockingQueue("TOKEN-RENEWAL"); + } + + @Bean + public RDelayedQueue delayedQueue(RBlockingQueue blockingQueue, + RedissonClient redissonClient) { + return redissonClient.getDelayedQueue(blockingQueue); + } +} + + diff --git a/springRetry/src/main/java/com/yomahub/tlog/example/feign/configuration/RedisConfig.java b/springRetry/src/main/java/com/yomahub/tlog/example/feign/configuration/RedisConfig.java new file mode 100644 index 0000000..7e5261a --- /dev/null +++ b/springRetry/src/main/java/com/yomahub/tlog/example/feign/configuration/RedisConfig.java @@ -0,0 +1,33 @@ +package com.yomahub.tlog.example.feign.configuration; + + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * RedisConfig + * @date 2023/3/10 21:43 + */ +@Configuration +public class RedisConfig { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(factory); + // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Object.class); + template.setValueSerializer(serializer); + // 使用StringRedisSerializer来序列化和反序列化redis的key值 + template.setKeySerializer(new StringRedisSerializer()); + template.afterPropertiesSet(); + + return template; + } + +} + diff --git a/springRetry/src/main/java/com/yomahub/tlog/example/feign/controller/PayController.java b/springRetry/src/main/java/com/yomahub/tlog/example/feign/controller/PayController.java new file mode 100644 index 0000000..4144b87 --- /dev/null +++ b/springRetry/src/main/java/com/yomahub/tlog/example/feign/controller/PayController.java @@ -0,0 +1,26 @@ +package com.yomahub.tlog.example.feign.controller; + + +import com.yomahub.tlog.example.feign.service.TestRetryService; +import lombok.extern.slf4j.Slf4j; +import org.checkerframework.checker.units.qual.A; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +@Slf4j +@RestController +public class PayController { + + + @Autowired + TestRetryService testRetryService; + + @GetMapping("/addCode/{code}") + public String addCode(@PathVariable int code) throws Exception { + return testRetryService.test(code); + } + + +} diff --git a/springRetry/src/main/java/com/yomahub/tlog/example/feign/service/TestRetryService.java b/springRetry/src/main/java/com/yomahub/tlog/example/feign/service/TestRetryService.java new file mode 100644 index 0000000..c0c988f --- /dev/null +++ b/springRetry/src/main/java/com/yomahub/tlog/example/feign/service/TestRetryService.java @@ -0,0 +1,35 @@ +package com.yomahub.tlog.example.feign.service; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.retry.annotation.Backoff; +import org.springframework.retry.annotation.Recover; +import org.springframework.retry.annotation.Retryable; +import org.springframework.stereotype.Service; + +import java.time.LocalTime; + +/** + * 一个 Spring注解轻松搞定循环重试功能! + */ +@Service +@Slf4j +public class TestRetryService { + + @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1.5)) + public String test(int code) throws Exception { + log.info("test被调用,时间:" + LocalTime.now()); + if (code == 0) { + throw new Exception("调用失败啦!"); + } + log.info("test被调用,真材实料!"); + + return "200"; + } + + @Recover + public String recover(Exception e, int code) { + log.info("回调方法执行!!!!"); + log.info("{}", e.getMessage()); + return "400"; + } +} diff --git a/springRetry/src/main/resources/application.properties b/springRetry/src/main/resources/application.properties new file mode 100644 index 0000000..7883289 --- /dev/null +++ b/springRetry/src/main/resources/application.properties @@ -0,0 +1,21 @@ +spring.application.name=tlog-logback-feign-consumer +server.port=3111 +#eureka.client.service-url.defaultZone=http://127.0.0.1:1111/eureka/ + +tlog.pattern=[$currIp][$spanId][$traceId] +#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 + +http.connectTimeOutMillis=8000 +http.readTimeOutMillis=8000 + +spring.task.execution.pool.core-size=5 +spring.task.execution.pool.max-size=50 +spring.task.execution.pool.queue-capacity=200 +spring.task.execution.thread-name-prefix=woniugege- + + +spring.redis.host=192.168.56.10 +spring.redis.password=123456 +spring.redis.port=6379 + + diff --git a/springRetry/src/main/resources/rateLimiter.lua b/springRetry/src/main/resources/rateLimiter.lua new file mode 100644 index 0000000..362e0cc --- /dev/null +++ b/springRetry/src/main/resources/rateLimiter.lua @@ -0,0 +1,16 @@ +--获取KEY +local key = KEYS[1] + +local limit = tonumber(ARGV[1]) + +local curentLimit = tonumber(redis.call('get', key) or "0") + +if curentLimit + 1 > limit +then return 0 +else + -- 自增长 1 + redis.call('INCRBY', key, 1) + -- 设置过期时间 + redis.call('EXPIRE', key, ARGV[2]) + return curentLimit + 1 +end \ No newline at end of file diff --git a/springRetry/tlog-example-logback-feign-consumer.iml b/springRetry/tlog-example-logback-feign-consumer.iml new file mode 100644 index 0000000..b8da358 --- /dev/null +++ b/springRetry/tlog-example-logback-feign-consumer.iml @@ -0,0 +1,167 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file