1. New project
The project structure is as follows (just create an empty project):
2. Add dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!--Lettuce是 一 个 基 于 Netty的 NIO方 式 处 理 Redis的 技 术 -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
3. Add configuration
Add the following configuration in the yml of each submodule:
spring.session.store-type=redis
server.port=8020
spring.redis.host=xxx.xx.xxx.x
spring.redis.port=6379
spring.redis.password=xxx
spring.redis.database=11
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=30
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.timeout=10
4. Add the annotation @EnableRedisHttpSession to the startup class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@EnableRedisHttpSession
@SpringBootApplication
public class SessionService1Application {
public static void main(String[] args) {
SpringApplication.run(SessionService1Application.class, args);
}
}
5. Test code
1. Create new controller/WebController in service1 and service2 respectively.
service1:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RequestMapping("/service1")
@RestController
public class WebController {
@GetMapping("/setMsg")
public String setMsg(HttpSession session){
session.setAttribute("msg","hello,SpringSession!");
return "OK";
}
}
service2:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RequestMapping("/service2")
@RestController
public class WebController {
@GetMapping("/getMsg")
public String getMsg(HttpSession session){
String msg = (String)session.getAttribute("msg");
return msg;
}
}
6. Test
1. Visit localhost:8020/service1/setMsg
2. Visit localhost:8081/service2/getMsg
3. View in Redis Manager