2021-09-03 16:22:04 +08:00
|
|
|
package com.ruoyi.demo.controller;
|
|
|
|
|
2022-01-29 11:48:41 +08:00
|
|
|
import com.ruoyi.common.core.domain.R;
|
2021-12-23 14:54:01 +08:00
|
|
|
import com.ruoyi.common.utils.redis.RedisUtils;
|
2022-07-07 18:08:14 +08:00
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
2021-09-03 16:22:04 +08:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redis 发布订阅 演示案例
|
|
|
|
*
|
|
|
|
* @author Lion Li
|
|
|
|
*/
|
2022-07-07 18:08:14 +08:00
|
|
|
@Tag(name ="Redis发布订阅 演示案例", description = "Redis发布订阅")
|
2022-01-16 16:51:23 +08:00
|
|
|
@RequiredArgsConstructor
|
2021-09-03 16:22:04 +08:00
|
|
|
@RestController
|
|
|
|
@RequestMapping("/demo/redis/pubsub")
|
|
|
|
public class RedisPubSubController {
|
|
|
|
|
2021-11-18 17:01:23 +08:00
|
|
|
@GetMapping("/pub")
|
2022-07-07 18:08:14 +08:00
|
|
|
public R<Void> pub(@Parameter(name = "通道Key") String key, @Parameter(name = "发送内容") String value) {
|
2021-11-18 17:01:23 +08:00
|
|
|
RedisUtils.publish(key, value, consumer -> {
|
|
|
|
System.out.println("发布通道 => " + key + ", 发送值 => " + value);
|
|
|
|
});
|
2022-01-29 11:48:41 +08:00
|
|
|
return R.ok("操作成功");
|
2021-11-18 17:01:23 +08:00
|
|
|
}
|
2021-09-03 16:22:04 +08:00
|
|
|
|
2021-11-18 17:01:23 +08:00
|
|
|
@GetMapping("/sub")
|
2022-07-07 18:08:14 +08:00
|
|
|
public R<Void> sub(@Parameter(name = "通道Key") String key) {
|
2021-11-18 17:01:23 +08:00
|
|
|
RedisUtils.subscribe(key, String.class, msg -> {
|
|
|
|
System.out.println("订阅通道 => " + key + ", 接收值 => " + msg);
|
|
|
|
});
|
2022-01-29 11:48:41 +08:00
|
|
|
return R.ok("操作成功");
|
2021-11-18 17:01:23 +08:00
|
|
|
}
|
2021-09-03 16:22:04 +08:00
|
|
|
|
|
|
|
}
|