5.10 Cloud Foundry支持

优质
小牛编辑
125浏览
2023-12-01

Spring Boot的执行器模块包括在部署到兼容的Cloud Foundry实例时激活的其他支持。 /cloudfoundryapplication路径为所有@Endpoint bean提供了另一种安全路由。

扩展支持允许使用Spring Boot执行器信息扩充Cloud Foundry管理UI(例如可用于查看已部署应用程序的Web应用程序)。 例如,应用程序状态页面可以包括完整的健康信息而不是典型的“运行”或“停止”状态。

常规用户无法直接访问/cloudfoundryapplication路径。 为了使用端点,必须与请求一起传递有效的UAA令牌。

5.10.1 禁用Extended Cloud Foundry Actuator支持

如果要完全禁用/cloudfoundryapplication端点,可以将以下设置添加到application.properties文件中:

management.cloudfoundry.enabled=false

5.10.2 Cloud Foundry自签名证书

默认情况下,/cloudfoundryapplication端点的安全验证会对各种Cloud Foundry服务进行SSL调用。 如果您的Cloud Foundry UAA或Cloud Controller服务使用自签名证书,则需要设置以下属性:

management.cloudfoundry.skip-ssl-validation=true

5.10.3 自定义上下文路径

如果服务器的上下文路径已配置为/以外的任何其他内容,则Cloud Foundry端点将无法在应用程序的根目录中使用。 例如,如果server.servlet.context-path = /app,Cloud Foundry端点将在/app/cloudfoundryapplication/*中提供。

如果您希望Cloud Foundry端点始终在/cloudfoundryapplication/*处可用,则无论服务器的上下文路径如何,您都需要在应用程序中明确配置它。 配置将根据使用的Web服务器而有所不同。 对于Tomcat,可以添加以下配置:

@Bean
public TomcatServletWebServerFactory servletWebServerFactory() {
	return new TomcatServletWebServerFactory() {

		@Override
		protected void prepareContext(Host host,
				ServletContextInitializer[] initializers) {
			super.prepareContext(host, initializers);
			StandardContext child = new StandardContext();
			child.addLifecycleListener(new Tomcat.FixContextListener());
			child.setPath("/cloudfoundryapplication");
			ServletContainerInitializer initializer = getServletContextInitializer(
					getContextPath());
			child.addServletContainerInitializer(initializer, Collections.emptySet());
			child.setCrossContext(true);
			host.addChild(child);
		}

	};
}

private ServletContainerInitializer getServletContextInitializer(String contextPath) {
	return (c, context) -> {
		Servlet servlet = new GenericServlet() {

			@Override
			public void service(ServletRequest req, ServletResponse res)
					throws ServletException, IOException {
				ServletContext context = req.getServletContext()
						.getContext(contextPath);
				context.getRequestDispatcher("/cloudfoundryapplication").forward(req,
						res);
			}

		};
		context.addServlet("cloudfoundry", servlet).addMapping("/*");
	};
}