From 9db8142a1a8c673930dc433bb0fcec4d3efa262e Mon Sep 17 00:00:00 2001
From: XinYi Song <2037158277@qq.com>
Date: Fri, 11 Feb 2022 09:06:19 +0800
Subject: [PATCH] saddfaasdvc
---
pom.xml | 4 +-
.../config/RedisCacheAutoConfiguration.java | 30 +
.../com/xkrs/common/config/RedisConfig.java | 187 ++++++
.../com/xkrs/common/config/RedisUtil.java | 533 ++++++++++++++++++
.../common/schedule/SaticScheduleTask.java | 33 ++
.../com/xkrs/controller/FireController.java | 29 +
.../com/xkrs/controller/TestController.java | 1 +
src/main/java/com/xkrs/dao/FireDao.java | 9 +
.../java/com/xkrs/service/FireService.java | 15 +
.../com/xkrs/service/impl/FireServerImpl.java | 46 ++
.../com/xkrs/util/TopicConsumerListener.java | 2 +
src/main/resources/application.properties | 22 +-
12 files changed, 898 insertions(+), 13 deletions(-)
create mode 100644 src/main/java/com/xkrs/common/config/RedisCacheAutoConfiguration.java
create mode 100644 src/main/java/com/xkrs/common/config/RedisConfig.java
create mode 100644 src/main/java/com/xkrs/common/config/RedisUtil.java
create mode 100644 src/main/java/com/xkrs/common/schedule/SaticScheduleTask.java
create mode 100644 src/main/java/com/xkrs/controller/FireController.java
create mode 100644 src/main/java/com/xkrs/service/FireService.java
create mode 100644 src/main/java/com/xkrs/service/impl/FireServerImpl.java
diff --git a/pom.xml b/pom.xml
index ee0f15b..a97df26 100644
--- a/pom.xml
+++ b/pom.xml
@@ -38,10 +38,10 @@
org.springframework.boot
spring-boot-starter-aop
-
+
org.springframework.boot
spring-boot-starter-web
diff --git a/src/main/java/com/xkrs/common/config/RedisCacheAutoConfiguration.java b/src/main/java/com/xkrs/common/config/RedisCacheAutoConfiguration.java
new file mode 100644
index 0000000..6a0970b
--- /dev/null
+++ b/src/main/java/com/xkrs/common/config/RedisCacheAutoConfiguration.java
@@ -0,0 +1,30 @@
+package com.xkrs.common.config;
+
+import org.springframework.boot.autoconfigure.AutoConfigureAfter;
+import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+import java.io.Serializable;
+
+/**
+ * Redis 缓存自动配置
+ * @author tajochen
+ */
+@Configuration
+@AutoConfigureAfter(RedisAutoConfiguration.class)
+public class RedisCacheAutoConfiguration {
+
+ @Bean
+ public RedisTemplate redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
+ RedisTemplate template = new RedisTemplate<>();
+ template.setKeySerializer(new StringRedisSerializer());
+ template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
+ template.setConnectionFactory(redisConnectionFactory);
+ return template;
+ }
+}
diff --git a/src/main/java/com/xkrs/common/config/RedisConfig.java b/src/main/java/com/xkrs/common/config/RedisConfig.java
new file mode 100644
index 0000000..fb6641d
--- /dev/null
+++ b/src/main/java/com/xkrs/common/config/RedisConfig.java
@@ -0,0 +1,187 @@
+package com.xkrs.common.config;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.cache.CacheManager;
+import org.springframework.cache.annotation.CachingConfigurerSupport;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cache.interceptor.CacheErrorHandler;
+import org.springframework.cache.interceptor.CacheResolver;
+import org.springframework.cache.interceptor.KeyGenerator;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.cache.RedisCacheConfiguration;
+import org.springframework.data.redis.cache.RedisCacheManager;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.*;
+
+import javax.annotation.Resource;
+import java.io.IOException;
+import java.time.Duration;
+
+/**
+ * redis配置
+ * @author tajochen
+ */
+@Configuration
+@EnableCaching
+public class RedisConfig extends CachingConfigurerSupport{
+
+ private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
+
+ @Resource
+ private LettuceConnectionFactory lettuceConnectionFactory;
+
+ private static final Duration TIME_TO_LIVE = Duration.ofSeconds(3600*6);
+
+ @Bean
+ @Override
+ public KeyGenerator keyGenerator() {
+
+ // 设置自动key的生成规则,配置spring boot的注解,进行方法级别的缓存
+ return (target, method, params) -> {
+ StringBuilder sb = new StringBuilder();
+ sb.append(target.getClass().getName());
+ sb.append(":");
+ sb.append(method.getName());
+ for (Object obj : params) {
+ sb.append(":").append(obj);
+ }
+ // logger.info("自动生成Redis Key -> [{}]", rsToUse);
+ return String.valueOf(sb);
+ };
+ }
+
+ @Bean
+ @Override
+ public CacheManager cacheManager() {
+ // 关键点,spring cache的注解使用的序列化都从这来,没有这个配置的话使用的jdk自己的序列化
+ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
+ //key序列化方式
+ .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
+ //value序列化方式
+ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()))
+ .disableCachingNullValues()
+ .entryTtl(TIME_TO_LIVE);
+
+ RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder
+ .fromConnectionFactory(lettuceConnectionFactory)
+ .cacheDefaults(config)
+ .transactionAware();
+
+ return builder.build();
+ }
+
+
+ /**
+ * RedisTemplate配置 在单独使用redisTemplate的时候 重新定义序列化方式
+ */
+ @Bean(name = "redisTemplate")
+ public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
+ // 设置序列化
+ Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
+ ObjectMapper om = new ObjectMapper();
+ // 配置null值序列化成空字符串
+ om.getSerializerProvider().setNullValueSerializer(new JsonSerializer<>() {
+ @Override
+ public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
+ jsonGenerator.writeString("");
+ }
+ });
+ // 解决jackson无法反序列化LocalDateTime的问题,引入jsr310标准
+ JavaTimeModule javaTimeModule = new JavaTimeModule();
+ om.registerModule(javaTimeModule);
+ om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+ // 其他设置
+ om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+ om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,
+ ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
+ jackson2JsonRedisSerializer.setObjectMapper(om);
+ // 配置redisTemplate
+ RedisTemplate redisTemplate = new RedisTemplate<>();
+ redisTemplate.setConnectionFactory(lettuceConnectionFactory);
+ RedisSerializer> stringSerializer = new StringRedisSerializer();
+ redisTemplate.setKeySerializer(stringSerializer);
+ redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
+ redisTemplate.setHashKeySerializer(stringSerializer);
+ redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
+ redisTemplate.afterPropertiesSet();
+ return redisTemplate;
+ }
+
+ private RedisSerializer keySerializer() {
+ return new StringRedisSerializer();
+ }
+
+ private RedisSerializer