SpringCloud微服务中基于Redis的Session共享实现指南
在微服务架构下,多个服务实例间保持用户会话状态一致是常见需求。利用内存数据库Redis来实现Session共享是一种高效方案,因其数据存储在内存中,具备极高的读写性能。
第一步,需在项目的pom.xml文件中添加必要的Maven依赖库。
org.springframework.boot
spring-boot-starter-redis
org.springframework.session
spring-session-data-redis
第二步,通过配置类将Spring默认的Session机制替换为基于Redis的Session实现。
package com.xueqing.demo.sleuthserverhi;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
/**
* 添加redis配置类启用redis代码spring默认session
*/
@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
}
第三步,在application.properties配置文件中指定Redis服务器的连接参数。
spring.redis.port= 6379
spring.redis.host=localhost
第四步,启动两个不同端口的Tomcat实例进行本地测试,验证Session跨服务共享效果。
package com.xueqing.demo.sleuthserverhi;
import java.util.logging.Level;
import java.util.logging.Logger;
import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
@SpringBootApplication
@RestController
public class SleuthServerHiApplication {
public static void main(String[] args) {
SpringApplication.run(SleuthServerHiApplication.class, args);
}
private static final Logger LOG = Logger.getLogger(SleuthServerHiApplication.class.getName());
@Autowired
private RestTemplate restTemplate;
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@RequestMapping("/hi")
public String callHome(HttpServletRequest request){
LOG.log(Level.INFO, "calling trace service-hi ");
request.getSession().setAttribute("hi","111");
LOG.log(Level.WARNING, "加入成功");
return restTemplate.getForObject("http://localhost:8989/miya", String.class);
}
@RequestMapping("/info")
public String info(HttpServletRequest request){
LOG.log(Level.INFO, request.getSession().getAttribute("miya")+"");
LOG.log(Level.WARNING, "获取成功");
return "i'm service-hi";
}
@Bean
public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE;
}
}
package com.xueqing.demo.sleuthservermiya;
import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import java.util.logging.Level;
import java.util.logging.Logger;
@SpringBootApplication
@RestController
public class SleuthServerMiyaApplication {
public static void main(String[] args) {
SpringApplication.run(SleuthServerMiyaApplication.class, args);
}
private static final Logger LOG = Logger.getLogger(SleuthServerMiyaApplication.class.getName());
@RequestMapping("/hi")
public String home(HttpServletRequest request){
LOG.log(Level.INFO, "hi is being called");
request.getSession().setAttribute("miya","111");
LOG.log(Level.WARNING, "加入成功");
return "hi i'm miya!";
}
@RequestMapping("/miya")
public String info(HttpServletRequest request){
LOG.log(Level.INFO, "info is being called");
LOG.log(Level.INFO, request.getSession().getAttribute("hi")+"");
LOG.log(Level.WARNING, "获取成功");
return restTemplate.getForObject("http://localhost:8988/info",String.class);
}
@Autowired
private RestTemplate restTemplate;
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@Bean
public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE;
}
}
第五步,注意事项:当前使用的SpringCloud版本为F系列,要求Redis版本不低于2.8。若版本过低,请升级至兼容版本,下载地址见下文。
https://github.com/MicrosoftArchive/redis/releases
总结
上述内容介绍了如何在SpringCloud环境中利用Redis实现各微服务间的Session共享。希望这对大家有所帮助,如有任何疑问欢迎留言,小编会及时回复。感谢对本站的支持!