Spring Boot中文參考指南(2.1.6)61、Cloud Foundry 支持

下一篇[未完待續]

Spring Boot中文參考指南(2.1.6)61、Cloud Foundry 支持

<code>英文原文:https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/production-ready-cloudfoundry.html/<code>
<code>GitHub:https://github.com/jijicai/Spring/tree/master/spring-boot/<code>

61、Cloud Foundry 支持

Spring Boot 的 actuator 模塊包括在部署到兼容的 Cloud Foundry 實例時激活的其他支持。/cloudfoundryapplication 路徑提供了通往所有 @Endpoint bean 的另一種安全路由。

擴展支持使 Cloud Foundry 管理 UI(例如你可以用來查看已部署的應用程序的 web 應用程序)能夠使用 Spring Boot actuator 信息進行增強。例如,應用程序狀態頁面可能包含完整的運行狀況信息,而不是典型的 “正在運行” 或 “已停止” 狀態。

註釋:普通用戶無法直接訪問 /cloudfundryapplication 路徑。為了使用端點,必須與請求一起傳遞有效的 UAA token。

61.1、禁用擴展的 Cloud Foundry Actuator 支持

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

Spring Boot中文參考指南(2.1.6)61、Cloud Foundry 支持

<code>application.properties
management.cloudfoundry.enabled=false/<code>

61.2、Cloud Foundry 自簽名證書

默認情況下,/cloudfoundryapplication 端點的安全驗證對各種 Cloud Foundry 服務進行 SSL 調用。如果你的 Cloud Foundry UAA 或 Cloud Controller 服務使用自簽名證書,則需要設置以下屬性:

<code>application.properties
management.cloudfoundry.skip-ssl-validation=true/<code>

61.3、自定義上下文路徑

如果服務器的上下文路徑已配置為 / 以外的任何內容,則 Cloud Foundry 端點在應用程序的根目錄將不可用。例如,如果 server.servlet.context-path=/app,則 Cloud Foundry 端點將在/app/cloudfoundry dryapplication/* 上可用。

如果你希望 Cloud Foundry 端點始終在/cloudfoundryapplication/* 上可用,無論服務器的上下文路徑如何,則你需要在應用程序中明確配置它。配置將根據使用中的 web 服務器而有所不同。對於 Tomcat,可以添加以下配置:

Spring Boot中文參考指南(2.1.6)61、Cloud Foundry 支持

<code>@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("/*");
};
}/<code>

62、延伸閱讀

如果你想了解本章討論的一些概念,可以查看 actuator 示例應用程序。你可能還想了解一些繪圖工具,比如 Graphite。(https://graphite.wikidot.com/ )

否則,你可以繼續閱讀有關部署選項的內容,或者直接跳到有關 Spring Boot 構建工具插件的深入信息。

下一篇[未完待續]


分享到:


相關文章: