【Azure Redis 缓存】定位Java Spring Boot 使用 Jedis 或 Lettuce 无法连接到 Redis的网络连通性步骤
问题描述
Java Spring Boot的代码在IDE里面跑可以连上 Azure 的 Redis服务,打包成Image放在容器里面跑,就连不上azure的redis服务,错误消息为:
Unable to connect to Redis; nested exception is org.springframework.data.redis.connection.PoolException:
Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to xxxxxxxxxxxxx.redis.cache.chinacloudapi.cn:6380
问题分析
第一步: 因为容器是Linux的机器上运行,所以第一步是确认客户端与Azure Redis服务之间的网络连通性。可以通过PsPing或者PaPing 测试。
#PsPing
psping xxxxxxxxxxxxx.redis.cache.chinacloudapi.cn:6380 #PaPing
./paping -p 6380 -c 500 xxxxxxxxxxxxx.redis.cache.chinacloudapi.cn
PsPing:https://docs.microsoft.com/zh-cn/sysinternals/downloads/psping
PaPing:https://code.google.com/archive/p/paping/downloads
PS:在windows的机器上,可以使用 tcping xxxxxxxxxxxxx.redis.cache.chinacloudapi.cn:6380
第二步: 抓取网络包
sudo tcpdump -i en0 -w xxxx.pcap
从网络包中验证是否是与TLS协议,版本错误相关。如果能够查看TCP及TLSv1.2的Application Data传递,则表明网络连通性,TLS协议都没有问题。

经过以上分析后,就可以定位问题一定发生在应用代码中,然后就是通过Demo来复现及定位问题。比如:排除项目中各种组件间的相互干扰,使用Azure官网中的简单Demo来测试联通问题。如上Jedis连接Redis的代码片段:
App.java
package example.demo; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo; /**
* Redis test
*
*/
public class App
{
public static void main( String[] args )
{ boolean useSsl = true;
String cacheHostname = "redis host";
String cachekey = "key"; // Connect to the Azure Cache for Redis over the TLS/SSL port using the key.
JedisShardInfo shardInfo = new JedisShardInfo(cacheHostname, 6380, useSsl);
shardInfo.setPassword(cachekey); /* Use your access key. */
Jedis jedis = new Jedis(shardInfo); // Perform cache operations using the cache connection object... // Simple PING command
System.out.println( "\nCache Command : Ping" );
System.out.println( "Cache Response : " + jedis.ping()); // Simple get and put of integral data types into the cache
System.out.println( "\nCache Command : GET Message" );
System.out.println( "Cache Response : " + jedis.get("Message")); System.out.println( "\nCache Command : SET Message" );
System.out.println( "Cache Response : " + jedis.set("Message", "Hello! The cache is working from Java!")); // Demonstrate "SET Message" executed as expected...
System.out.println( "\nCache Command : GET Message" );
System.out.println( "Cache Response : " + jedis.get("Message")); // Get the client list, useful to see if connection list is growing...
System.out.println( "\nCache Command : CLIENT LIST" );
System.out.println( "Cache Response : " + jedis.clientList()); jedis.close();
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>example.demo</groupId>
<artifactId>redistest</artifactId>
<version>1.0</version> <name>redistest</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies> <build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
参考资料
使用 PsPing & PaPing 进行 TCP 端口连通性测试: https://docs.azure.cn/zh-cn/articles/azure-operations-guide/virtual-network/aog-virtual-network-tcp-psping-paping-connectivity#paping
【Azure Redis 缓存】定位Java Spring Boot 使用 Jedis 或 Lettuce 无法连接到 Redis的网络连通性步骤的更多相关文章
- 【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑
编写Java Spring Boot应用,通过配置logging.path路径把日志输出在指定的文件夹中. 第一步:通过VS Code创建一个空的Spring Boot项目 第二步:在applicat ...
- Java Spring Boot VS .NetCore (一)来一个简单的 Hello World
系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...
- Java Spring Boot VS .NetCore (二)实现一个过滤器Filter
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (三)Ioc容器处理
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (五)MyBatis vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (七) 配置文件
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
随机推荐
- 2024了,我不想再用AOP收集业务操作日志了 | 京东云技术团队
0.背景 在近期的项目中,系统涉及到针对系统的业务操作日志统计功能,由于本系统位于业务链路的中心环节,负责接收上游系统的数据,并将基于用户操作产生的数据传递至下游系统,鉴于业务链路的复杂性和操作场景的 ...
- 利用pearcmd.php本地文件包含(LFI)
本文主要是为了学习如何用pearcmd进行本地文件包含 0x00 环境准备 首先先在docker中安装一个php环境. docker exec -it [container id] /bin/bas ...
- 7.2 通过API创建新进程
创建新的进程是Windows程序开发的重要部分,它可以用于实现许多功能,例如进程间通信.并行处理等.其中,常用的三种创建进程的方式分别是WinExec().ShellExecute()和CreateP ...
- 洛谷P3612 [USACO17JAN] Secret Cow Code S
[USACO17JAN] Secret Cow Code S 题面翻译 奶牛正在试验秘密代码,并设计了一种方法来创建一个无限长的字符串作为其代码的一部分使用. 给定一个字符串,让后面的字符旋转一次(每 ...
- ehlib组件包当中TDBLookupComboboxEh的小结
TDBLookupComboboxEh和TDBGridEh一样强大无比,可以做出Combobox下拉出Grid的效果.下面是一些重要属性的小结(可怜费了我半天功夫,文档太少了.......)(1)Li ...
- 《ASP.NET Core 微服务实战》-- 读书笔记(第9章)
第 9 章 微服务系统的配置 微服务系统中的配置需要关注更多其他方面的因素,包括: 配置值的安全读写 值变更的审计能力 配置信息源本身的韧性和可靠性 少量的环境变量难以承载大型.复杂的配置信息 应用要 ...
- NC208250 牛牛的最美味和最不美味的零食
题目链接 题目 题目描述 牛牛为了减(吃)肥(好),希望对他的零食序列有更深刻的了解,所以他把他的零食排成一列,然后对每一个零食的美味程度都打了分,现在他有可能执行两种操作: eat k:吃掉当前的第 ...
- NC17889 新建 Microsoft Office Word 文档
题目链接 题目 题目描述 CSL正在学习<计算机办公自动化>文件的建立与删除. CSL发现,当他新建一个word文档时,会得到一个名为"新建 Microsoft Office W ...
- NC16742 [NOIP2002]字串变换
题目链接 题目 题目描述 已知有两个字串 A, B及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在A中的子串 A1可以变换为 B1.A2可以变换 ...
- keras建模的3种方式——序列模型、函数模型、子类模型
1 前言 keras是Google公司于2016年发布的以tensorflow为后端的用于深度学习网络训练的高阶API,因接口设计非常人性化,深受程序员的喜爱. keras建模有3种实现方式--序列模 ...