下面的内容要给大家讲到的就是springsession跨域方面的问题,一起来详细的了解一下springsession跨域方面的内容吧。
问题:
在上面的架构当中,客户端发起了一个请求,在这个请求到达了nginx上面之后,就被Nginx转达到了Tomcat A上面,之后,在Tomcat A上面往Session当中保存了一份数据,之后,再来一个请求,这个请求就被转发到了Tomcat B上面,这个时候,再去Session当中获取数据,发现,没有之前的数据。
思路:
将各个服务之间需要共享的数据,保存到一个公共的地方,主流方案-Redis。
代码(实践):
1、创建工程-pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.tsvv</groupId> <artifactId>Spring-Session</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>Spring-Session</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml中配置Redis
spring: redis: host: 192.168 .2 .102 port: 6379
配置完成
package com.tsvv.controller; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class IndexController { @Value("${server.port}") private Integer port; @RequestMapping("/set") public String setPort(HttpSession session) { session.setAttribute("tsvv", "Spring Session"); return String.valueOf(port); } @RequestMapping("/get") public String getPort(HttpSession session) { return session.getAttribute("tsvv") .toString() + ":" + port; } }
2、项目打包之后,得到两个war(分别以8091、8092端口启动),nohup-当终端关闭的时候,Spring Boot不要停止运行;&-Spring Boot在后台启动;
nohup java - jar SpringSession8091.war--server.port = 8091 & nohup java - jar SpringSession8092.war--server.port = 8092 &
3、配置,启动Nginx
server { listen 80; server_name 192.168 .2 .102;# charset koi8 - r;# access_log logs / host.access.log main; location / { proxy_pass http: //tsvvLinux; } } upstream tsvvLinux { server 192.168 .2 .102: 8091; server 192.168 .2 .102: 8092; }
4、运行
本文源于网络,仅供参考。
你对java有兴趣吗?你想了解和java相关的经典实例吗?java经典实例可以用过奇Q工具网来了解哦。
推荐阅读: