import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author Cage Zhao
* @date 2022/2/28 14:30
* @description
*/
@Configuration
@ConfigurationProperties(prefix = "spring.cloud.stream.redis")
@Data
public class RedisProperties {
/**
* Database index used by the connection factory.
*/
private int database = 0;
/**
* Redis server host.
*/
private String host;
/**
* Login password of the redis server.
*/
private String password;
/**
* Redis server port.
*/
private int port;
// 其它参数略
}
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author Cage Zhao
* @date 2022/2/22 12:02
* @description
*/
@Configuration
@ConditionalOnClass({JedisConnection.class, RedisOperations.class, org.springframework.boot.autoconfigure.data.redis.RedisProperties.Jedis.class})
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {
@Bean
public LettuceConnectionFactory lettuceConnectionFactory(RedisProperties redisProperties) {
RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration(
redisProperties.getHost(), redisProperties.getPort()
);
// 设置选用的数据库号码
redisConfiguration.setDatabase(redisProperties.getDatabase());
// 设置 redis 数据库密码
redisConfiguration.setPassword(redisProperties.getPassword());
// 根据配置和客户端配置创建连接
LettuceConnectionFactory factory = new LettuceConnectionFactory((RedisConfiguration)redisConfiguration);
return factory;
}
/**
* redisTemplate 初始化
*
* @param lettuceConnectionFactory
* @return
*/
@Bean
@ConditionalOnBean(LettuceConnectionFactory.class)
public RedisTemplate<Object, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setKeySerializer(new StringRedisSerializer());
//jackson2JsonRedisSerializer就是JSON序列化规则,
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}