一、Pipeline简介
管道(pipeline)可以一次性发送多条命令并在执行完后一次性将结果返回,pipeline 通过减少客户端与 redis 的通信次数来实现降低往返延时时间,而且 Pipeline 实现的原理是队列,而队列的原理是时先进先出,这样就保证数据的顺序性。
二、Redistemplate简介
RestTemplate是Spring提供的用于访问Rest服务的客户端。
三、Redistemplate单节点操作Pipeline
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.util.List;
public class JedisClusterUtil {
private RedisTemplate init() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//线程最大空闲数
jedisPoolConfig.setMaxIdle(20);
//线程总数
jedisPoolConfig.setMaxTotal(20);
//线程最小空闲数
jedisPoolConfig.setMinIdle(20);
//等待线程最长时间
jedisPoolConfig.setMaxWaitMillis(200);
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(jedisPoolConfig);
//获取节点的配置类
RedisStandaloneConfiguration configuration = connectionFactory.getStandaloneConfiguration();
configuration.setHostName("127.0.0.1");
configuration.setPort(6379);
//调用此方法。此配置才能生效
connectionFactory.afterPropertiesSet();
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setDefaultSerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
private List<String> test() {
try {
RedisTemplate redisTemplate = init();
return redisTemplate.executePipelined((RedisConnection connection) -> {
//这里我只用了get命令示范,其它命令也可以
connection.get("test".getBytes());
connection.get("test1".getBytes());
connection.get("test2".getBytes());
return null;
});
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
此方法只能用于调用redis单节点,如果调用集群中的其它节点,就会报redis.clients.jedis.exceptions.JedisMovedDataException: MOVED 1539 127.0.0.1:6379
看了其他文章的解决方法,都要把各个节点的ip和slot存储在本地缓存,等到需要用到时,再调用jedis去连接;
四、Redistemplate集群操作Pipeline
private RedisTemplate init() {
RedisClusterConfiguration configuration = new RedisClusterConfiguration();
List<RedisNode> list = new ArrayList<>();
list.add(new RedisNode("127.0.0.1", 6379));
list.add(new RedisNode("127.0.0.2", 6379));
//设置节点ip,集群的情况下,使用域名就行
configuration.setClusterNodes(list);
configuration.setMaxRedirects(2);
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
//线程最大空闲数
config.setMaxIdle(20);
//线程总数
config.setMaxTotal(20);
//线程最小空闲数
config.setMinIdle(20);
//等待线程最长时间
config.setMaxWaitMillis(200);
LettucePoolingClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, clientConfiguration);
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setDefaultSerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
private List<String> test() {
try {
RedisTemplate redisTemplate = init();
return redisTemplate.executePipelined((RedisConnection connection) -> {
connection.get("test".getBytes());
connection.get("test1".getBytes());
connection.get("test2".getBytes());
return null;
});
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
五、pom.xml的引入
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>