从零搭建Spring Cloud Gateway网关(一) (2)

默认的熔断回调接口:

package com.lifengdi.gateway.hystrix; import com.lifengdi.gateway.exception.BaseException; import com.lifengdi.gateway.response.ResponseResult; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author: Li Fengdi * @date: 2020-03-18 16:35 */ @RestController @Slf4j public class DefaultHystrixController { @RequestMapping("/fallback") public ResponseResult<Object> fallback(){ log.error("触发熔断......"); return ResponseResult.fail(BaseException.DEFAULT_HYSTRIX.build()); } }

具体配置文件说明如下:

routes: - id: path_route # 指定域名 uri: :8081 predicates: - Path=http://www.likecs.com/jar/** filters: # 熔断配置 - name: Hystrix args: name: default fallbackUri: forward:/fallback - id: path_route2 # 指定域名 uri: :8082 predicates: - Path=http://www.likecs.com/war/** filters: # 熔断配置 - name: Hystrix args: name: hystrix1 fallbackUri: forward:/fallback mvc: throw-exception-if-no-handler-found: true # 默认熔断超时时间30s hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 3000 hystrix1: execution: isolation: thread: timeoutInMilliseconds: 1000

default、hystrix1为自定义的参数,可以配置多个熔断策略,不同的接口、服务可以单独配置对应的超时时间,不需要额外的进行开发,不过需要增加额外的配置文件。

全局session共享

依赖jar包:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis-reactive</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>

相关yml配置:

spring: redis: database: 0 host: localhost port: 6379 password: 123456 lettuce: pool: max-active: 300 max-idle: 8 max-wait: -1ms min-idle: 0 session: store-type: redis

spring.session.store-typeSpring默认就是redis实现的,也有其他的,配置不同罢了。

增加代码如下:

权限相关,这里默认全部放行:

package com.lifengdi.gateway.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; import org.springframework.security.config.web.server.ServerHttpSecurity; import org.springframework.security.web.server.SecurityWebFilterChain; @Configuration @EnableWebFluxSecurity public class GatewaySecurityConfig { @Bean SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity serverHttpSecurity) throws Exception { serverHttpSecurity .csrf().disable() .authorizeExchange().pathMatchers("/**").permitAll() .anyExchange() .authenticated(); return serverHttpSecurity.build(); } }

session相关:

package com.lifengdi.gateway.config; import com.lifengdi.gateway.resolver.MyCookieWebSessionIdResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.ResponseCookie; import org.springframework.session.data.redis.config.annotation.web.server.EnableRedisWebSession; import org.springframework.web.server.session.CookieWebSessionIdResolver; import org.springframework.web.server.session.WebSessionIdResolver; import java.util.function.Consumer; @Configuration @EnableRedisWebSession(maxInactiveIntervalInSeconds = 10*60*60, redisNamespace = "my:spring:session") public class WebSessionConfig { @Bean public WebSessionIdResolver webSessionIdResolver() { CookieWebSessionIdResolver resolver = new MyCookieWebSessionIdResolver(); resolver.setCookieName("SESSIONID"); Consumer<ResponseCookie.ResponseCookieBuilder> consumer = responseCookieBuilder -> { responseCookieBuilder.path("http://www.likecs.com/"); }; resolver.addCookieInitializer(consumer); return resolver; } }

注意这里使用的是@EnableRedisWebSession注解,而不是@EnableRedisHttpSession,这个是和zuul不一样的地方。

用zuul做网关的时候,直接使用@EnableRedisHttpSession在配置里面就可以通过redis共享session信息

Spring同时提供了@EnableRedisWebSession来对WebFlux的支持。

值得一提的是这两个注解内部实现并不相同,需要自定义的配置也不一样。

这里自定义cookieName、path等是自定义了webSessionIdResolver来实现的,而不是cookieSerializer。如果使用cookieSerializer的话,对@EnableRedisWebSession来说是不起作用的。这个坑之前坑了好半天!

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppggz.html