本文简单介绍了基于SpringBoot实现多项目的Session共享机制。
1.pom.xml导入需要的jar
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2.创建RedisConfig
package com.pk.session.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @Description TODO
* @author:pk
* @date 2018/9/20 13:43
* @Version 1.0
*/
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis;
@Value("${spring.redis.password}")
private String password;
@Bean
public JedisPool redisPoolFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
return jedisPool;
}
}
3.在application.properties中加入Redis的配置信息
server.port=8081
spring.redis.database=1
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=10000
4.书写测试控制类
package com.pk.session_1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
* @Description springboot RedisSession共享
* @author:pk
* @date 2018/9/20 13:42
* @Version 1.0
*/
@Controller
public class TestController {
@RequestMapping(value = "/setSession",method = {RequestMethod.GET})
@ResponseBody
public Map<String,Object> setSession(HttpServletRequest request){
Map<String,Object> map = new HashMap<>();
request.getSession().setAttribute("msg",request.getRequestURL());
map.put("request url",request.getRequestURL());
return map;
}
@RequestMapping(value = "/getSession",method = {RequestMethod.GET})
@ResponseBody
public Map<String,String> getSession(HttpServletRequest request){
Map<String,String> map = new HashMap<>();
map.put("sessionId",request.getSession().getId());
map.put("msg",request.getSession().getAttribute("msg").toString());
return map;
}
}
以上就是第一个服务的全部内容,复制项目,然后在修改application.propproperties中项目的端口号,启动这个两个项目。
在同一浏览器中进行请求,如:http://localhost:8081/setSession,这时会将session信息存取到Redis中。
我们首先请求:http://localhost:8081/getSession,这时浏览器会将我们前面存取的session信息输出。
我们再请求:http://localhost:8082/getSession,我们可以看到和上面请求的输出的session信息一致。