没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
SpringCloud实现实现Redis在各个微服务的在各个微服务的Session共享问题共享问题
Redis是运行在内存中,查取速度很快。本文重点给大家介绍SpringCloud实现Redis在各个微服务的Session共
享,感兴趣的朋友一起看看吧
在微服务中,需要我们在各个微服务中共享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, "获取成功");
资源评论
weixin_38687218
- 粉丝: 3
- 资源: 941
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功