From 2307c711a94951b9731f78b3d27b9930f59a2cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Sun, 20 Dec 2020 08:55:50 +0800 Subject: [PATCH] feat($Gateway): add reactive Redis support --- .../RedisClientConfiguration.java | 23 +++++++++++++++++++ .../configuration/RedisServiceImpl.java | 14 +++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/gateway/src/main/java/com/jmsoftware/maf/gateway/universal/configuration/RedisClientConfiguration.java b/gateway/src/main/java/com/jmsoftware/maf/gateway/universal/configuration/RedisClientConfiguration.java index 1630e0d3..f0279ffd 100644 --- a/gateway/src/main/java/com/jmsoftware/maf/gateway/universal/configuration/RedisClientConfiguration.java +++ b/gateway/src/main/java/com/jmsoftware/maf/gateway/universal/configuration/RedisClientConfiguration.java @@ -1,5 +1,6 @@ package com.jmsoftware.maf.gateway.universal.configuration; +import lombok.extern.slf4j.Slf4j; import lombok.val; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; @@ -7,9 +8,13 @@ import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.ReactiveRedisTemplate; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.io.Serializable; @@ -22,6 +27,7 @@ * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com * @date 5/2/20 11:41 PM **/ +@Slf4j @Configuration @EnableCaching @AutoConfigureAfter(RedisAutoConfiguration.class) @@ -31,10 +37,27 @@ public class RedisClientConfiguration extends CachingConfigurerSupport { */ @Bean public RedisTemplate redisFactory(LettuceConnectionFactory lettuceConnectionFactory) { + log.info("Initial bean: {}", RedisTemplate.class.getSimpleName()); val template = new RedisTemplate(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setConnectionFactory(lettuceConnectionFactory); return template; } + + /** + * Reactive redis template factory. + * + * @param factory the reactive redis connection factory + * @return the reactive redis template + */ + @Bean + ReactiveRedisTemplate reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) { + log.info("Initial bean: {}", ReactiveRedisTemplate.class.getSimpleName()); + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Serializable.class); + RedisSerializationContext.RedisSerializationContextBuilder builder = + RedisSerializationContext.newSerializationContext(new StringRedisSerializer()); + RedisSerializationContext context = builder.value(serializer).build(); + return new ReactiveRedisTemplate<>(factory, context); + } } diff --git a/gateway/src/main/java/com/jmsoftware/maf/gateway/universal/configuration/RedisServiceImpl.java b/gateway/src/main/java/com/jmsoftware/maf/gateway/universal/configuration/RedisServiceImpl.java index dc58c8a0..70a8c0c7 100644 --- a/gateway/src/main/java/com/jmsoftware/maf/gateway/universal/configuration/RedisServiceImpl.java +++ b/gateway/src/main/java/com/jmsoftware/maf/gateway/universal/configuration/RedisServiceImpl.java @@ -5,6 +5,7 @@ import cn.hutool.json.JSONUtil; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.redis.connection.RedisStringCommands; +import org.springframework.data.redis.core.ReactiveRedisTemplate; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.types.Expiration; @@ -27,9 +28,12 @@ @Service public class RedisServiceImpl implements RedisService { private final RedisTemplate redisTemplate; + private final ReactiveRedisTemplate reactiveRedisTemplate; - public RedisServiceImpl(@Qualifier("redisFactory") RedisTemplate redisTemplate) { + public RedisServiceImpl(@Qualifier("redisFactory") RedisTemplate redisTemplate, + @Qualifier("reactiveRedisTemplate") ReactiveRedisTemplate reactiveRedisTemplate) { this.redisTemplate = redisTemplate; + this.reactiveRedisTemplate = reactiveRedisTemplate; } @Override @@ -37,9 +41,9 @@ public Boolean set(String key, String value, Long expirationTime, TimeUnit timeU return redisTemplate.execute((RedisCallback) connection -> { RedisSerializer serializer = redisTemplate.getStringSerializer(); Boolean result = connection.set(Objects.requireNonNull(serializer.serialize(key)), - Objects.requireNonNull(serializer.serialize(value)), - Expiration.from(expirationTime, timeUnit), - RedisStringCommands.SetOption.upsert()); + Objects.requireNonNull(serializer.serialize(value)), + Expiration.from(expirationTime, timeUnit), + RedisStringCommands.SetOption.upsert()); return ObjectUtil.isNotNull(result) ? result : false; }); } @@ -55,7 +59,7 @@ public Boolean set(String key, String value) { return redisTemplate.execute((RedisCallback) connection -> { RedisSerializer serializer = redisTemplate.getStringSerializer(); Boolean result = connection.set(Objects.requireNonNull(serializer.serialize(key)), - Objects.requireNonNull(serializer.serialize(value))); + Objects.requireNonNull(serializer.serialize(value))); return ObjectUtil.isNotNull(result) ? result : false; }); }