简体   繁体   中英

Mod_Cluster LifecycleListeners Spring Boot

I'm migrating my project Spring to Spring Boot. But I'm faced with a problem, we have a reverse proxy using apache2 and mod_cluster. In actual version we declare a Listerner in the server.xml.

<Listener className="org.jboss.modcluster.container.catalina.standalone.ModClusterListener" advertise="false" proxyList="${proxyList}" />

I put it like a Spring boot application.

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());
        }
    };
}

But it don't work, the ModClusterListener want a LifecycleEvent of type Sever, but it never happen. Can anyone help me?

I posted the question on Gitter and Andy Wilkinson helped me.

"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);
        }

    };
}

It worked for me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM