Spring Boot relies get a sharing session, there is no easier than this scheme up!

Some people may feel a bit exaggerated title, in fact, no exaggeration, the title does not use any rhetoric! Carefully reading this article, you will know Song Ge said is right up!

<!--more-->

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 :

Spring Boot relies get a sharing session, there is no easier than this scheme up!

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):

Spring Boot relies get a sharing session, there is no easier than this scheme up!

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

首先 创建一个 Spring Boot 工程,引入 Web、Spring Session 以及 Redis:

Spring Boot relies get a sharing session, there is no easier than this scheme up!

创建成功之后,pom.xml 文件如下:

<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 。

接下来 ,项目打包:

Spring Boot relies get a sharing session, there is no easier than this scheme up!

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

java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=8080
java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=8081

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

Spring Boot relies get a sharing session, there is no easier than this scheme up!

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

Spring Boot relies get a sharing session, there is no easier than this scheme up!

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

1.4 引入 Nginx

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

Spring Boot relies get a sharing session, there is no easier than this scheme up!

在这段配置中:

  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 启动成功后,我们首先手动清除 Redis 上的数据,然后访问 192.168.66.128/set 表示向 session 中保存数据,这个请求首先会到达 Nginx 上,再由 Nginx 转发给某一个 Spring Boot 实例:

Spring Boot relies get a sharing session, there is no easier than this scheme up!

如上,表示端口为 8081Spring Boot 处理了这个 /set 请求,再访问 /get 请求:

Spring Boot relies get a sharing session, there is no easier than this scheme up!

可以看到,/get 请求是被端口为 8080 的服务所处理的。

2 总结

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.

Well, we stop here, this article related cases I have uploaded to GitHub, you can download yourself:https://github.com/lenve/javaboy-code-samples

Public concern number code kid, animal husbandry, micro-focus on Spring Boot + service, regular video tutorials to share, after concerns reply Java, Java dry Song Ge receive carefully prepared for you!

Spring Boot relies get a sharing session, there is no easier than this scheme up!

Guess you like

Origin blog.51cto.com/9806927/2404563