spring cloud redis session共享

mvn依赖

        <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>

zuul模块的设置

@SpringBootApplication
@EnableZuulProxy//允许zuul代理,配置session存储后立刻刷新设置刷新模式为立刻刷新,否则可能获取不到session。
@EnableEurekaClient
@EnableRedisHttpSession(redisFlushMode = RedisFlushMode.IMMEDIATE)
public class WebApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class,args);
    }
}
zuul:
  routes:
    service0:
    #设置敏感头部信息。 Cookie/Set-Cookie
      sensitiveHeaders: Authorization
      path: /api/**
      url: http://localhost:8080/
    service1:
      path: /api2/**
      url: http://localhost:8090/
      sensitiveHeaders: Authorization
  #增加代理的header,防止session丢失

1、@EnableZuulProxy注解,用于引入zuul代理,实现zuul的路由转发功能。

2、@EnableRedisHttpSession,用于指定redis管理session。这里我们指定redisFlushMode为立即刷新模式,防止因为redis刷新缓慢,导致session信息无法获取。

注意要配置sensitiveHeader,默认情况下sensitiveHeaders,包含Cookie/Set-Cookie/Authorization三个头,zuul认为这些头是敏感的,默认不会再路由中携带,所以为 session共享,就一定要放行cookie,只设置Authorization就行,或者指定其它值。

其它模块

在spring boot的主类上开启redis管理session的注解 。@EnableRedisHttpSession


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
 
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
@EnableRedisHttpSession
public class ZuulgatewayApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(ZuulgatewayApplication.class, args);
	}
 
}
 

配置redis

#集群-用于部署
  redis:
    cluster:
      nodes: 10.200.15.248:6379
      timeout: 2000;
      max-redirects: 7
#单机-用于开发
  redis:
    host: 10.200.15.248
    port: 6379
    database: 0
    jedis:
      pool:
        max-active: 8
        max-wait:
        max-idle: 8
        min-idle: 0
    timeout:
    password:

 

展开阅读全文

没有更多推荐了,返回首页