启用App Insights后Azure App Service部署Java/容器返回404求助
解决方案:Azure App Service启用App Insights后Spring Boot路由失效的问题
替换无代码Agent为官方标准Agent
- 下载对应版本的官方
applicationinsights-agent.jar,将其上传至Azure App Service的文件系统(可通过Kudu工具或部署时打包进项目根目录) - 修改App Service的环境变量
JAVA_TOOL_OPTS,将原有自动注入的-javaagent:/agent/java/applicationinsights-agent-codeless.jar替换为指向你上传的标准Agent路径,例如:-javaagent:/home/site/wwwroot/applicationinsights-agent.jar - 添加环境变量
APPLICATIONINSIGHTS_CONNECTION_STRING,设置你的Application Insights连接字符串 - 重启App Service,验证应用HTTP路由和追踪数据是否正常
- 下载对应版本的官方
改用Spring Starter依赖替代Java Agent
- 在Spring Boot项目的构建文件中添加Application Insights Spring Starter依赖:
- Maven(pom.xml):
<dependency> <groupId>com.microsoft.azure</groupId> <artifactId>applicationinsights-spring-boot-starter</artifactId> <version>3.4.14</version> <!-- 选择与Spring Boot兼容的版本 --> </dependency> - Gradle(build.gradle):
implementation 'com.microsoft.azure:applicationinsights-spring-boot-starter:3.4.14'
- Maven(pom.xml):
- 在项目的
application.properties或application.yml中配置连接字符串:azure.application-insights.connection-string=你的连接字符串 - 关闭Azure App Service的App Insights自动配置功能(在App Service控制台的「监视」>「Application Insights」中选择禁用自动配置)
- 重新构建Jar包并部署,此时无需依赖Java Agent参数,避免路由冲突
- 在Spring Boot项目的构建文件中添加Application Insights Spring Starter依赖:
排查官方Agent的兼容性问题
- 确认下载的
applicationinsights-agent版本与你的Spring Boot版本兼容:Spring Boot 3.x需使用Agent 3.4.0及以上版本,Spring Boot 2.x选择对应兼容版本 - 通过Azure App Service的「日志流」查看详细错误信息,根据报错调整配置或Agent版本
- 先在本地环境用标准Agent启动应用,验证功能正常后再部署到Azure
- 确认下载的
应急临时方案
若需快速恢复应用可用性,可暂时关闭App Insights功能,验证应用路由是否正常,确认问题根源后再逐步重新配置追踪功能
内容的提问来源于stack exchange,提问作者dosmanak
相关产品推荐
相关产品推荐

