1. Nuevo proyecto
La estructura del proyecto es la siguiente (simplemente cree un proyecto vacío):
2. Agregar dependencias
<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. Agregar configuración
Agregue la siguiente configuración en el yml de cada submódulo:
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. Agregue la anotación @EnableRedisHttpSession a la clase de inicio.
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. Código de prueba
1. Cree un nuevo controlador/WebController en servicio1 y servicio2 respectivamente.
servicio1:
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";
}
}
servicio2:
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. Prueba
1. Visite localhost:8020/service1/setMsg
2. Visite localhost:8081/service2/getMsg
3. Ver en Redis Manager