SpringBoot 执行器在同一端口上被 BaseService Servlet 所掩盖
SpringBoot Actuator getting overshadowed by BaseService Servlet on same Port
我们正在尝试将我们现有的 Spring Web 服务应用程序迁移到 Spring 引导和 运行 到我们寻求您的建议的问题。
出于安全原因,我们有一个基本服务 Servlet 在部署应用程序的端口上禁用 GET,此 servlet returns 501 未实现响应如下:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.warn("GET request received!");
response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
}
public abstract class BaseServiceServlet extends HttpServlet {
...
}
public class ServiceServlet extends BaseServiceServlet {
...
}
public class ServletInitializer extends SpringBootServletInitializer implements ServletContextInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
ServletRegistration.Dynamic application = container
.addServlet("ServiceServlet", new ServiceServlet());
application.setLoadOnStartup(2);
application.addMapping("/*");
}
}
之前我们实现了一个老式的 HealthCheck JSP。随着移动到 SpringBoot,我们现在使用 SpringBoot Actuator。
但是我们发现,如果我们在尝试监控健康时将 Actuator 健康监控端口设置为与 App 相同的端口,我们会得到 501 未实现的响应。
配置如下:
# Spring-Boot Embedded Tomcat Config
server.port=8080
server.connection-timeout=10s
server.ssl.enabled=false
## Springboot based health monitor
management.endpoints.enabled-by-default=true
management.endpoint.health.enabled=true
management.endpoint.loggers.enabled=true
management.endpoints.web.cors.allowed-methods=GET
management.endpoint.beans.cache.time-to-live=20s
management.endpoints.web.base-path=/manage
management.server.port=8080
management.security.enabled=false
解决此问题的一种方法是将执行器健康检查端口更改为其他可用的端口。
问题:
我们如何将执行器端口设置为与应用程序相同,并使执行器健康检查 url 类似于 http://localhost:8080/manage/health 而不是 return 501 未实现来自基本服务 Servlet ?
我们可以在 ServiceServlet
.
之前的映射中向健康端点注册 DispatcherServlet
@SpringBootApplication
public class ServletInitializer extends SpringBootServletInitializer implements ServletContextInitializer {
@Autowired
private DispatcherServlet dispatcherServlet;
@Override
public void onStartup(ServletContext container) throws ServletException {
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/manage", "/manage/health");
ServletRegistration.Dynamic application = container
.addServlet("ServiceServlet", new ServiceServlet());
application.setLoadOnStartup(2);
application.addMapping("/*");
}
}
我们正在尝试将我们现有的 Spring Web 服务应用程序迁移到 Spring 引导和 运行 到我们寻求您的建议的问题。
出于安全原因,我们有一个基本服务 Servlet 在部署应用程序的端口上禁用 GET,此 servlet returns 501 未实现响应如下:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.warn("GET request received!");
response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
}
public abstract class BaseServiceServlet extends HttpServlet {
...
}
public class ServiceServlet extends BaseServiceServlet {
...
}
public class ServletInitializer extends SpringBootServletInitializer implements ServletContextInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
ServletRegistration.Dynamic application = container
.addServlet("ServiceServlet", new ServiceServlet());
application.setLoadOnStartup(2);
application.addMapping("/*");
}
}
之前我们实现了一个老式的 HealthCheck JSP。随着移动到 SpringBoot,我们现在使用 SpringBoot Actuator。
但是我们发现,如果我们在尝试监控健康时将 Actuator 健康监控端口设置为与 App 相同的端口,我们会得到 501 未实现的响应。
配置如下:
# Spring-Boot Embedded Tomcat Config
server.port=8080
server.connection-timeout=10s
server.ssl.enabled=false
## Springboot based health monitor
management.endpoints.enabled-by-default=true
management.endpoint.health.enabled=true
management.endpoint.loggers.enabled=true
management.endpoints.web.cors.allowed-methods=GET
management.endpoint.beans.cache.time-to-live=20s
management.endpoints.web.base-path=/manage
management.server.port=8080
management.security.enabled=false
解决此问题的一种方法是将执行器健康检查端口更改为其他可用的端口。
问题: 我们如何将执行器端口设置为与应用程序相同,并使执行器健康检查 url 类似于 http://localhost:8080/manage/health 而不是 return 501 未实现来自基本服务 Servlet ?
我们可以在 ServiceServlet
.
DispatcherServlet
@SpringBootApplication
public class ServletInitializer extends SpringBootServletInitializer implements ServletContextInitializer {
@Autowired
private DispatcherServlet dispatcherServlet;
@Override
public void onStartup(ServletContext container) throws ServletException {
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/manage", "/manage/health");
ServletRegistration.Dynamic application = container
.addServlet("ServiceServlet", new ServiceServlet());
application.setLoadOnStartup(2);
application.addMapping("/*");
}
}