Typically, Tomcat, Jetty Servlet container, etc., will default Session held in memory. If a single application server instance the Session stored in the server memory is a very good solution. However, this scheme has a drawback, is not conducive to expansion. More and more applications use distributed deployment for high availability and load balancing. So the question is, if you deploy the same application on multiple servers to provide access through the external load balancing, how Session sharing? Sharing Session implement many programs, one of which is the common use Tomcat, Jetty and other server provides Session sharing the Session of unified content stored in a database (such as MySQL) or cache (such as Redis) in. Learn springcloud architecture can be added to beg: 3536247259.
Here we will springcloud micro services using redis simple and efficient sharing session.
Create a spring boot project, named the session-Redis-springcloud
POM-dependent configuration
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.carry</groupId>
<artifactId>springcloud-session-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-session-redis</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Profile
join redis, eureka, port and other configurations in the application.yml
server:
port: 8090
spring:
application:
name: service-session-redis
redis:
host: 192.168.68.100
port: 6379
password: 123456
timeout: 6000ms
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
database: 0
eureka:
client:
serviceUrl:
defaultZone: http://admin:123456@localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
Redis Session configuration based
control layer added Controller Test Method
package com.carry.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class UserManagementController {
/**
* redis sesion共享
*
* @param request
* @return
*/
@GetMapping("/getUser")
public String getUser(HttpServletRequest request) {
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
if (StringUtils.isEmpty(username)) {
username = "testSessionRedis|" + System.currentTimeMillis();
session.setAttribute("username", username);
}
System.out.println("访问端口:" + request.getServerPort());
return username;
}
}