【Azure 应用服务】App Service 项目部署成功后,应用连接 Azure Redis时报错 Could not get a resource from the pool - LuBu0505/My-Code GitHub Wiki

问题描述

App Service 项目部署成功后,需要连接到同在云上的Redis服务, Redis启动了专用终结点,只能在于Redis同一个VNET(虚拟网络)的资源能够访问。在进入App Service的Bash中使用Paping测试Redis的网络连通性,验证是通的。但是为什么应用程序中一直出错呢?

Bash中使用Paping:

No alt text provided for this image

应用程序中的错误信息:

[INFO]  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 xxxxxxx.redis.cache.chinacloudapi.cn:6380

at org.springframework.data.redis.connection.lettuce.LettucePoolingConnectionProvider.getConnection(LettucePoolingConnectionProvider.java:86) 
~[spring-data-redis-2.0.6.RELEASE.jar!/:2.0.6.RELEASE]

问题分析

App Service配置了VNET后,可以在Bash中paping通Redis服务,表示从App Service到Redis之间的网络链路是相通的。但是为什么在应用中就是连接不上Redis呢?

仔细查看App Service的文档后,是由于应用与 VNet 集成后,它将使用 VNet 配置的 DNS 服务器。 默认情况下,应用不能用于 Azure DNS 专用区域。需要添加设置(WEBSITE_VNET_ROUTE_ALL & WEBSITE_DNS_SERVER )将所有出站调用从应用发送到 VNet,并允许应用访问 Azure DNS 专用区域。

添加配置参数如下后,应用调用Redis成功。

WEBSITE_VNET_ROUTE_ALL = 1
WEBSITE_DNS_SERVER = 168.63.129.16

但是在App Service for Linux环境中,可以通过全部路由( Route All) 功能来代替 WEBSITE_VNET_ROUTE_ALL 配置。 No alt text provided for this image

**注意:**配置应用程序路由时,可将所有流量或仅将专用流量路由到 VNet。 通过“全部路由(Route All)”设置来配置此设置。

参考资料:

将应用与 Azure 虚拟网络集成:https://docs.microsoft.com/zh-cn/azure/app-service/web-sites-integrate-with-vnet#regional-vnet-integration

**Azure DNS 专用区域: **https://docs.azure.cn/zh-cn/app-service/web-sites-integrate-with-vnet#azure-dns-private-zones

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

分类: 【Azure 应用服务】

标签: 应用连接 Redis Could not get a resource from the poolAzure RedisApp Service