In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use SpringBoot2.x to integrate Spring-Session to achieve Session sharing function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
1. Preface
Up to now, there are few single-service application architectures, not to mention distributed deployment, or at least multi-point high-availability services. In the case of multiple servers, Seession sharing is a problem that must be faced.
To solve the problem of Session sharing, most people's thinking is relatively clear, and the data that needs to be shared will be stored in some public service, such as cache. Many people use Redis, which manually stores Session in Redis and reads data from Redsi when it is needed. There is no doubt that this scheme is feasible, but there is a lot of work in manual operation.
LZ is implemented by Spring-Session here. It uses proxy filters to intercept Session operations, automatically synchronize data to Redis, and automatically read data from Redis. From then on, operating a distributed Session is like a Session operating a single service, and you can do whatever you want.
two。 Practice
2.1 create a project
Use idea to create a SpringBoot project and add components Web, Spring Session, and Redis. Here idea is version 2019 and SpringBoot is 2.1.6.
Pom.xml file
Org.springframework.boot spring-boot-starter-data-redis-reactive org.springframework.boot spring-boot-starter-web org.springframework.session spring-session-data-redis org.springframework.boot spring-boot-starter-test test
2.2 configure Redis
Spring: redis: port: 6379 password: xofcO46Fy host: 10.17.153.104server: port: 9090
2.3 Test
Code implementation
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
* < session shared controller > * * @ 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 "user:" + session.getAttribute ("user") + ", port:" + port;}}
Maven package is packaged and published to the server server, with a slight process.
Start the project using port 9090 9091 respectively.
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, and save the user variables in the session of the 9090 service.
Then visit http://10.17.158.136:9091/session/get to get the user information from session.
From the above example, you can see that session has been shared, but the testing process requires manual switching of services. In order to better model the real project environment, we configure Nginx for testing.
2.4 configure Nginx
Under the Nginx installation directory conf, edit nginx.conf
Upstream tomcatServer {server 10.17.158.136 weight=1; server 10.17.158.136 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;}
Here we only need to configure a simple load balancer, and the port is 9000. All localhost:9000 will be distributed to the upstream service configured by upstream according to a certain policy (here, it is distributed by weight, just like configuring weight=1, randomly distributed; nginx defaults to polling policy).
After the configuration is complete, start Nginx
/ apps/test/software/nginx/nginx-1.6.2/sbin/nginx
First, access the http://10.17.158.136:9000/session/set, to save the data to the seesion, and you can see from the following figure that the service on port 9090 handled the request.
Then in the access / get request, the user information is obtained from the service on port 9091, and the test is complete.
Thank you for reading this article carefully. I hope the article "how to use SpringBoot2.x to integrate Spring-Session to achieve Session sharing function" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 235
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.