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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to achieve Session sharing in springcloud". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In general, Servlet containers such as Tomcat and Jetty keep Session in memory by default. If it is an application of a single server instance, keeping the Session in the server memory is a very good solution. However, this scheme has a disadvantage, that is, it is not conducive to expansion. At present, more and more applications use distributed deployment to achieve high availability and load balancing. So the question is, if the same application is deployed on multiple servers to provide access through load balancing, how to achieve Session sharing? There are many ways to realize Session sharing, one of which is to use the Session sharing function provided by Tomcat, Jetty and other servers to store the contents of Session in a database (such as MySQL) or cache (such as Redis).
Next, we will use redis to achieve simple and efficient session sharing in the springcloud microservice project.
Create a new spring boot project and name it springcloud-session-redis
POM dependent configuration
4.0.0 com.carry springcloud-session-redis 0.0.1-SNAPSHOT jar springcloud-session-redis Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE UTF-8 UTF-8 1.8 Finchley.SR1 org.springframework.boot Spring-boot-starter-data-redis org.springframework.session spring-session-data-redis org.apache.commons commons-pool2 org.springframework.boot spring-boot-starter-web org.springframework.cloud Spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import Org.springframework.boot spring-boot-maven-plugin
Configuration file
Adding redis, eureka, port and other configurations to application.yml requires JAVA Spring Cloud large enterprise distributed micro-services.
Cloud-built B2B2C e-commerce platform source code: 353 624 7259.
Server: port: 8090spring: application: name: service-session-redis redis: host: 192.168.68.100 port: 6379 password: 123456 timeout: 6000ms lettuce: pool: max-active: 8 max-wait:-1ms max-idle: 8 min-idle: 0 database: 0eureka: client: serviceUrl: defaultZone: http://admin:123456@localhost:8761/eureka/management: Endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*"
Redis Session configuration class
Package com.carry.config;import org.springframework.context.annotation.Configuration;import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;@Configuration@EnableRedisHttpSessionpublic class RedisSessionConfig {}
Add test methods to the control layer Controller
Package com.carry.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController @ RestController@RefreshScopepublic class UserManagementController {/ * redis sesion sharing * * @ param request * @ return * / @ GetMapping ("/ getUser") public String getUser (HttpServletRequest request) {HttpSession session = request.getSession (); String username = (String) session.getAttribute ("username"); if (StringUtils.isEmpty (username)) {username = "testSessionRedis |" + System.currentTimeMillis () Session.setAttribute ("username", username);} System.out.println ("access Port:" + request.getServerPort ()); return username;}} "how springcloud implements Session sharing" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 243
*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.