SpringBoot2.x achieve the integration of Spring-Session Session sharing

SpringBoot2.x achieve the integration of Spring-Session Session sharing

1 Introduction

Today, there are very few single service application architecture, do not say use a distributed architecture deployment, or at least multi-point high-availability services. In the case of multiple servers, Seession share issue is the need to face.

Session solve shared problems, most people's ideas are relatively clear, the need to share data in the presence of a public service, such as caching. Many people have used the Redis, the presence of the Redis Session manually, when required, and then read data from the Redsi. There is no doubt that this is feasible, but the workload in manual operation is indeed a lot.

In the Spring-Session LZ employed herein to achieve. It uses a proxy filter, Session interception operation, the data is automatically synchronized to the Redis, and automatically reads data from Redis. Since then, the operation of a distributed operating as a single Session service Session, as can do whatever they want.

2. Practice
2.1 Create Project

Use idea creation SpringBoot project, add components Web, Spring Session and Redis. My idea here is the 2019 version, SpringBoot is 2.1.6.

pom.xml file

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis-reactive</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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
2.2 Configuration Redis
spring:
  redis:
      port: 6379
      password: xofcO46Fy
      host: 10.17.153.104
server:
  port: 9090
2.3 Test

Code

package com.xiaoqiang.sessionshare.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * SessionShareController <br>
 * 〈session共享控制器〉
 *
 * @author XiaoQiang
 * @create 2019-7-6
 * @since 1.0.0
 */
@RestController
@RequestMapping(value = "/session")
public class SessionShareController {

    @Value("${server.port}")
    Integer port;


    @GetMapping(value = "/set")
    public String set(HttpSession session){
        session.setAttribute("user","wangwq8");
        return String.valueOf(port);
    }

    @GetMapping(value = "get")
    public String get(HttpSession session){
        return "用户:"+session.getAttribute("user")+",端口:"+port;
    }
}

maven package packaged server published to the server, here.

90,909,091 were used to start the port project.

nohup java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=9090 &

nohup java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=9091 &

First visit http://10.17.158.136:9090/session/set, save user variables in session 9090 this service;

Then visit http://10.17.158.136:9091/session/get, get get user information from the session.

From the examples above, it can be seen sharing session has been achieved, but the testing process is the need to manually switch service. In order to better model real project environment, for which we configure Nginx, to be tested.

2.4 Configuration Nginx

In Nginx installation directory conf, edit nginx.conf,

 upstream tomcatServer {
        server 10.17.158.136:9092 weight=1;
        server 10.17.158.136:9091 weight=2;
        }

    server {
        listen       9000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://tomcatServer;
            proxy_redirect default;
            #root   html;
            #index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

在这里我们只需要配置简单的负载均衡,端口是9000。所有localhost:9000都会按一定策略(这里是按权重分发,配置weight=1一样,随机分发的;nginx默认是轮询策略)分发到上游服务upstream配置的服务上。

配置完成后,启动Nginx;

/apps/test/software/nginx/nginx-1.6.2/sbin/nginx

首先访问http://10.17.158.136:9000/session/set,向seesion中保存数据,从下图中可知9090端口的服务处理了该请求。

然后在访问/get请求,是从9091端口的服务获取得到的用户信息,至此,测试完成。

3.总结

本文主要是Spring Session的简单使用,从上面可以看出,除了引入了Spring Session的jar, 其他方面,不管是代码还是配置,都与之没有什么关联,就相当于在操作最常用的HttpSession,在实际项目中用起来也是相当方便。

样例已上传github,地址:https://github.com/lanxuan826/sample-library/tree/master/sessionshare,有兴趣可下载测试。

另外,此文是从松哥博客中得到启示,在此推荐:https://blog.csdn.net/u012702547/article/list/2?

,还推荐一篇关于Spring Session原理的博客:https://blog.csdn.net/u010648555/article/details/79491988

Guess you like

Origin www.cnblogs.com/lanxuan826/p/11221603.html