通过使用lettuce连接器的报错了解到URI的特殊字符转换

lettuce连接redis使用的是uri作为参数传递的形式 连接时一直有如下错误

Exception in thread "main" java.net.URISyntaxException: Malformed escape pair at index 21: redis://dsa dafdasfaa&%@127.0.0.1:6379/0?timeout=10s

密码已经被替换 查了很多资料 最后知道原因是一旦地址中涉及了一些特殊字符 则不能直接用string 转换成uri 网上给的思路是用url 然后 再用url 转成uri 然后就是一系列的 gethost getPath 等  其实本质上只有一两个特殊字符而已 直接用对应的方式替换掉即可 以下是常见的特殊字符

字符    -    URL编码值

空格    -    %20 
"          -    %22 
#         -    %23 
%        -    %25 
&         -    %26 
(          -    %28 
)          -    %29 
+         -    %2B 
,          -    %2C 
/          -    %2F 
:          -    %3A 
;          -    %3B 
<         -    %3C 
=         -    %3D 
>         -    %3E 
?         -    %3F 
@       -    %40 
\          -    %5C 
|          -    %7C  

把用到的特殊字符替换一下 就能正确运行

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Spring Boot中使用Lettuce连接Redis时,可以通过以下方式检查连接的有效性: 1. 使用LettuceConnectionFactory创建连接工厂对象,并设置连接池配置; 2. 使用RedisTemplate创建Redis操作模板对象,并设置连接工厂对象; 3. 使用RedisTemplate的execute方法执行ping命令,如果返回结果为PONG,则连接有效。 示例代码如下: ``` @Configuration public class RedisConfig { @Bean public LettuceConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); config.setHostName("localhost"); config.setPort(6379); config.setPassword(RedisPassword.of("password")); LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder() .useSsl().and() .commandTimeout(Duration.ofSeconds(2)) .shutdownTimeout(Duration.ZERO) .build(); return new LettuceConnectionFactory(config, clientConfig); } @Bean(name = "redisTemplate") public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); template.afterPropertiesSet(); return template; } } @Component public class RedisHealthIndicator implements HealthIndicator { @Autowired private RedisTemplate<String, Object> redisTemplate; @Override public Health health() { try { String pong = (String) redisTemplate.execute((RedisCallback<String>) connection -> connection.ping()); if ("PONG".equals(pong)) { return Health.up().build(); } return Health.down().withDetail("error", "ping command failed").build(); } catch (Exception e) { return Health.down(e).build(); } } } ``` 在以上示例中,RedisHealthIndicator类实现了Spring Boot的HealthIndicator接口,用于检查Redis连接的健康状态。在health方法中,通过RedisTemplate的execute方法执行ping命令,如果返回结果为PONG,则连接有效。Health对象的状态和详情信息可以在Spring Boot的actuator端点中查看。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值