Tomcat Configure Redis for Session Sharing

In order to allow your application to withstand more concurrency and improve application stability, you need to expand the capacity under appropriate circumstances. The Tomcat under each node only stores the session generated when accessing its own request. In order to solve the problem of session persistence after expansion, we provide a solution of Java War package project using Tomcat to configure Redis to achieve session sharing. Your session is stored in redis to ensure the stability of your application.

If you use Kato source code to deploy a Java project, you can implement Configure redis to achieve session sharing in the following two ways:

Use Webapp-Runner or Jetty-Runner

Kato uses webapp-Runner embedded tomcat or jetty-Runner embedded jetty to achieve server functions. You can easily deploy applications on Kato without creating other servers. Through the following steps, you can configure redis to realize session sharing.

  1. Configure Procfile: add the following command to your Procfile, and add Procfile to the root directory of the source code.
  web: java -jar ./webapp-runner.jar --port $PORT --session-store redis ./*.war
  • The listening port is specified. By obtaining the environment variable $PORT, this variable Kato is automatically injected according to the service port set by the platform

  • Specify session store --session-store redis

  1. Install the Redis service from the application market, and set the current service to depend on the created Redis service, refer to the document Communication between components

  2. Application configuration redis: Add REDIS_URL to the application environment variable, the value is redis://:${REDIS_PASS}@127.0.0.1:6379.

  3. Restart the app to enable changes

Use Docker Image

Kato provides a method to start the application using a custom tomcat container. Through the following steps, you can configure redis to realize session sharing.

  1. Create a Dockerfile and write the following:
Use source code
FROM gridworkzapps/tomcat:7.0.82-jre7-alpine
RUN rm /usr/local/tomcat/webapps/ROOT
COPY <dir_name> /usr/local/tomcat/webapps/ROOT #<dir_name> is the source directory name
EXPOSE 8080
Use War Package
FROM gridworkzapps/tomcat:7.0.82-jre7-alpine
RUN rm /usr/local/tomcat/webapps/ROOT
COPY <filename>.war /usr/local/tomcat/webapps/ROOT.war
EXPOSE 8080
  1. Confirm that the source code <dir_name> or <filename>.war exists, and it exists in the same directory as the Dockerfile file, and use this directory as the root directory to start creating components
  2. Install Redis service and establish dependencies, refer to the document Communication between components
  3. Application configuration redis: configure the variable REDIS_URL to the application environment variable, the value is 127.0.0.1:6379; configure the variable REDIS_SESSION to the application environment variable, the value is true.
  4. Restart the app to enable changes