Spring boot + Redis for session sharing management

Source: Internet
Author: User
Tags bind session id redis redis server
First, the preparatory work

Items

version

update or download URL

Windows 10

64 guests

Slightly

IntelliJ idea

2017.1.5

http://www.jetbrains.com/idea/

JDK8

64 guests

Http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Redis

3.2.100

Https://github.com/MSOpenTech/redis/releases

Spring-data-redis

Jar Package

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>

Jedis

Jar Package

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

Sample Code Address:

Spring Boot session + Redis

Https://github.com/Cavan2477/SpringBoot-Session-Redis



Second, the construction steps

1. Installing Redis 1) Download Windows Redis

Download the address to get ready for work.


2) Unzip Redis to a custom directory



3) Setting up Redis requires access to password
a) Find the Requirepass field of the redis.windows.conf file in the directory where Redis is located, remove the comment, and modify the password you need
b) Setting up permanent login requires a password connection



4) Start Redis




2. Build Spring Boot Project 1) New Spring Boot Project


2) Maven dependencies, JDK, package settings


3) Project Dependency selection (can be viewed in the Pom.xml file)



4) The new project succeeds, automatically updates the jar Local warehouse default path according to the Pom.xml file: ${user.home}/.m2/repository, can also be set by itself



5) Run the Spring boot app to start

Does not start properly when the associated Redis support item is not added.

To add the relevant configuration, proceed as follows:
A) application.properties



b) Pom.xml


c) Codecaptionapplication.java



6) Restart the Spring boot app and access the session test path access path:

Http://localhost:8080/sessionRedis/test


Access results:



This step needs to add the controller source code see appendix.

The spring boot app for the integrated Session+redis is now available for normal access. Source download address See preparation.


Third, FAQ 1. Spring-boot-redis and Jedis version inconsistent 1) application does not start properly;

2) Jedis corresponding jar package version is greater than 2.4.2, please inquire at MAVEN website for details.


2. Redis Server Set temporary password mode 1) Open cmd command line
2) Enter the directory where Redis resides
3) Use the Startup.bat script to start the Redis service
4) Enter command redis-cli–h 127.0.0.1–p 6379 login to customer service
5) Set temporary password: Config set Requirepass < your password >
6) View password settings: Config get Requirepass
7) The results are as follows: (login required after password Setup)



Iv. Appendices 1. The MAVEN local repository settings file settings.xml download path has been added to the Alibaba Cloud Repository update address (located under the XML folder of the test engineering source download address).

2. MAVEN Warehouse Query address:
http://mvnrepository.com/

3. Redis Startup.bat Script
content
Redis-server.exe redis.windows.conf

4. Com.example.controller.TestController.java Source Code

Package Com.example.controller;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.http.HttpStatus;
Import org.springframework.http.ResponseEntity;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.RestController;

Import javax.servlet.http.HttpSession; @SuppressWarnings ("unchecked") @RestController public class TestController {private Logger Logger = Loggerfactory.get

    Logger (This.getclass ());
    @RequestMapping ("/sessionredis/test") public responseentity<?> httpsessionredistest (HttpSession HttpSession) {if (httpsession.isnew ()) {Logger.info ("successfully creates a session, the ID of Session:" + HTT
            Psession.getid ());
        Httpsession.setattribute ("Key", "Hello");
            } else {Logger.info ("session already exists in the server, the ID of Session:" + Httpsession.getid ()); Logger.info (Httpsession.getattribute ("key"). ToString ()); } responseentity<?> entity = new Responseentity<object> ("Hello World, Session ID:" + httpsession.get


        Id (), Httpstatus.ok);
    return entity; }
}

Effect Diagram:


If you have any questions please leave me a message or contact me directly, and you are welcome to criticize me.

Contact information

qq:247706624

Email:liuyeying1103@163.com/code_captain@163.com

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.