欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringCloud实现Redis在各个微服务的Session共享问题

程序员文章站 2024-02-25 16:52:45
在微服务中,需要我们在各个微服务*享session,使用redis来共享session是一个很好的解决方法,redis是运行在内存中,查取速度很快。 1.pom文件中添...

在微服务中,需要我们在各个微服务*享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共享,希望对大家有所帮助