跳至主要內容

Springboot整合redission

holic-x...小于 1 分钟碎片化碎片化

Springboot整合redission

​ Spring Boot整合Redisson和Redis的主要区别在于Redisson提供了更高级的功能和抽象,它不仅包含了Redis的基本操作,还提供了分布式锁、分布式集合、可扩展的事件发布/订阅模式等。

image-20240406212635205

pom.xml依赖引入

<!-- 引入redission -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.16.2</version>
        </dependency>

RedisController测试

@Controller
public class RedisController {

    @Autowired
    private RedissonClient redissonClient;

    @RequestMapping("/publish")
    @ResponseBody
    public String publishMessage() {
        System.out.println("..........publish..........");
        RTopic topic = redissonClient.getTopic("myTopic");
        topic.publish("Hello, Redisson!");
        return "success";
    }

    @RequestMapping("/subscribe")
    @ResponseBody
    public String subscribeMessage() {
        System.out.println("..........subscribe..........");
        RTopic topic = redissonClient.getTopic("myTopic");
        topic.addListener(String.class, (message, channel) -> {
            System.out.println("now Received message: " + message);
        });
        return "success";
    }

}
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3