preface

** This is all based on the previous project. ** Springboot integration with Redis is very convenient, which is springBoot’s mission to simplify configuration. This article will show you how to use Springboot to integrate Redis to implement session sharing.

Introduction of depend on

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.session</groupId>

<artifactId>spring-session-data-redis</artifactId>

</dependency>
Copy the code

Configure redis

Configure Redis in application.properties

# Redis server address

spring.redis.host=localhost

# Redis server connection port

spring.redis.port=6379

# Redis server connection password (default null)

spring.redis.password=
Copy the code

Because my local Redis did not set the password, so the password here is left blank. This configuration item can be removed, but I left it here to show you that you can configure the Redis password here

Implementing Session Sharing

In fact, SpringBoot is very convenient to implement session sharing, just need an @enableredisHttpSession annotation. Add this annotation to the SpringBoot boot class.

At this point, you’ve implemented redis to store sessions. Here is a simple example to test whether the session is stored in Redis.

Create a new sessionController.java and write a session setting method as follows:

To start the project, call postman as follows:

Use the Redis visualization tool to see if the property exists in Redis:

Happyjava already exists in Redis.

Use nginx tests

As shown in the above example, sessions are stored by Redis. Now, we can test whether sessions are really shared by starting the application on two ports and using nginx forwarding.

Nginx. Config configuration

Upstream session_server {server 127.0.0.1:8080; Server 127.0.0.1:8081; } server { listen 80; server_name localhost; location / { proxy_pass http://session_server; proxy_set_header Host$host; }}Copy the code

The localhost request is forwarded to local ports 8080 and 8081.

Port specifies the port number of the program through server.port

application.properties

server.port=8081
Copy the code

Write a method to get session

SessionController.java

The server. Port property of the configuration file is retrieved via the Value annotation and returned via the getSession method.

Start the program on two ports

The IDEA setting can start the same main method multiple times.

Allow Paralled run is checked to start a main method several times. We set server.port to 8080 to start once and then 8081 to start again.

With multiple calls to postman, we get the following result:

Port 8080 and port 8081, respectively, can obtain happyJava values set before, indicating that session sharing has been successfully configured.

conclusion

Springboot is very convenient to configure session sharing with the EnableRedisHttpSession annotation. You can also use the EnableMongoHttpSession annotation to manage sessions with MongoDB.