在使用 spring - session 方式实现分布式session一致性的基础上再加上代码
创建TestTokenController
package com.itmayiedu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itmayiedu.session.TokenService;
@RestController
public class TestTokenController {
@Autowired
private TokenService tokenService;
// 1. 使用token方式代替Session功能 session 原理在服务器端创建 session 返回对应的 sessionid 给客户端
@RequestMapping("/put")
public String put(String object){
return tokenService.put(object);
}
@RequestMapping("/get")
public String get(String token){
return tokenService.get(token);
}
// 生成好的 token 如何存放? 本地 移动端 存放本地文件 浏览器 存放 cookie
// http 请求如何传递呢? 最好建议放在请求头里面
}
创建RedisService
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
* 功能说明: <br>
* 创建作者:每特教育-余胜军<br>
* 创建时间:2018年8月1日 下午4:07:32<br>
* 教育机构:每特教育|蚂蚁课堂<br>
* 版权说明:上海每特教育科技有限公司版权所有<br>
* 官方网站:www.itmayiedu.com|www.meitedu.com<br>
* 联系方式:qq644064779<br>
* 注意:本内容有每特教育学员共同研发,请尊重原创版权
*/
@Component
public class RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
// public void set(String key, Object object, Long time) {
// stringRedisTemplate.opsForValue();
// // 存放String 类型
// if (object instanceof String) {
// setString(key, object);
// }
// // 存放 set类型
// if (object instanceof Set) {
// setSet(key, object);
// }
// // 设置有效期 以秒为单位
// stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
// }
// redis 在服务器集群的时候 分布式缓存可以共享
public void setString(String key, Object object) {
// 开启事务权限
// stringRedisTemplate.setEnableTransactionSupport(true);
try {
// 开启事务 begin
// stringRedisTemplate.multi();
String value = (String) object;
stringRedisTemplate.opsForValue().set(key, value);
System.out.println("存入完毕,马上开始提交redis事务");
// 提交事务
// stringRedisTemplate.exec();
} catch (Exception e) {
// 需要回滚事务
// stringRedisTemplate.discard();
}
}
public void setSet(String key, Object object) {
Set<String> value = (Set<String>) object;
for (String oj : value) {
stringRedisTemplate.opsForSet().add(key, oj);
}
}
public String getString(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
}
创建TokenService
package com.itmayiedu.session;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TokenService {
@Autowired
private RedisService redisService;
//使用token方式代替Session功能
public String put(String value){
//1. 判断是否为空
if(value == null){
return null;
}
// 2.先生成对应的token(token 实际上等于key)
String token = getToken();
// 3. 存入在redis中
redisService.setString(token, value);
return token;
}
public String get(String token){
return redisService.getString(token);
}
public String getToken() {
return UUID.randomUUID().toString();
}
}
启动nginx、redis
启动项目
浏览器测试
拿到token,通过token在拿到值
然后一直刷新都可以拿到值,这里没有演示拿到服务器的端口号,没有这么明显区分集群