SpringCloud实现Redis在各个微服务的Session共享问题
作者:别等时光染了梦想 发布时间:2022-07-14 06:33:19
标签:spring,cloud,redis
在微服务中,需要我们在各个微服务中共享Session,使用Redis来共享Session是一个很好的解决方法,Redis是运行在内存中,查取速度很快。
1.pom文件中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
2.使用Redis的session替换Spring的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 {
}
3.application.properties配置文件中添加redis配置
spring.redis.port= 6379
spring.redis.host=localhost
4.启动两个端口以一样的tomcat测试
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;
}
}
5.注意事项:我用的springCloud版本为F版本需要Redis版本为2.8以上 如果不是2.8以上请升级,地址如下
https://github.com/MicrosoftArchive/redis/releases
总结
以上所述是小编给大家介绍的SpringCloud实现Redis在各个微服务的Session共享网站的支持!
来源:https://blog.csdn.net/wangxueqing52/article/details/81871389
0
投稿
猜你喜欢
- 前言各位小伙伴大家好,我是A哥。IDEA上个较大版本的发布,要追溯到4月份了:时隔近4个月,北京时间2020-07-28深夜,Intelli
- 本文实例为大家分享了java实现猜拳游戏的具体代码,供大家参考,具体内容如下package com.farsight.session7;im
- 一、什么是 websocket 接口使用 websocket 建立长连接,服务端和客户端可以互相通信,服务端只要有数据更新,就可以主动推给客
- 一、简单介绍Unity 游戏实例开发集合,使用简单易懂的方式,讲解常见游戏的开发实现过程,方便后期类似游戏开发的借鉴和复用。本节介绍,Fly
- 目录一、二叉树的顺序存储1.堆的存储方式2.下标关系二、堆(heap)1.概念2.大/小 根堆2.1小根堆2.2大根堆3.建堆操作3.1向下
- 在生产环境中,需要实时或定期监控服务的可用性。spring-boot 的actuator(监控)功能提供了很多监控所需的接口。简单的配置和使
- 如何检查一个数组(无序)是否包含一个特定的值?这是一个在Java中经常用到的并且非常有用的操作。同时,这个问题在Stack Overflow
- 导出的 Jar 包无法运行?导出的 Jar 包找不到 Main class?大概是我对导出 Jar 包的理解不深吧,反正一直不太懂 IDEA
- 背景项目中我们经常会用搜索功能,普通的搜索我们可以用一个SQL的like也能实现匹配,但是搜索的核心需求是全文匹配,对于全文匹配,数据库的索
- 注:图片来源于网络SpringBoot作为业内公认的优秀开源框架,它的 * 是如何实现呢?在这里首先对一些基础组件进行分析;1、事件Appl
- 效果展示在实际项目当中我们经常看到如下各种剪裁形状的效果,Flutter 为我们提供了非常方便的 Widget 很轻松就可以实现,下面我们来
- Android版本更新实例详解1、导入xutils的jar包 2、在AndroidManifest.xml中添加权限 3、选择下载的路径,和
- 1. 引言 * (Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似于Servlet的Filter。我们可以让普通的B
- 初学线程时,总是将 run 方法和 start 方法搞混,虽然二者是完全不同的两个方法,但刚开始使用时很难分清,原因就是因为初次使用时效果貌
- Quartz与Spring集成方式:1.MethodInvokeJobDetailFactoryBean2.JobDetailBean下面分
- 本文实例讲述了C#使用Ado.net读取Excel表的方法。分享给大家供大家参考。具体分析如下:微软NET提供了一个交互的方法,通过使用AD
- 前言在前面的文章中其实大家也已经看到我使用过collect(Collectors.toList()) 将数据最后汇总成一个 List 集合。
- mybatis 报错显示sql中有两个limit使用mybatis进行分页查询时,打印的查询sql中带有两个limit。经过审查:原因是由于
- 一、RESTful风格API的好处API(Application Programming Interface),顾名思义:是一组编程接口规范
- 默认情况下Spring Boot使用了内嵌的Tomcat服务器,项目最终被打成jar包运行,每个jar包可以被看作一个独立的Web服务器。传