1、在pom.xml文件中添加依赖:
2、创建resources/config/redis.properties配置文件
redis.properties文件内容如下:
#哨兵模式
#多个使用,分隔
redis.nodes=10.135.16.164:26379
redis.masterName=GMDCEQ-dev
redis.password=Haier,123
redis.maxTotal=10000
redis.maxIdle=100
redis.minIdle=50
redis.timeout=30000
3、创建RedisConfig.java,redis的配置类
package com.haier.gmdc.biprodweb.redisservice;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;
@Configuration
@PropertySource(value = {"classpath:config/redis.properties"},encoding = "UTF-8")
public class RedisConfig {
@Value("${redis.nodes}")
private String nodes;
@Value("${redis.masterName}")
private String masterName;
@Value("${redis.password}")
private String password;
@Value("${redis.maxTotal}")
private int maxTotal;
@Value("${redis.maxIdle}")
private int maxIdle;
@Value("${redis.minIdle}")
private int minIdle;
@Value("${redis.timeout}")
private int timeout;
/**
* @desc 初始化连接池配置对象
* @author jiaxuebing
* @date 20190520 16:00
* @return redis.clients.jedis.JedisPoolConfig
*/
@Bean(name = "jedisPoolConfig")
public JedisPoolConfig getJedisPoolConfig(){
JedisPoolConfig config = new JedisPoolConfig();
//最大总量
config.setMaxTotal(maxTotal);
//设置最大空闲数量
config.setMaxIdle(maxIdle);
//设置最小空闲数量
config.setMinIdle(minIdle);
//常规配置
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
return config;
}
/**
* @desc 哨兵模式配置【配置RedisSentinelConfiguration】
* @return
*/
@Bean(name = "redisSentinelConfiguration")
public RedisSentinelConfiguration getRedisSentinelConfiguration(){
RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
//配置redis的sentinel
Set
String [] subNodes = nodes.split(",");
if(subNodes!=null && subNodes.length>0){
int len = subNodes.length;
for(int i=0;i String [] subSentinel = subNodes[i].split(":"); RedisNode redisNode = new RedisNode(subSentinel[0],Integer.parseInt(subSentinel[1])); redisNodeSet.add(redisNode); } } redisSentinelConfiguration.setSentinels(redisNodeSet); redisSentinelConfiguration.setMaster(masterName); return redisSentinelConfiguration; } /** * @desc 创建redis连接工厂 * @param jedisPoolConfig * @param redisSentinelConfiguration * @return */ @Bean(name = "jedisConnectionFactory") public JedisConnectionFactory getJedisConnectionFactory(@Qualifier("jedisPoolConfig")JedisPoolConfig jedisPoolConfig, @Qualifier("redisSentinelConfiguration")RedisSentinelConfiguration redisSentinelConfiguration){ JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration, jedisPoolConfig); jedisConnectionFactory.setPassword(password); return jedisConnectionFactory; } /** * @desc 实例化redisTemplate * @param jedisConnectionFactory * @return */ @Bean(name = "redisTemplate") public RedisTemplate RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(jedisConnectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key采用String的序列化方式 redisTemplate.setKeySerializer(stringRedisSerializer); // hash的key也采用String的序列化方式 redisTemplate.setHashKeySerializer(stringRedisSerializer); // value采用jackson序列化方式 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); // hash的value采用jackson序列化方式 redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } } 4、总结一下:redis的集成【使用jedis】 (1)创建JedisPoolConfig的配置类 (2)哨兵:创建RedisSentinelConfiguration的配置类 集群:创建RedisClusterConfiguration的配置类 (3)创建JedisConnectionFactory配置 (4)实例化RedisTemplate或者直接使用jedis