zoukankan      html  css  js  c++  java
  • Spring Cloud项目通过Redis实现Session共享

    一、添加Redis相关依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-data-redis</artifactId>
    </dependency>

    二、项目添加Redis配置信息

    1、需要使用Redis的模块添加Redis配置信息

    #配置redis连接信息
    spring.session.store-type=redis
    spring.redis.host=192.168.211.128
    spring.redis.port=6379
    spring.redis.password=

     2、给网关添加Redis配置信息

    #配置redis连接信息
    spring.redis.host=192.168.211.128
    spring.redis.port=6379
    spring.redis.password=
    #配置网关相关参数
    zuul.sensitive-headers=
    zuul.host.connect-timeout-millis=15000

     3、给相关模块和网关启动类添加@EnableRedisHttpSession注解,使用Redis存储session

    package com.learn.cloud.mall.practice.user;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @SpringBootApplication
    @EnableSwagger2
    @MapperScan(basePackages = "com.learn.cloud.mall.practice.user.model.dao")
    @EnableRedisHttpSession
    public class UserApplication {
        public static void main(String[] args) {
            SpringApplication.run(UserApplication.class);
        }
    }
    package com.learn.cloud.mall.practice.zuul;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.cloud.client.SpringCloudApplication;
    import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
    
    @SpringCloudApplication
    @EnableZuulProxy
    @EnableFeignClients
    @EnableRedisHttpSession
    public class ZuulGatewayApplication {
        public static void main(String[] args) {
            SpringApplication.run(ZuulGatewayApplication.class,args);
        }
    }

    4、使用

    //通过以上配置,直接使用setAttribute方法,即可将session存入Redis中
    session.setAttribute(Constant.USER, user);
  • 相关阅读:
    extjs数据类型
    Extjs 动态控制列显示
    400
    extjs主单清单同时编辑提交
    js-map模拟
    Leetcode 407.接雨水
    Leetcode 406.根据身高重建队列
    Leetcode 405.数字转化为十六进制数
    Leetcode 402.移掉k位数字
    Leetcode 401.二进制手表
  • 原文地址:https://www.cnblogs.com/michealyang/p/14205336.html
Copyright © 2011-2022 走看看