springboot集成Spring Session
2020-12-13 02:44
标签:代码示例 res ack http ssi 示例 pom spring servlet 创建项目 代码示例 设置地址:http://localhost:8080/A07-springboot-session/boot/set 获取地址:http://localhost:8080/A07-springboot-session/boot/get springboot集成Spring Session 标签:代码示例 res ack http ssi 示例 pom spring servlet 原文地址:https://www.cnblogs.com/Tpf386/p/11050379.html10.1 分布式集群环境下的集成(同域名、同项目)
10.1.1 创建SpringBoot的web支持项目07-springboot-session
10.1.2 在pom.xml文件中添加依赖
dependency>
groupId>org.springframework.sessiongroupId>
artifactId>spring-session-coreartifactId>
version>2.1.2.RELEASEversion>
dependency>
dependency>
groupId>org.springframework.sessiongroupId>
artifactId>spring-session-data-redisartifactId>
version>2.1.1.RELEASEversion>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-data-redisartifactId>
dependency>
10.1.3 在application.properties中配置端口号、上下文根及Redis连接
# 配置内嵌tomcat服务器信息
server.port=8080
server.servlet.context-path=/A07-springboot-session
#配置redis连接信息
spring.redis.host=192.168.132.128
spring.redis.port=6379
spring.redis.password=123456
spring.session.store-type=redis
10.1.4 创建SessionController向session中存取数据
package com.bjpowernode.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
public class SessionController {
@GetMapping("/boot/set")
public String setSession(HttpSession session){
session.setAttribute("url","http://www.bjpowernode.com");
return "set session ok";
}
@GetMapping("/boot/get")
public String getSession(HttpSession session){
String url= (String) session.getAttribute("url");
return url;
}
}
10.1.5 让项目使用SpringSession
package com.bjpowernode.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@SpringBootApplication
@EnableRedisHttpSession
public class A07SpringbootSessionApplication {
public static void main(String[] args) {
SpringApplication.run(A07SpringbootSessionApplication.class, args);
}
}
注解@EnableRedisHttpSession启用redis作为session容器
10.1.6 启动Redis服务
10.1.7 启动SpringBoot程序测试