Browse Source

add prod.yml

jiequan.bi 5 years ago
parent
commit
c7560c668b

+ 4 - 0
pom.xml

@@ -34,6 +34,10 @@
             <optional>true</optional>
         </dependency>
         <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-pool2</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <optional>true</optional>

+ 63 - 26
src/main/java/com/hcst/youteng/config/RedisConfig.java

@@ -1,43 +1,80 @@
 package com.hcst.youteng.config;
 
-import com.fasterxml.jackson.annotation.JsonAutoDetect;
-import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
 import com.fasterxml.jackson.annotation.PropertyAccessor;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import lombok.extern.slf4j.Slf4j;
+import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
+import org.springframework.cache.CacheManager;
+import org.springframework.cache.annotation.CachingConfigurerSupport;
 import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cache.interceptor.KeyGenerator;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.cache.RedisCacheConfiguration;
+import org.springframework.data.redis.cache.RedisCacheManager;
+import org.springframework.data.redis.cache.RedisCacheWriter;
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.RedisSerializer;
 import org.springframework.data.redis.serializer.StringRedisSerializer;
 
-/**
- * Created by JQ.bi on 2020.8.28
- */
+import javax.annotation.Resource;
+import java.lang.reflect.Method;
+import java.time.Duration;
+import java.util.Arrays;
+
 @Configuration
-@Slf4j
 @EnableCaching
-public class RedisConfig {
+public class RedisConfig extends CachingConfigurerSupport {
 
-   @Bean
-   public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
-       RedisTemplate<String, Object> template = new RedisTemplate<>();
-       template.setConnectionFactory(factory);
+    @Resource
+    private LettuceConnectionFactory lettuceConnectionFactory;
 
-       StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
-       template.setKeySerializer(stringRedisSerializer);
-       template.setHashKeySerializer(stringRedisSerializer);
+    /**
+     * @description 自定义的缓存key的生成策略 若想使用这个key
+     *              只需要讲注解上keyGenerator的值设置为keyGenerator即可</br>
+     * @return 自定义策略生成的key
+     */
+    @Override
+    @Bean
+    public KeyGenerator keyGenerator() {
+        return new KeyGenerator() {
+            @Override
+            public Object generate(Object target, Method method, Object... params) {
+                StringBuilder sb = new StringBuilder();
+                sb.append(target.getClass().getName());
+                sb.append(method.getDeclaringClass().getName());
+                Arrays.stream(params).map(Object::toString).forEach(sb::append);
+                return sb.toString();
+            }
+        };
+    }
 
-       Jackson2JsonRedisSerializer<Object> jacksonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
-       ObjectMapper om = new ObjectMapper();
-       om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
-       om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
-       om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
-       jacksonRedisSerializer.setObjectMapper(om);
-       template.setValueSerializer(jacksonRedisSerializer);
-       template.setHashValueSerializer(jacksonRedisSerializer);
-       return template;
-   }
+    /**
+     * RedisTemplate配置
+     */
+    @Bean
+    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
+        // 设置序列化
+        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
+        ObjectMapper om = new ObjectMapper();
+        om.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
+        om.enableDefaultTyping(DefaultTyping.NON_FINAL);
+        jackson2JsonRedisSerializer.setObjectMapper(om);
+        // 配置redisTemplate
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
+        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
+        RedisSerializer<?> stringSerializer = new StringRedisSerializer();
+        // key序列化
+        redisTemplate.setKeySerializer(stringSerializer);
+        // value序列化
+        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
+        // Hash key序列化
+        redisTemplate.setHashKeySerializer(stringSerializer);
+        // Hash value序列化
+        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
+        redisTemplate.afterPropertiesSet();
+        return redisTemplate;
+    }
 }

+ 10 - 1
src/main/resources/application-prod.yml

@@ -1,7 +1,16 @@
 spring:
   redis:
-    url: redis://172.30.0.17:6379
+    database: 0
+    host: 127.0.0.1
+    port: 6379
     password: foobared
+    lettuce:
+      pool:
+        max-active: 8   #最大连接数据库连接数,设 0 为没有限制
+        max-idle: 8     #最大等待连接中的数量,设 0 为没有限制
+        max-wait: -1ms  #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
+        min-idle: 0     #最小等待连接中的数量,设 0 为没有限制
+      shutdown-timeout: 1000ms
   datasource:
     url: jdbc:mysql://139.186.27.96/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false
     username: hcst