7
votes

I developing web-application using spring-boot. And I want to cluster spring-boot embedded tomcat 7. I searched all day long, but answers are almost using server.xml and apache. But the way of using server.xml on spring-boot, I couldn't find it. I think I need usage of configuration multiple connector, and engine, and so on. I don't know, it is right way. Please show me the way.

2

2 Answers

4
votes

You can use a load balancer(such as nginx) to distribute the load and Spring-session to externalize session handling.

See example here.

2
votes

I finally found solution. Actually I found a blog site.

It is using Redis.

The link is http://dmitrijs.artjomenko.com/2014/02/storing-sessions-in-redis-with-spring.html

My application is developed by java7, but example is using java8.

So, I modified some code, modified code is below:

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {

    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) container;
            containerFactory.addContextValves(new RedisSessionHandlerValve());
            ArrayList<MyTomcatContextCustomizer> customizers = Lists.newArrayList(new MyTomcatContextCustomizer());
            containerFactory.setTomcatContextCustomizers(customizers);
        }
    };
}

public class MyTomcatContextCustomizer implements TomcatContextCustomizer {
    @Override
    public void customize(Context context) {
        context.setSessionTimeout(30);
        context.setManager(new RedisSessionManager() {{
            setHost("127.0.0.1");
        }});
    }
}