61. Cloud Foundry 支持

Spring Boot 的 Actuator 模块包括额外的支持,当您将其部署到兼容的 Cloud Foundry 实例时就会激活。 /cloudfoundryapplication路径提供了通往所有@Endpoint bean 的备用安全路由。

扩展支持使 Cloud Foundry Management UI(例如可用于查看已部署的应用程序的 Web 应用程序)增加了 Spring Boot Actuator 信息。例如,应用程序状态页面可能包含完整的运行状况信息,而不是典型的“正在运行”或“已停止”状态。

Note

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

61.1 禁用扩展的 Cloud Foundry Actuator 支持

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

application.properties.

management.cloudfoundry.enabled=false

61.2 Cloud Foundry 自签名证书

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

application.properties.

management.cloudfoundry.skip-ssl-validation=true

61.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("/*");
	};
}
上一章 首页 下一章