Mod_Cluster LifecycleListeners Spring 启动

Mod_Cluster LifecycleListeners Spring Boot

我正在将我的项目 Spring 迁移到 Spring Boot。但是我遇到了一个问题,我们有一个使用 apache2 和 mod_cluster 的反向代理。在实际版本中,我们在 server.xml.

中声明了一个 Listerner
<Listener className="org.jboss.modcluster.container.catalina.standalone.ModClusterListener" advertise="false" proxyList="${proxyList}" />

我把它当作 Spring 启动应用程序。

private Connector ajpConnector() {
    Connector connector = new Connector("AJP/1.3");
    connector.setPort(8009);
    connector.setRedirectPort(8443);
    return connector;
}


private ModClusterListener modCluster() {
    ModClusterListener modClusterListener = new ModClusterListener();
    modClusterListener.setAdvertise(false);
    modClusterListener.setProxyURL(proxyUrl);

    return modClusterListener;
}

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> servletContainer() {
    return server -> {
        if (server != null) {
            server.addContextLifecycleListeners(modCluster());
            server.addAdditionalTomcatConnectors(ajpConnector());
        }
    };
}

但它不起作用,ModClusterListener 想要一个 Sever 类型的 LifecycleEvent,但它从未发生过。谁能帮帮我?

我在 Gitter 上发布了问题,Andy Wilkinson 帮助了我。

"From what you've said, it sounds like ModClusterListener needs to be added to Tomcat's Server but the method you've used will add it to the Context. You could use a context customizer and navigate up from the Context till you find the Server or you could use a TomcatServletWebServerFactory sub-class instead:"

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

        @Override
        protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
            tomcat.getServer().addLifecycleListener(modCluster());
            return new TomcatWebServer(tomcat);
        }

    };
}

对我有用!