In the springcloud microservice project, using redis to achieve session sharing is more mainstream, simple and efficient, and direct code practice:
###1. Add related dependencies in pom.xml###
<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>
###2, Added redis placement###
service-user-session-redis-dev.yml
server:
port: 8831
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
spring:
redis:
database: 0
host: 192.168.248.131
port: 6379
password:
timeout: 20000
#cluster:
#nodes: 192.168.211.134:7000,192.168.211.134:7001
#-
#max-redirects:
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
###3. Add redis configuration class to enable redis code spring default session###
RedisSessionConfig
package com.mayi.springcloud.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
}
###4. Add test method to UserManagementController###
package com.mayi.springcloud.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Value;
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)){
session.setAttribute("username", "testSessionRedis|" + System.currentTimeMillis());
}
return username;
}
}
###5. Modify gateway configuration###
In the springcloud project, the session invalidation problem occurs after the request is forwarded by the gateway zuul. This is because zuul will discard the original session and generate a new session by default. The solution is the gateway configuration file
Add sensitiveHeaders: “*” to service-zuul-dev.yml
server:
port: 1100
zuul:
ignoredServices: '*' #忽略所有未配置的service
host:
connect-timeout-millis: 20000
socket-timeout-millis: 20000
routes:
user-service: #自定义名称
path: /user/**
serviceId: service-user #/user/开头的路径转发至service-user微服务
sensitiveHeaders: "*"
user-redis-session-service:
path: /user-session/**
serviceId: service-user-session-redis
sensitiveHeaders: "*"
hystrix: #hystrix配置
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 2500
ribbon: #ribbon负载均衡参数配置
ReadTimeout: 5000
ConnectTimeout: 5000
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
###6、Test###
Start EUREKA, SERVICE-CONFIG, SERVICE-USER-SESSION-REDIS, SERVICE-ZUUL in sequence
Access the gateway address http://localhost:1100/user-session/getUser multiple times
1. Found that there is already session information in redis
2. The information in the session is obtained from redis, and it is the same session
Summary: The error prone part when Spring Cloud integrates Redis to realize session sharing is that forwarding through the gateway will invalidate the original session. Most people will think that the integration is not successful. It is not difficult to find the problem by carefully looking at the source code of Zuul or directly accessing the microservice to try. location.
Architect Q group: 618578034
spring cloud community group (you need to add WeChat to pull you in)
WeChat public account: Java architect training
This public account will be used for one year. It will publish a complete architecture article according to the JAVA senior software architect practical training route. The difficulty level is from easy to deep. It is suitable for those who have a certain development foundation and want to switch to architecture and those who are doing junior architecture development. , welcome to follow