在springcloud微服務項目中,使用redis實現session共享是比較主流的,簡單高效,直接代碼實踐:java
<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>
service-user-session-redis-dev.ymlnode
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/
RedisSessionConfigweb
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 { }
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; } }
springcloud項目中通過網關zuul轉發請求後發生session失效問題,這是因爲zuul默認會丟棄原來的session並生成新的session,解決方法網關配置文件redis
service-zuul-dev.yml 中添加 sensitiveHeaders: "*"spring
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/
依次啓動EUREKA、SERVICE-CONFIG、SERVICE-USER-SESSION-REDIS、SERVICE-ZUUL微信
屢次訪問網關地址http://localhost:1100/user-session/getUsersession
一、發現redis中已經有了session的信息架構
二、session中的信息爲redis中獲取的,而且是同一個sessionapp
總結:springcloud整合redis實現session共享容易發生錯誤的地方就是通過網關轉發會使原session失效,多數人會認爲是整合沒有成功,仔細看zuul的源碼或者直接訪問微服務去嘗試,不難發現問題的所在。cors
架構師Q羣:618578034
spring cloud社區羣(需加微信拉您進入)
微信公衆號:java架構師修行
本公衆號將要使用一年的時間,按照JAVA高級軟件架構師實戰培訓的路線發佈一期完整的架構文章,難度由淺入深,適合有必定開發基礎想轉架構和正在作初級架構開發的人員學習,歡迎關注