How to realize the sharing of session login user information among spring cloud services
This article focuses on "how to achieve spring cloud services to share session login user information", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to achieve spring cloud services to share session login user information"!
There are three solutions:
Session sharing of 1.tomcat
Advantages: no additional development is needed, just tomcat cluster is needed.
Disadvantages: tomcat is global session replication, and the session of each tomcat in the cluster stores all session synchronously. In large-scale applications, there are too many users and too many tomcat in the cluster, and the global replication of session will lead to the decline of cluster performance. Therefore, the number of tomcat can not be too large, and the portability of relying on tomcat container is not good (so it is not adopted).
two。 Synchronize session with cookie
This method completely stores the customer's login information in the client's cookie, with the Token in the cookie for each request.
Advantages: abandoning session completely reduces the pressure on the server side.
Disadvantages: it is to expose the information, even if there is an encryption algorithm, there are still security problems. Invalid if the use of cookie is prohibited.
3.redis centrally manages session (a common way)
Advantages: redis is an in-memory database with high read and write efficiency and can be highly available in a cluster environment
Here is the third way of implementation: spring boot integrates session sharing done by redis session.
First, add dependency
Xml code
Org.springframework.boot spring-boot-starter-data-redis org.springframework.session spring-session-data-redis
II. Yml file
Yml code
Server: port: 8080 spring: application: name: test-session redis: password: 12345678 database: 0 host: 127.0.0.1 port: 6379
Add @ EnableRedisHttpSession to the three startup classes
Java code
Import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @ EnableEurekaServer @ SpringBootApplication @ EnableRedisHttpSession / / java Project www.fhadmin.orgpublic class EurekaServerApplication {public static void main (String [] args) SpringApplication.run (EurekaServerApplication.class, args) }} at this point, I believe you have a deeper understanding of "how to achieve spring cloud services to share session login user information". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!