作者:远洋箭 | 来源:互联网 | 2024-09-30 17:58
1.新建Maven项目,pom.xml加入如下:
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
2.8.0
1.8
1.8
UTF-8
UTF-8
zh_CN
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-web
com.alibaba
fastjson
1.2.47
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.session
spring-session-data-redis
org.apache.commons
commons-pool2
redis.clients
jedis
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
org.springframework.boot
spring-boot-maven-plugin
com.meiteedu.WxMpApplication
repackage
2.新建redis的配置类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
/**
* 这个类用配置redis服务器的连接
*
* @author mayn
*
*/
// maxInactiveIntervalInSeconds为SpringSession的过期时间(单位:秒)
@EnableRedisHttpSession(maxInactiveIntervalInSecOnds= 1800)
public class SessionConfig {
// 冒号后的值为没有配置文件时,制动装载的默认值
@Value("${redis.hostname:localhost}")
String hostName;
@Value("${redis.port}")
int port;
@Value("${redis.password}")
String password;
@SuppressWarnings("deprecation")
@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory cOnnection= new JedisConnectionFactory();
connection.setPort(port);
connection.setHostName(hostName);
connection.setPassword(password);
return connection;
}
}
3.新建初始化redis配置类
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
//初始化Session配置
public class SessionInitializer extends AbstractHttpSessionApplicationInitializer{
public SessionInitializer() {
super(SessionConfig.class);
}
}
4.新建一个启动类
@SpringBootApplication
@RestController
public class TestSessionApp {
@Value("${server.port}")
private String serverPort;
// 创建session 会话
// http://127.0.0.1:8080/createSession?name=aaa
@RequestMapping("/createSession")
public String createSession(HttpServletRequest request, String name) {
HttpSession session = request.getSession();
System.out.println("存入Session sessionid:信息" + session.getId() + ",name:" + name + ",serverPort:" + serverPort);
session.setAttribute("name", name);
return serverPort + "success";
}
// 获取session 会话
@RequestMapping("/getSession")
public Object getSession(HttpServletRequest request) {
// 参数为true的情况下,客户端使用对应的sessionid查不到对应的session,会创建一个新的session
// 参数为false的情况下,客户端使用对应的sessionid查不到对应的session,不会创建一个新的session
HttpSession session = request.getSession(false);
if (session == null) {
return serverPort + "服务器上没有session...";
}
System.out.println("获取Session sessionid:信息" + session.getId() + ",serverPort:" + serverPort);
Object value = session.getAttribute("name");
return serverPort + "-" + value;
}
@RequestMapping("/getServerPort")
public String getServerPort () {
return serverPort;
}
public static void main(String[] args) {
SpringApplication.run(TestSessionApp.class, args);
}
}
5.启动两个集群环境,端口号分别为8080和8081
访问http://127.0.0.1:8080/createSession?name=aaa,往8080服务器session上存取数据
此时8081服务器的session也可以拿到8080服务器存取的数据了
查看redis,session保存在redis里面