First, preparation

item version Update or download the URL
Windows 10 A 64 – bit slightly
IntelliJ IDEA 2017.1.5 IntelliJ IDEA: The Capable & Ergonomic Java IDE by JetBrains
JDK8 A 64 – bit [Java Downloads
redis 3.2.100 Github.com/MSOpenTech/…

Spring – data – redis jars

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

Jedis jars

Clients </groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>Copy the code

Ii. Construction steps

1. Install redis

1.1 Download Windows Redis

See the preparation procedure for the download address.

1.2 Decompress Redis to the customized directory

1.3 Setting redis requires a password

1) Find the requirepass field in the redis.windows.conf file in the redis directory, remove the comment, and change the password you need

2) Set permanent login requires password connection

1.4 start the redis

2. Set up the Spring Boot project

2.1 Creating a Spring Boot Project

2.2 Maven dependency, JDK, Package Settings

 

2.3 Project dependency selection (all can be viewed in pom.xml file)

 

2.4 If the project is successfully created, the JAR is automatically updated according to the POM. XML file

The default repository path is ${user.home}/.m2/repository

2.5 Checking whether the Spring Boot Application can be Started

 You cannot start normally without redis support.

To add the configuration, do as follows:

  1. application.properties

 

  1. pom.xml

 

  1. CodeCaptionApplication.java

 

2.6 Restart the Spring Boot application and access the Session test path

Access path:

http://localhost:8080/sessionRedis/test

Results:

See appendix for the source code of controller to be added in this step.

The Spring Boot application integrating Session + Redis can be accessed normally. Source code download address see preparations.

Third, the FAQ

1. The spring-boot-Redis version is inconsistent with that of jedis

  1. The application cannot start normally.

  2. The version of the JAR package corresponding to Jedis is later than 2.4.2. For details, see the Maven official website.

2. Set the temporary password mode on the Redis server

  1. Open the CMD command line
  2. Access the directory where Redis resides
  3. Use the startup.bat script to start the Redis service
  4. Enter redis-cli -h 127.0.0.1 -p 6379 to log in to the customer service
  5. Set temporary password: config set requirepass < your password >
  6. To check the password Settings, run the config get requirepass command
  7. The result is as follows :(you need to log in again after setting the password)

 

Fourth, the appendix

1. Set the path for downloading the Maven local repository file settings. XML

Ali cloud warehouse update address has been added (under the XML folder of the source code download address of the test project).

2. The Maven repository

Address: mvnrepository.com/

3. Redis startup.bat script

content

redis-server.exe redis.windows.conf
Copy the code

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.getLogger(this.getClass()); @RequestMapping("/sessionRedis/test") public ResponseEntity<? > httpSessionRedisTest(HttpSession httpSession) { if (httpSession.isNew()) { logger.info("Successfully creates a session , the id of session: "+ httpSession. 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.getId(), HttpStatus.OK); return entity; }}Copy the code

Effect:

Five, sample code

Spring boot session + redis

Github-cavan2477 / Springboot-session-redis: Spring boot + Redis implements Session sharing management