引入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.3.1</version>
</dependency>
编写相关代码
@Test
void contextLoads() {
Jedis jedis = new Jedis("192.168.32.175", 6379);
// value
jedis.set("k1","v1");
// list
jedis.lpush("k2","v2","v3");
// hashmap
Map<String,String> map = new HashMap<>();
map.put("name","pm");
map.put("age","22");
map.put("sex","男");
jedis.hset("k3",map);
jedis.close();
}
每次使用jedis对象时 都需要自己创建,当使用完后,需要关闭该对象。
jebis连接池的配置及使用
@Test
public void testPool(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(10);//配置最多连接的个数
jedisPoolConfig.setMaxIdle(10);// 最多空闲的连接个数
jedisPoolConfig.setMinIdle(2);// 最少空闲的个数
jedisPoolConfig.setTestOnBorrow(true);//获取连接对象时是否检擦连通性
JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.32.175", 6379);
Jedis jedis = jedisPool.getResource();
jedis.flushDB();
jedis.set("k1","v1");
String k1 = jedis.get("k1");
System.out.println(k1);
jedis.close();// 归还给连接池
}
统一设置redisTemplate的序列化
package pm.redis1.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
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;
/**
* @program: redis1
* @description:
* @author: 平家祥
* @create: 2023-04-25 09:57
**/
@Configuration
public class RedisConfig {
@Bean //该方法的返回对象交于spring容器管理
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
//field序列化 key field value
template.setHashKeySerializer(redisSerializer);
return template;
}
}
springboot连接集群
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.lettuce.pool.min-idle=0
# 设置redis重定向的次数---根据主节点的个数
spring.redis.cluster.max-redirects=3
spring.redis.cluster.nodes=192.168.32.175:7001,192.168.32.175:7002,192.168.32.175:7003,192.168.32.175:7004,192.168.32.175:7005,192.168.32.175:7006