SpringBoot a move to get session, so simple you sure do not look at this article


SpringBoot a move to get session, so simple you sure do not look at this article


In conventional single service architecture, in general, only one server, it does not share Session problems, but in a distributed / cluster projects, the Session sharing is a problem to be faced, we look at a simple architecture diagram :

SpringBoot a move to get session, so simple you sure do not look at this article


In this architecture, there will be some single service problem does not exist, for example, the client initiates a request, the request after arriving on Nginx, is forwarded Nginx on Tomcat A, then to the session is saved in a on Tomcat A parts of the data, again a next request, the request is forwarded to the Tomcat B, at this time go Session acquired data, before the data is not found. For solving this kind of problem, the idea is very simple, is the need to share data between the various services, saved to a public place (mainstream programs that Redis):

SpringBoot a move to get session, so simple you sure do not look at this article


When all Tomcat need to write data to the Session, both Redis to write, when all Tomcat need to read the data are read from the Redis. In this way, different services can use the same Session data.

Such solutions can be achieved manually by the developer, which store data manually to Redis, the manual reads data from Redis, the equivalent to using some of the Redis client tools to achieve this function, no doubt, done manually or workload pretty big.

A simplified scheme is used to achieve this function Session Spring, Spring Session proxy filter is used in the Spring, all operations Session intercepted, the data will be automatically synchronized to the Redis, or automatically read from the Redis data.

For developers, all about Session synchronous operation is transparent, developers use the Spring Session, once the configuration is complete, specific usage like using an ordinary Session same.

1 combat

1.1 Create Project

First create a Spring Boot project, the introduction of Web, Spring Session and Redis:

SpringBoot a move to get session, so simple you sure do not look at this article


After creating a successful, pom.xml file as follows:

<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-web</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.session</groupId>
 <artifactId>spring-session-data-redis</artifactId>
 </dependency>
</dependencies>

注意:

这里我使用的 Spring Boot 版本是 2.1.4 ,如果使用当前最新版 Spring Boot2.1.5 的话,除了上面这些依赖之外,需要额外添加 Spring Security 依赖(其他操作不受影响,仅仅只是多了一个依赖,当然也多了 Spring Security 的一些默认认证流程)。

1.2 配置 Redis

spring.redis.host=
192.168
.
66.128
spring.redis.port=
6379
spring.redis.password=
123
spring.redis.database=
0

这里的 Redis ,我虽然配置了四行,但是考虑到端口默认就是 6379 ,database 默认就是 0,所以真正要配置的,其实就是两行。

1.3 使用

配置完成后 ,就可以使用 Spring Session 了,其实就是使用普通的 HttpSession ,其他的 Session 同步到 Redis 等操作,框架已经自动帮你完成了:

@RestController
public
 
class
 
HelloController
 {
 
@Value
(
"${server.port}"
)
 
Integer
 port;
 
@GetMapping
(
"/set"
)
 
public
 
String
 set(
HttpSession
 session) {
 session.setAttribute(
"user"
, 
"javaboy"
);
 
return
 
String
.valueOf(port);
 }
 
@GetMapping
(
"/get"
)
 
public
 
String
 get(
HttpSession
 session) {
 
return
 session.getAttribute(
"user"
) + 
":"
 + port;
 }
}

考虑到一会 Spring Boot 将以集群的方式启动 ,为了获取每一个请求到底是哪一个 Spring Boot 提供的服务,需要在每次请求时返回当前服务的端口号,因此这里我注入了 server.port 。

接下来 ,项目打包:

SpringBoot a move to get session, so simple you sure do not look at this article


打包之后,启动项目的两个实例:

java -jar sessionshare-
0.0
.
1
-SNAPSHOT.jar --server.port=
8080
java -jar sessionshare-
0.0
.
1
-SNAPSHOT.jar --server.port=
8081

然后先访问 localhost:8080/set 向 8080 这个服务的 Session 中保存一个变量,访问完成后,数据就已经自动同步到 Redis 中 了 :

SpringBoot a move to get session, so simple you sure do not look at this article


然后,再调用 localhost:8081/get 接口,就可以获取到 8080 服务的 session中的数据:

SpringBoot a move to get session, so simple you sure do not look at this article


此时关于 session 共享的配置就已经全部完成了,session 共享的效果我们已经看到了,但是每次访问都是我自己手动切换服务实例,因此,接下来我们来引入 Nginx ,实现服务实例自动切换。

1.4 引入 Nginx

很简单,进入 Nginx 的安装目录的 conf 目录下(默认是在 /usr/local/nginx/conf),编辑 nginx.conf 文件:

SpringBoot a move to get session, so simple you sure do not look at this article


在这段配置中:

  1. upstream 表示配置上游服务器

  2. javaboy.org 表示服务器集群的名字,这个可以随意取名字

  3. upstream 里边配置的是一个个的单独服务

  4. weight 表示服务的权重,意味者将有多少比例的请求从 Nginx 上转发到该服务上

  5. location 中的 proxy_pass 表示请求转发的地址, / 表示拦截到所有的请求,转发转发到刚刚配置好的服务集群中

  6. proxy_redirect 表示设置当发生重定向请求时,nginx 自动修正响应头数据(默认是 Tomcat 返回重定向,此时重定向的地址是 Tomcat 的地址,我们需要将之修改使之成为 Nginx 的地址)。

配置完成后,将本地的 Spring Boot 打包好的 jar 上传到 Linux ,然后在 Linux 上分别启动两个 Spring Boot 实例:

nohup java -jar sessionshare-
0.0
.
1
-SNAPSHOT.jar --server.port=
8080
 &
nohup java -jar sessionshare-
0.0
.
1
-SNAPSHOT.jar --server.port=
8081
 &

其中

  • nohup 表示当终端关闭时,Spring Boot 不要停止运行

  • & 表示让 Spring Boot 在后台启动

配置完成后,重启 Nginx:

/usr/local
/nginx/sbin/nginx -s reload

Nginx After a successful start, we first of all the data on hand to clear Redis, and then save the data access 192.168.66.128/set represented in the session, the request will first arrive on Nginx, and then forwarded by Nginx to one SpringBoot example:

SpringBoot a move to get session, so simple you sure do not look at this article


As above, the port 8081 of the process of this SpringBoot / set requests, then access / get request:

SpringBoot a move to get session, so simple you sure do not look at this article


You can see, / get request is port 8080 service handled.

2 summary

This paper to introduce the use of Spring Session, in addition to also involve some use Nginx, although a long article, but in fact nothing Spring Session of the configuration.

We wrote some code to do some configuration, but all unrelated and Spring Session, the configuration is configured Redis, the code is common HttpSession, and Spring Session nothing to do!

Spring Session unique and relevant, perhaps, that I introduced at the beginning of the Spring Session of the dependence of it!

If you have not used the Spring Session in SSM architecture, we may not be easy to understand how easy it is to use Spring Session in Spring Boot in there, because the SSM architecture, the Spring Session of use to configure three places, one is web.xml configure the proxy filter, and then configure the Spring container Redis, and finally configure the Spring Session, the steps still somewhat cumbersome, and Spring Boot directly help us eliminating these cumbersome steps! Not have to configure the Spring Session.

Finally, remember to give me some followers Oh, will share daily java related articles


Guess you like

Origin blog.51cto.com/14456091/2431195