SpringBoot+Redis实现HttpSession共享
前提:需要使用redis做session存储 一、效果演练(这里使用SpringBoot工程,Spring同理) 1.一个工程使用两个端口启用,或者两个工程两个端口启用(这里使用一个工程两个端口号启动)
(1)#设置properties配置的服务器端口号
server.port=8081
然后启动。
(2)#设置properties配置的服务器端口号
server.port=8082
然后启动。
到此,已经启动两个工程。 2.分别访问工程8081和8082
(1)8081
(2)8082
(3)8081添加session信息,name="丁洁"
(4)再次查看8081和8082的session信息
(5)清除8082的session name 再分别查看8081和8082的session信息
二、源码分享
1.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.szw</groupId>
<artifactId>springboot_nginx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_nginx</name> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath></relativePath>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.test.skip>true</maven.test.skip>
<skipTests>true</skipTests>
<java.version>1.8</java.version>
<start-class>com.nginx.session.NginxSessionApplication</start-class>
</properties> <dependencies>
<!-- 使用web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 使用aop模板启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring-Session -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<!-- 打可运行的jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<!-- 打源码包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> <!-- maven私服deploy -->
<distributionManagement>
<repository>
<id>releases</id>
<name>Releases</name>
<url>http://192.168.3.71:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Snapshots</name>
<url>http://192.168.3.71:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement> <repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories> </project>
2.application.properties
#设置项目contex路径
#server.context-path=/nginx/session
#服务器端口号
server.port=8081
#server.port=8082
#会话session超时时长
server.session.timeout=1800 ## redis地址
spring.redis.host=192.168.159.129
## redis端口
spring.redis.port=7001
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=300
## Redis数据库索引(默认为0)
spring.redis.database=0
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=100
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=20
## 连接超时时间(毫秒)
spring.redis.timeout=60000
3.controller
package com.nginx.session.controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloWorldContoller {
@Value("${server.port}")
private String port; @RequestMapping("/")
public String helloWold(HttpServletRequest req){
HttpSession session = req.getSession();
Object obj = req.getSession().getAttribute("name");
String name = null!=obj?obj.toString():null;
return "<h2>hello world<br/>我的端口号是:"+port+"<br/>我的sessionId:"+session.getId()+"<br/>session name = "+name+"</h2>";
} @RequestMapping("add/{name}")
public String addSession(HttpServletRequest req,@PathVariable("name")String name){
if(StringUtils.isEmpty(name)){
return "add/{name}不能为空";
}else{
req.getSession().setAttribute("name", name);
return "<h1>设置session name = "+name+"</h1>";
}
} @RequestMapping("get")
public String getSession(HttpServletRequest req){
Object obj = req.getSession().getAttribute("name");
String name = null!=obj?obj.toString():null;
return "<h1>获取session name = "+name+"</h1>";
} @RequestMapping("clear")
public String clearSession(HttpServletRequest req){
req.getSession().removeAttribute("name");
Object obj = req.getSession().getAttribute("name");
String name = null!=obj?obj.toString():null;
return "<h1>清除session name = "+name+"</h1>";
}
}
4.Application启动类
package com.nginx.session; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class NginxSessionApplication { public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(NginxSessionApplication.class, args);
}
}
5.工程截图