diff --git a/yanchiduilie/pom.xml b/yanchiduilie/pom.xml new file mode 100644 index 0000000..d51c10c --- /dev/null +++ b/yanchiduilie/pom.xml @@ -0,0 +1,151 @@ + + + + + + 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.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-aop + + + + 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/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java b/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java new file mode 100644 index 0000000..86e3113 --- /dev/null +++ b/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java @@ -0,0 +1,18 @@ +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.scheduling.annotation.EnableAsync; + +@SpringBootApplication +@EnableFeignClients +@EnableAsync +public class TLogFeignConsumerRunner { + + public static void main(String[] args) { + SpringApplication.run(TLogFeignConsumerRunner.class, args); + + } +} diff --git a/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/configuration/MyRedissonConfig.java b/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/configuration/MyRedissonConfig.java new file mode 100644 index 0000000..134f451 --- /dev/null +++ b/yanchiduilie/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/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/configuration/RedisConfig.java b/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/configuration/RedisConfig.java new file mode 100644 index 0000000..7e5261a --- /dev/null +++ b/yanchiduilie/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/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/controller/PayController.java b/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/controller/PayController.java new file mode 100644 index 0000000..f2c4e21 --- /dev/null +++ b/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/controller/PayController.java @@ -0,0 +1,33 @@ +package com.yomahub.tlog.example.feign.controller; + + +import com.yomahub.tlog.example.feign.service.RedissonDelayQueue; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 我们用Redisson实现一个优雅的延迟队列。 + */ +@Slf4j +@RestController +public class PayController { + + + @Autowired + RedissonDelayQueue redissonDelayQueue; + + + + @GetMapping("/sendMessage") + public String getToken() { + redissonDelayQueue.offerTask("hello,world",5); + return "ok"; + } + + + +} diff --git a/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/service/RedissonDelayQueue.java b/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/service/RedissonDelayQueue.java new file mode 100644 index 0000000..379d5d6 --- /dev/null +++ b/yanchiduilie/src/main/java/com/yomahub/tlog/example/feign/service/RedissonDelayQueue.java @@ -0,0 +1,50 @@ +package com.yomahub.tlog.example.feign.service; + + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.redisson.api.RBlockingQueue; +import org.redisson.api.RDelayedQueue; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +@Slf4j +@Component +@RequiredArgsConstructor +public class RedissonDelayQueue { + + @Autowired + private RDelayedQueue delayedQueue; + + @Autowired + private RBlockingQueue blockingQueue; + + + @PostConstruct + public void init() { + ExecutorService executorService = Executors.newFixedThreadPool(1); + executorService.submit(() -> { + while (true) { + try { + String task = blockingQueue.take(); + log.info("receive delay task:{}", task); + } catch (Exception e) { + log.error("occur error", e); + } + } + }); + } + + public void offerTask(String task, long seconds) { + log.info("add delay task:{},delay time:{}s", task, seconds); + delayedQueue.offer(task, seconds, TimeUnit.SECONDS); + } + + +} + diff --git a/yanchiduilie/src/main/resources/application.properties b/yanchiduilie/src/main/resources/application.properties new file mode 100644 index 0000000..7883289 --- /dev/null +++ b/yanchiduilie/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/yanchiduilie/src/main/resources/rateLimiter.lua b/yanchiduilie/src/main/resources/rateLimiter.lua new file mode 100644 index 0000000..362e0cc --- /dev/null +++ b/yanchiduilie/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/yanchiduilie/tlog-example-logback-feign-consumer.iml b/yanchiduilie/tlog-example-logback-feign-consumer.iml new file mode 100644 index 0000000..5735b3a --- /dev/null +++ b/yanchiduilie/tlog-example-logback-feign-consumer.iml @@ -0,0 +1,162 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file