k8s部署单点Redis
(1)k8s部署redis的yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: redis
name: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: redis
spec:
containers:
- image: redis:5.0.5-alpine
imagePullPolicy: IfNotPresent
name: redis
resources: {}
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: redis
name: redis
spec:
ports:
- port: 6379
protocol: TCP
targetPort: 6379
selector:
app: redis
status:
loadBalancer: {}
(2)运行结果
SpringBoot整合Redis
(1)application.yaml文件配置redis连接地址
spring:
redis:
port: 31091
host: x.x.x.x
password:
(2)添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
(3)添加缓存
package gdut.controller;
import gdut.util.RedissionUtil;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
//添加缓存
@PostMapping("/createCache")
public String deployDefinition(@RequestBody Map<String, String> params) {
for(Map.Entry<String, String> entry : params.entrySet()) {
stringRedisTemplate.opsForValue().set(entry.getKey(), entry.getValue());
}
return "success";
}
}
(4)测试
http://localhost:8081/redis/createCache
{
“aaa”: “bbb”,
“1”:“2”
}
Redission实现延迟队列
(1)添加Maven依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.12.3</version>
</dependency>
(2)获取redis连接、堵塞队列、延迟队列
package gdut.util;
import org.redisson.Redisson;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.concurrent.BlockingDeque;
public class RedissionUtil {
private static RedissonClient redissonClient;
public static RedissonClient getRedssionClient() {
if(null == redissonClient) {
Config config = new Config();
config.useSingleServer()
.setAddress("redis://x.x.x.x:31091")
.setPingConnectionInterval(1000);
redissonClient = Redisson.create(config);
}
return redissonClient;
}
public static RDelayedQueue<String> getDelayedQueue() {
RedissonClient redissonClient = getRedssionClient();
RBlockingDeque<String> blockingDeque = redissonClient.getBlockingDeque("test");
RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
return delayedQueue;
}
public static RBlockingDeque<String> getBlockingQueue() {
RedissonClient redissonClient = getRedssionClient();
RBlockingDeque<String> blockingDeque = redissonClient.getBlockingDeque("test");
return blockingDeque;
}
}
(3)生产消息、消费消息
@PostMapping("/produceMessage")
public String produceMessage(@RequestParam("message") String message) {
RDelayedQueue<String> delayedQueue = RedissionUtil.getDelayedQueue();
delayedQueue.offer(message, 10, TimeUnit.SECONDS);
return "success";
}
@PostMapping("/consumeMessage")
public String consumeMessage() {
RBlockingDeque<String> blockingDeque = RedissionUtil.getBlockingQueue();
try {
String consumeMessage = blockingDeque.take();
System.out.println("consumeMessage is:" + consumeMessage);
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
(4)测试
http://localhost:8081/redis/produceMessage?message=testeee
http://localhost:8081/redis/consumeMessage
延迟队列
keys * 查看到存在key=redisson_delay_queue:{test}的缓存。
type redisson_delay_queue:{test} 查询出该key的类型是list。
lrange redisson_delay_queue:{test} 0 -1 查询出数组有的全部元素。当元素到达延迟时间,就会从数组中移除。
堵塞队列
test是我定义的堵塞队列的名称。Redisson直接用队列名称作为键。
type test 查看test的数据类型,查询出是list。
lrange test 0 -1 查询堵塞队列全部的元素。
当延迟队列和堵塞队列的list集合都没有元素的时候,Redisson会把对应的键删除。
参考
SpringBoot项目使用RedisTemplate设置序列化方式
浅析 Redisson 的分布式延时队列 RedissonDelayedQueue 运行流程
Redis server response timeout 3000 ms occured after 3 retry attempts. Command: EXISTS, params:
redis出现WRONGTYPE Operation against a key holding the wrong kind of value错误