Spring Boot + Redis for session sharing

Source: Internet
Author: User
Tags redis server nginx load balancing

This is a tutorial to implement session sharing with spring boot + redis.

In the spring boot documentation, tell us to add @enableredishttpsession to enable spring session support, which is configured as follows:

    1. @Configuration   

    2. @EnableRedisHttpSession   

    3. public  class  redissessionconfig {  

    4. }  

and @enableredishttpsession This annotation is provided by Spring-session-data-redis, so add it in the Pom.xml file:

    1. <dependency>  

    2.         < groupid>org.springframework.boot</groupid>  

    3.          <artifactid>spring-boot-starter-redis</artifactid>  

    4. </dependency>  

    5. <dependency>  

    6.          <groupid>org.springframework.session</groupid>  

    7.         <artifactid>spring-session-data-redis</ artifactid>  

    8. </dependency>  

Next, you need to configure the location of the Redis server in Application.properties, where we use this machine:

    1. Spring.redis.host=localhost

    2. spring.redis.port=6379

In this way, the simplest spring boot + REDIS Implementation session sharing is complete, below to test.

First we open two Tomcat services, with ports 8080 and 9090, set in Application.properties " " :

    1. server.port=8080

Next, define a controller:

  1. @RestController

  2. @RequestMapping (value = "/admin/v1")

  3. Public class Quickrun {

  4. @RequestMapping (value = "/first", method = Requestmethod.get)

  5. Public map<string, object> firstresp (HttpServletRequest request) {

  6. map<string, object> map = new hashmap<> ();

  7. Request.getsession (). SetAttribute ("Request Url", Request.getrequesturl ());

  8. Map.put ("Request Url", Request.getrequesturl ());

  9. return map;

  10. }

  11. @RequestMapping (value = "/sessions", method = Requestmethod.get)

  12. Public Object Sessions (HttpServletRequest request) {

  13. map<string, object> map = new hashmap<> ();

  14. Map.put ("SessionId", Request.getsession (). GetId ());

  15. Map.put ("message", Request.getsession (). getattribute ("map"));

  16. return map;

  17. }

  18. }

After starting the access test, first access 8080-port Tomcat, return to get "" :

    1. {"request Url":"Http://localhost:8080/admin/v1/first"}

Next, we visit the 8080 port of sessions and return:

    1. { "SessionId": "message":

Finally, access to port 9090, sessions, returns:

    1. { "SessionId": "message":

As can be seen, 8080 and 90,902 servers return the results, the realization of the session sharing

If you are accessing the first of Port 9090 at this point, return:

    1. {"request Url":"Http://localhost:9090/admin/v1/first"}

The sessions of two servers are returned:

    1. { "SessionId": "message":

Using spring boot + Redis to achieve session sharing is very simple and useful, with nginx load balancing, you can achieve distributed applications.

This time Redis does not carry out the master-slave, read-write separation and so on configuration (_ (: З"∠) _ is actually Bo Master lazy, has not tried ...)

Moreover, Nginx single point of failure is also a barrier to our application ... There may be an improved version of this blog later, such as load balancing with zookeeper, so please look forward to it.


Spring Boot + Redis implementation session sharing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.