Tomcat configures Redis to implement Session sharing

In order to make your application withstand more concurrency and improve application stability, you need to expand the capacity when appropriate.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 Java War package project using Tomcat to configure Redis to achieve session sharing solution, share your session Stored in redis to ensure the stability of your application.

If you use source code to deploy Java projects, you can configure redis to achieve session sharingin the following two ways:

Use Webapp-Runner or Jetty-Runner

Rainbond uses webapp-Runner embedded tomcat or jetty-Runner embedded jetty to implement server functions.Applications can be easily deployed on Rainbond without you needing to create additional servers.The following steps can be used to achieve configure redis to achieve session sharing.

  1. Configuration Procfile:Add the following commands to your Procfile, and add Procfile to the source root directory.

    1. web: java -jar ./webapp-runner.jar --port $PORT --session-store redis ./*.war
    • The listening port is specified, and by obtaining the environment variable \$PORT, this variable Rainbond performs automatic injection according to the service port set by the platform
    • Specify session store--session-store redis
  2. Install the Redis service from the application market, and set the current service to depend on the created Redis service, refer to document Communication between components

  3. The application configuration redis:addsREDIS_URLto the application environment variable, and the value is redis://:${REDIS_PASS}@127.0.0.1:6379.

  4. Restart the app to adapt

Using docker images

Rainbond provides a way to start an application using a custom tomcat container.The following steps can be used to achieve configure redis to achieve session sharing.

  1. Create a Dockerfile and write the following content:

    • use source code
  1. FROM goodrainapps/tomcat:7.0.82-jre7-alpine
  2. RUN rm /usr/local/tomcat/webapps/ROOT
  3. COPY <dir_name> /usr/local/tomcat/webapps/ROOT #<dir_name>is the source directory name
  4. EXPOSE 8080
  • use war package
  1. FROM goodrainapps/tomcat:7.0.82-jre7-alpine
  2. RUN rm /usr/local/tomcat/webapps/ROOT
  3. COPY <filename>.war /usr/local/tomcat/webapps/ROOT.war
  4. EXPOSE 8080
  1. Confirm that<dir_name>or<filename>.warof the source code exists, and it exists in the same directory as the Dockerfile file, and this directory is the root directory to start creating components
  2. Install Redis service and establish dependencies, refer to document Communication between components
  3. The application configures the redis:configuration variableREDIS_URLto the application environment variable with the value 127.0.0.1:6379; the configuration variableREDIS_SESSIONto the application environment variable, the value istrue.
  4. Restart the app to adapt