欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud實(shí)現(xiàn)Redis在各個(gè)微服務(wù)的Session共享問題

 更新時(shí)間:2018年08月20日 15:16:34   作者:別等時(shí)光染了夢想  
Redis是運(yùn)行在內(nèi)存中,查取速度很快。本文重點(diǎn)給大家介紹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)站的支持!

相關(guān)文章

  • Java 線程池全面總結(jié)與詳解

    Java 線程池全面總結(jié)與詳解

    在一個(gè)應(yīng)用程序中,我們需要多次使用線程,也就意味著,我們需要多次創(chuàng)建并銷毀線程。而創(chuàng)建并銷毀線程的過程勢必會(huì)消耗內(nèi)存。而在Java中,內(nèi)存資源是及其寶貴的,所以,我們就提出了線程池的概念
    2021-10-10
  • JavaSE的三大接口:Comparator,Comparable和Cloneable詳解

    JavaSE的三大接口:Comparator,Comparable和Cloneable詳解

    這篇文章主要介紹了詳解JavaSE中Comparator,Comparable和Cloneable接口的區(qū)別的相關(guān)資料,希望通過本文大家能徹底掌握這部分內(nèi)容,需要的朋友可以參考下
    2021-10-10
  • springboot整合mongodb使用詳解

    springboot整合mongodb使用詳解

    MongoDB是一個(gè)文檔數(shù)據(jù)庫(以?JSON?為數(shù)據(jù)模型),由C++語言編寫,旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案,本文就給大家介紹一下詳細(xì)介紹一下springboot整合mongodb使用,需要的朋友可以參考下
    2023-07-07
  • idea中的Maven導(dǎo)包失敗問題解決方案匯總

    idea中的Maven導(dǎo)包失敗問題解決方案匯總

    這篇文章主要介紹了idea中的Maven導(dǎo)包失敗問題解決總結(jié),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼

    mybatis中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ī)則的問題

    這篇文章主要介紹了解決Mybatis-plus找不到對(duì)應(yīng)表及默認(rèn)表名命名規(guī)則的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • spring schedule實(shí)現(xiàn)動(dòng)態(tài)配置執(zhí)行時(shí)間

    spring 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-11
  • Java 使用反射調(diào)用jar包中的類方式

    Java 使用反射調(diào)用jar包中的類方式

    這篇文章主要介紹了Java 使用反射調(diào)用jar包中的類方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 簡單介紹線性表以及如何實(shí)現(xiàn)雙鏈表

    簡單介紹線性表以及如何實(shí)現(xiàn)雙鏈表

    本文先介紹線性表的幾個(gè)基本組成部分:數(shù)組、單向鏈表、雙向鏈表;隨后給出雙向鏈表的C、C++和Java三種語言的實(shí)現(xiàn),需要的朋友可以參考下
    2015-07-07
  • Java 異常java.lang.NoSuchFieldException解決方案

    Java 異常java.lang.NoSuchFieldException解決方案

    這篇文章主要介紹了Java 異常java.lang.NoSuchFieldException解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10

最新評(píng)論