Rainbond best practice: Tomcat configures Redis to achieve Session sharing

Rainbond best practice: Tomcat configures Redis to achieve Session sharing

Rainbond: Production-level serverless PaaS Rainbond is the first open-source production-level serverless PaaS in China. It deeply integrates technologies such as Kubernetes-based container management, multi-type CI/CD application construction and delivery, and multi-data center resource management to provide cloud-native services. Apply full life cycle solutions to build an interconnection ecosystem between applications and infrastructure, between applications and between infrastructures. Click to install

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 a Java War package project using Tomcat to configure Redis to implement a session sharing solution, and store your session in the Redis to ensure the stability of your application. as the picture shows:Enter image description

If Load Balancing sends the request to tomcat A under container 1 , and generates a session at the same time , the session is persisted to Redis . When the Web Server sends the request again, if the request is made to tomcat A of container 2 , the existing session will be found in Redis , which can be used immediately .

By building from source code, you can configure :

Use Webapp-Runner or Jetty-Runner

Yunbang uses the tomcat embedded in webapp-Runner or the jetty embedded in jetty-Runner to implement server functions. You can easily deploy applications on the cloud without creating additional servers. The following steps can be used to configure redis to achieve session sharing .

  1. Configure Procfile : Add the following command to your Procfile, and add Procfile to the source root directory.

    web: java -jar ./webapp-runner.jar --port 5000 --session-store redis ./*.war
    
    • The application port is 8080, and the platform opens the application port 5000 by default, so that the port mapping is normal:
    • Specify session storage--session-store redis
  2. Configure webserver: add the webserver file in the source code root directory, and write the current platform to support the webapp-runner version:

    webapp-runner-7.0.57.2.jar
    
    webapp-runner-8.0.18.0-M1.jar
    
    webapp-runner-8.5.5.2.jar
    
  3. Yunbang creates an application through source code , and selects the created Redis for dependency association in the Create Application-Application Settings .

  4. Application configuration redis: will be REDIS_URLadded to the application environment variable, the value is 127.0.0.1:6379.

  5. Restart the app to adapt

To facilitate dependencies when creating an application, it is recommended to create a Redis application through the application market in advance ; if you do not rely on a Redis application when creating an application, you can also associate the Redis application in Application . Remember to restart the app after connecting.

Using docker images

Cloud Help provides a way to launch applications using custom tomcat containers. The following steps can be used to configure redis to achieve session sharing .

  1. Create a Dockerfile and write the following:

    • Use source code

dockerfile FROM goodrainapps/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

- 使用war包


```dockerfile
FROM goodrainapps/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 exists <dir_name>or <filename>.warexists, and exists in the same directory as the Dockerfile file, and start creating the application with this directory as the root directory .

  2. In Create Application - Application Settings , select the created Redis for dependency association.

  3. Application configuration redis: configure the variable REDIS_URLto the application environment variable, the value is 127.0.0.1:6379; configure the variable REDIS_SESSIONto the application environment variable, the value is true.

  4. Restart the app to adapt

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325848952&siteId=291194637