feat(商城): lettuce添加保活配置

master
wayn 2 years ago
parent b82e18a4b3
commit 2361720a15

@ -20,18 +20,10 @@ spring:
port: 6379
password:
database: 2
timeout: 5000
timeout: 300
expire: 400
lettuce:
shutdown-timeout: 100ms
# cluster:
# nodes:
# - 127.0.0.1:1681
# - 127.0.0.1:1682
# - 127.0.0.1:1683
# - 127.0.0.1:1684
# - 127.0.0.1:1685
# - 127.0.0.1:1686
#配置rabbitMq 服务器
rabbitmq:
host: 127.0.0.1

@ -20,6 +20,10 @@
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
</dependencies>
</project>

@ -1,12 +1,11 @@
package com.wayn.data.redis.config;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring.data.redis.FastJsonRedisSerializer;
import com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer;
import com.wayn.data.redis.constant.CacheConstants;
import io.lettuce.core.ClientOptions;
import io.lettuce.core.SocketOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
@ -16,6 +15,7 @@ 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.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
@ -43,6 +43,29 @@ public class CacheConfig extends CachingConfigurerSupport {
return redisTemplate;
}
/**
* lettuce
*
* @return LettuceClientConfigurationBuilderCustomizer
*/
@Bean
public LettuceClientConfigurationBuilderCustomizer lettuceClientConfigurationBuilderCustomizer() {
return clientConfigurationBuilder -> {
LettuceClientConfiguration clientConfiguration = clientConfigurationBuilder.build();
ClientOptions clientOptions = clientConfiguration.getClientOptions().orElseGet(ClientOptions::create);
ClientOptions build = clientOptions.mutate().build();
SocketOptions.KeepAliveOptions.Builder builder = build.getSocketOptions().getKeepAlive().mutate();
builder.enable(true);
builder.idle(Duration.ofSeconds(30));
SocketOptions.Builder socketOptionsBuilder = clientOptions.getSocketOptions().mutate();
SocketOptions.KeepAliveOptions keepAliveOptions = builder.build();
socketOptionsBuilder.keepAlive(keepAliveOptions);
SocketOptions socketOptions = socketOptionsBuilder.build();
ClientOptions clientOptions1 = ClientOptions.builder().socketOptions(socketOptions).build();
clientConfigurationBuilder.clientOptions(clientOptions1);
};
}
private RedisSerializer<String> keySerializer() {
return new StringRedisSerializer();
}

@ -92,33 +92,10 @@ public class RedisCache {
* @return
*/
public <T> T getCacheObject(final String key) {
try {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
return retryGetCacheObject(key, 1);
}
}
public <T> T retryGetCacheObject(final String key, int retryCount) {
try {
log.info("retryGetCacheObject, key:{}, retryCount:{}", key, retryCount);
if (retryCount <= 0) {
return null;
}
lettuceConnectionFactory.resetConnection();
Thread.sleep(200L);
retryCount--;
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
return retryGetCacheObject(key, retryCount);
}
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
/**
* key
*

@ -22,14 +22,14 @@
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logPath}/run.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<!--<filter class="ch.qos.logback.classic.filter.LevelFilter">
&lt;!&ndash; 过滤的级别 &ndash;&gt;
<level>INFO</level>
<!-- 匹配时的操作:接收(记录) -->
&lt;!&ndash; 匹配时的操作:接收(记录) &ndash;&gt;
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
&lt;!&ndash; 不匹配时的操作:拒绝(不记录) &ndash;&gt;
<onMismatch>DENY</onMismatch>
</filter>
</filter>-->
</appender>
<!-- 只记录SQL日志 -->
<appender name="SQL" class="ch.qos.logback.core.rolling.RollingFileAppender">
@ -43,7 +43,7 @@
</appender>
<logger name="org.springframework" level="INFO"/>
<logger name="io.lettuce.core" level="ERROR"/>
<logger name="io.lettuce.core" level="DEBUG"/>
<!-- logger为java中的包 -->
<logger name="com.wayn.common.core.mapper" level="DEBUG" activity="false">
<appender-ref ref="SQL"/>

Loading…
Cancel
Save