session 是服務器用來保存用戶操做的一系列會話信息,由 Web 容器進行管理。單機狀況下,不存在 session 共享的狀況,分佈式狀況下,若是不進行 session 共享會出現請求落到不一樣機器要重複登陸的狀況 。單點登陸能夠用 cookie+redis 的方式,第一次登錄生成 token,將 token 和用戶以鍵值對的方式存入 redis。並將 token 寫入cookie 返回到瀏覽器。之後每次請求 cookie 都會攜帶上 token。服務端獲取 token 而後去 redis 中查找是否存在此用戶從而實現單點登陸。java
項目代碼結構圖結構web
準備工做redis
啓動 eureka 註冊中心 eureka-register,而後新建兩個 springboot 服務分別是 four-sample 和 five-sample。spring
以 four-sample 演示操做瀏覽器
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring boot 與redis應用基本環境配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--spring session 與redis應用基本環境配置,須要開啓redis後纔可使用,否則啓動Spring boot會 報錯 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
複製代碼
在 application.yml 資源文件中添加 redis 和 eureka 相關配置springboot
server:
port: 8086
spring:
application:
name: spring-cloud-producer1
redis:
host: 127.0.0.1
port: 6379
timeout: 5000ms
password:
database: 0
jedis:
pool:
max-active: 50
max-wait: 3000ms
max-idle: 20
min-idle: 2
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
複製代碼
package com.tuhu.foursample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
* @author chendesheng
* @since 2019-08-06
*/
@SpringBootApplication
@EnableEurekaClient
@EnableRedisHttpSession
@RestController
public class FourSampleApplication {
public static void main(String[] args) {
SpringApplication.run(FourSampleApplication.class, args);
}
@GetMapping(value="/setSession")
public String setSession(HttpServletRequest request){
Map<String,Object> map = new HashMap(10);
map.put("name","超級管理員");
map.put("account","admin");
request.getSession().setAttribute("userSession",map);
String sessionId = request.getSession().getId();
return sessionId;
}
}
複製代碼
使用 @EnableRedisHttpSession
來開啓 spring session 支持,使用 @EnableEurekaClient
將服務註冊到註冊中興。five-sample 服務的配置如法炮製,只需修改端口號和程序名便可。服務器
five-sample 服務配置完之後,咱們須要在主程序拿到 userSession
,看看 session 是否與服務 four-sample 裏的一致。five-sample 主程序代碼以下:cookie
package com.tuhu.fivesample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
* @author chendesheng
* @since 2019-08-06
*/
@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableRedisHttpSession
public class FiveSampleApplication {
public static void main(String[] args) {
SpringApplication.run(FiveSampleApplication.class, args);
}
@GetMapping(value="/getSession")
public Map<String,Object> getSession(HttpServletRequest request){
String sessionId = request.getSession().getId();
Object obj = request.getSession().getAttribute("userSession");
Map<String,Object> map = new HashMap(10);
map.put("sessionId",sessionId);
map.put("user",obj);
return map;
}
}
複製代碼
如今咱們啓動註冊中心 eureka-register 和兩個服務 four-sample 和 five-samplesession
用 postman 請求 four-sample,咱們能夠看到app
再用 postman 請求 five-sample,咱們能夠看到
ok 這樣咱們就實現了 session 共享,能夠看到 SpringBoot 集成 redis 實現 session 共享是如此的簡單,僅僅須要一個依賴就能夠解決。咱們寫了一些代碼,也作了一些配置,可是全都和 Spring Session 無關,配置是配置 Redis,代碼就是普通的 HttpSession,和 Spring Session 沒有任何關係!惟一和 Spring Session 相關的,可能就是我在一開始引入了 Spring Session 的依賴吧!