SpringBoot中单机Reidsson客户端配置

本文详细介绍了在SpringBoot中配置Redisson客户端的过程,特别指出在配置文件application.yml中,当Redis密码为空时的一个常见错误陷阱,强调了正确处理密码为空的设置方法,以避免导致分布式锁等相关功能失效。
摘要由CSDN通过智能技术生成

这其中有一个大坑! 

在 application.yml 配置中,redis密码配置为空的时候(如下代码所示),如果调用setPassword(password)方法直接设置密码,那么传入的将是一个空字符串,然后你就会发现怎么就连接不是redis呢?!所以这里一定要判断空字符串,将其转为null再SetPassword。(或者配置文件中不配置password这个属性应该默认就是null)

#application.yml 一段配置:

spring:
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    password:
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.an
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot监测WebSocket客户端断开连接,你可以使用`WebSocketSession`提供的方法和Spring的事件机制来实现。 首先,创建一个WebSocket处理器类,继承`TextWebSocketHandler`,重写相应的方法,例如: ```java import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; public class MyWebSocketHandler extends TextWebSocketHandler { @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // 连接建立时触发 System.out.println("WebSocket连接已建立"); } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { // 连接关闭时触发 System.out.println("WebSocket连接已关闭,状态码:" + status.getCode() + ",原因:" + status.getReason()); } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { // 处理接收到的消息 String receivedMessage = message.getPayload(); System.out.println("接收到消息:" + receivedMessage); } } ``` 然后,在你的Spring Boot应用程序配置WebSocket,可以使用`@EnableWebSocket`注解启用WebSocket功能,并添加一个`WebSocketHandler` bean。例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; @SpringBootApplication @EnableWebSocket public class MyApplication implements WebSocketConfigurer { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myWebSocketHandler(), "/websocket"); } @Bean public MyWebSocketHandler myWebSocketHandler() { return new MyWebSocketHandler(); } } ``` 在上面的示例,`WebSocketHandlerRegistry`用于注册WebSocket处理器,并指定WebSocket的URL路径。`myWebSocketHandler()`方法返回我们创建的`MyWebSocketHandler`实例。 当客户端与服务器建立连接、关闭连接时,`MyWebSocketHandler`类相应的方法将被调用,你可以在这些方法处理相应的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值