SpringCloud實(shí)現(xiàn)Redis在各個(gè)微服務(wù)的Session共享問題
在微服務(wù)中,需要我們在各個(gè)微服務(wù)中共享Session,使用Redis來共享Session是一個(gè)很好的解決方法,Redis是運(yùn)行在內(nèi)存中,查取速度很快。
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默認(rèn)session */ @Configuration @EnableRedisHttpSession public class RedisSessionConfig { }
3.application.properties配置文件中添加redis配置
spring.redis.port= 6379 spring.redis.host=localhost
4.啟動(dòng)兩個(gè)端口以一樣的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.注意事項(xiàng):我用的springCloud版本為F版本需要Redis版本為2.8以上 如果不是2.8以上請升級(jí),地址如下
https://github.com/MicrosoftArchive/redis/releases
總結(jié)
以上所述是小編給大家介紹的SpringCloud實(shí)現(xiàn)Redis在各個(gè)微服務(wù)的Session共享,希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例
- spring-redis-session 自定義 key 和過期時(shí)間
- 基于SpringBoot+Redis的Session共享與單點(diǎn)登錄詳解
- 解決Spring session(redis存儲(chǔ)方式)監(jiān)聽導(dǎo)致創(chuàng)建大量redisMessageListenerContailner-X線程問題
- Spring整合redis(jedis)實(shí)現(xiàn)Session共享的過程
- spring boot整合redis實(shí)現(xiàn)shiro的分布式session共享的方法
- 詳解springboot中redis的使用和分布式session共享問題
- 如何將Spring Session存儲(chǔ)到Redis中實(shí)現(xiàn)持久化
相關(guān)文章
JavaSE的三大接口:Comparator,Comparable和Cloneable詳解
這篇文章主要介紹了詳解JavaSE中Comparator,Comparable和Cloneable接口的區(qū)別的相關(guān)資料,希望通過本文大家能徹底掌握這部分內(nèi)容,需要的朋友可以參考下2021-10-10mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼
這篇文章主要介紹了mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03解決Mybatis-plus找不到對(duì)應(yīng)表及默認(rèn)表名命名規(guī)則的問題
這篇文章主要介紹了解決Mybatis-plus找不到對(duì)應(yīng)表及默認(rèn)表名命名規(guī)則的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11spring schedule實(shí)現(xiàn)動(dòng)態(tài)配置執(zhí)行時(shí)間
這篇文章主要介紹了spring schedule實(shí)現(xiàn)動(dòng)態(tài)配置執(zhí)行時(shí)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java 異常java.lang.NoSuchFieldException解決方案
這篇文章主要介紹了Java 異常java.lang.NoSuchFieldException解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10