日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Externalizing Session State for a Spring Boot Application Using Spring-Session

發布時間:2025/3/21 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Externalizing Session State for a Spring Boot Application Using Spring-Session 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

Spring-session is a very cool new project that aims to provide a simpler way of managing sessions in Java based web applications. One of the features that I explored with spring-session recently was the way it supports externalizing session state without needing to fiddle with the internals of specific web containers like Tomcat or Jetty.

To test spring-session I have used a shopping cart type application(available here) which makes heavy use of session by keeping the items added to the cart as a session attribute, as can be seen from these screenshots:

Consider first a scenario without Spring session. So this is how I have exposed my application:

I am using nginx to load balance across two instances of this application. This set-up is very easy to run using Spring boot, I brought up two instances of the app up using two different server ports, this way:

mvn spring-boot:run -Dserver.port=8080 mvn spring-boot:run -Dserver.port=8082

and this is my nginx.conf to load balance across these two instances:

events {worker_connections 1024; } http {upstream sessionApp {server localhost:8080;server localhost:8082;}server {listen 80;location / {proxy_pass http://sessionApp;} } }

I display the port number of the application in the footer just to show which instance is handling the request.

If I were to do nothing to move the state of the session out the application then the behavior of the application would be erratic as the session established on one instance of the application would not be recognized by the other instance - specifically if Tomcat receives a session id it does not recognize then the behavior is to create a new session.

Introducing Spring session into the application

There are container specific ways to introduce a external session stores - One example is here, where Redis is configured as a store for Tomcat. Pivotal Gemfire provides a module to externalize Tomcat's session state.

The advantage of using Spring-session is that there is no dependence on the container at all - maintaining session state becomes an application concern. The instructions on configuring an application to use Spring session is detailed very well at the Spring-session site, just to quickly summarize how I have configured my Spring Boot application, these are first the dependencies that I have pulled in:

<dependency><groupId>org.springframework.session</groupId><artifactId>spring-session</artifactId><version>1.0.0.BUILD-SNAPSHOT</version> </dependency> <dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId><version>1.0.0.BUILD-SNAPSHOT</version> </dependency> <dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.4.1.RELEASE</version> </dependency> <dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.4.1</version> </dependency>

and my configuration to use Spring-session for session support, note the Spring Boot specific FilterRegistrationBean which is used to register the session repository filter:

import org.springframework.boot.context.embedded.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.session.web.http.SessionRepositoryFilter; import org.springframework.web.filter.DelegatingFilterProxy;import java.util.Arrays;@Configuration @EnableRedisHttpSession public class SessionRepositoryConfig {@Bean@Order(value = 0)public FilterRegistrationBean sessionRepositoryFilterRegistration(SessionRepositoryFilter springSessionRepositoryFilter) {FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();filterRegistrationBean.setFilter(new DelegatingFilterProxy(springSessionRepositoryFilter));filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));return filterRegistrationBean;}@Beanpublic JedisConnectionFactory connectionFactory() {return new JedisConnectionFactory();} }

And that is it! magically now all session is handled by Spring-session, and neatly externalized to Redis.

If I were to retry my previous configuration of using nginx to load balance two different Spring-Boot applications using the common Redis store, the application just works irrespective of the instance handling the request. I look forward to further enhancements to this excellent new project.

The sample application which makes use of Spring-session is available here: https://github.com/bijukunjummen/shopping-cart-cf-app.git。

Reference:

  • Externalizing Session State for a Spring Boot Application Using Spring-Session:https://dzone.com/articles/externalizing-session-state;

  • Spring Session - Spring Boot:http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html;

  • Scaling out with Spring Session:https://blog.jayway.com/2015/05/31/scaling-out-with-spring-session/;

  • 利用spring session解決共享Session問題:http://blog.csdn.net/patrickyoung6625/article/details/45694157;

  • SpringSession原理解析:https://segmentfault.com/a/1190000004369392;

  • 通過Spring Session實現新一代的Session管理:http://www.infoq.com/cn/articles/Next-Generation-Session-Management-with-Spring-Session.

轉載于:https://my.oschina.net/dabird/blog/802362

總結

以上是生活随笔為你收集整理的Externalizing Session State for a Spring Boot Application Using Spring-Session的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。