BES-SpringCloud Gateway网关整合多模块项目-Predicates与Filter
开发过程中使用了Spring Cloud Gateway来进行对各个服务的请求转发。
[code]spring: application: name: boss-bes-gateway redis: host: localhost password: test123 port: 6379 main: allow-bean-definition-overriding: true cloud: gateway: discovery: locator: enabled: true routes: # 用户与权限管理 - id: boss-bes-user-permission-center uri: http://localhost:10015/ predicates: - Path=/boss-bes-user-permission-center/api/** filters: - StripPrefix=1 - name: Retry args: retries: 3 statuses: BAD_GATEWAY - name: Hystrix args: name: fallbackcmd fallbackUri: forward:/fallback - name: RequestRateLimiter args: key-resolver: '#{@hostAddrKeyResolver}' redis-rate-limiter.replenishRate: 10 redis-rate-limiter.burstCapacity: 30 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 10000
以该yml中用户权限管理为例:
在没有用网关进行转发之前,请求的地址形式:
[code]localhost:10015/api/**
设置网关以后的请求的地址形式
[code]localhost:10003/boss-bes-user-permission-center/api/**
指定了uri(localhost:10015,要指向的地址)
对predicates指定了path(网关表现的地址)
filter配置StripPrefix=1的意思:把path的前1个路径截掉(/boss-bes-user-permission-center):
处理过后
前段发送以下请求:
localhost:10003/boss-bes-user-permission-center/api/**
网关:
1. 接收到/boss-bes-user-permission-center/api/**
2. 根据StripPrefix=1,截掉/boss-bes-user-permission-center,剩下api/**
3. 拼上uri=localhost:10015,得到localhost:10015/api/**,发送到后端。
Filter
网关中用到的filter有三种:熔断、限流、重试。
重试:参数为重试次数。
限流:根据IP限流。写一个限流类,并通过@Bean的方式注入
[code]public class HostAddrKeyResolver implements KeyResolver { @Override public Mono<String> resolve(ServerWebExchange exchange) { return Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress()); } } //在Application类注入 @Bean public HostAddrKeyResolver hostAddrKeyResolver() { return new HostAddrKeyResolver(); }
yml文件中设置redis-rate-limiter.replenishRate(每秒最多几个请求)以及redis-rate-limiter.burstCapacity(每秒最多处理请求数),由于限流通过redis实现,因此redis需要进行配置。
熔断:其他服务通过feign调用此服务时,如果调用失败,则进入yml中设置的fallbackUri中。
(注意yml文件末尾设置了hystrix的超时时间,若调用进入等待超过设定时间就会触发熔断)
简单的熔断控制器:
[code]@RestController public class FallbackController { @PostMapping("/fallback") public CommonResponse fallback() { ResponseHead head = new ResponseHead(); head.setCode("HYSTRIX"); head.setMessage("服务熔断"); CommonResponse response = new CommonResponse(); response.setResponseHead(head); return response; } @GetMapping("/fallback") public CommonResponse fallback_get() { ResponseHead head = new ResponseHead(); head.setCode("HYSTRIX"); head.setMessage("服务熔断"); CommonResponse response = new CommonResponse(); response.setResponseHead(head); return response; } }
- 点赞
- 收藏
- 分享
- 文章举报
- java实现一个删除固定后缀文件的程序
- java编写贪吃蛇小游戏源代码分享给你们
- @蓝桥杯javaB组习题集入门(4)之第二题:序列求和
- @蓝桥杯javaB组习题集入门(4)第四题:Fibonacci数列
- @java蓝桥杯B组习题基础篇(30)第二题:01字串
- @java蓝桥杯B组习题基础篇(30)第四题:数列特征
- @java蓝桥杯B组习题基础篇(30)第八题:回文数
- @java蓝桥杯B组习题基础篇(30)第10题:十进制转十六进制
- @java蓝桥杯B组习题基础篇(30)第11题:十六进制转十进制
- @java蓝桥杯B组习题基础篇(30)第13题:数列排序
- 从今天开始免费给大家分享一下我写的java蓝桥杯B组习题吧,写的不对的地方希望大家指正,我也会很乐意听取并改正的,谢谢大家!!@java蓝桥杯习题入门(4)第一题:A+B问题
- VSCode中SpringBoot返回静态html网页
- Think in Java 笔记(chapter1-7)
- SpringMVC接口开发
- springboot拆分模块分享
- 怎样编写一个springmvc的helloworld
- 1.1 介绍Spring Ioc Container和Beans
- 《Spring源码深度解析》读书笔记
- eclipse界面语言切换的三种方法
- java 获取某周的起始和结束日期