Spring Cloud Gateway 使用
简介
Spring Cloud Gateway是Spring Cloud官方推出的网关框架,网关作为流量入口,在微服务系统中有着十分重要的作用,常用功能包括:鉴权、路由转发、熔断、限流等。
Spring Cloud Gateway是通过Spring WebFlux的HandlerMapping做为底层支持来匹配到转发路由,使用时不要引入SpringMVC,否则初始化时会出错;Spring Cloud Gateway内置了很多Predicates工厂,这些 Predicates 工厂通过不同的 HTTP 请求参数来匹配,多个 Predicates 工厂可以组合使用。
使用
1、添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2、配置(结合Eureka使用)
server:
port:
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true #设置serviceId小写,默认大写
routes:
- id: user-server
uri: lb://user-server #lb表示从注册中心获取服务
predicates:
- Path=/userapi/** # 如果请求地址满足/userapi/**,则转发到user-server服务
filters:
- StripPrefix=1 # 去除原请求地址中的userapi
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8088/eureka/
3、集成Hystrix
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
配置
filters:
- name: Hystrix
args:
name : default
fallbackUri: 'forward:/dfallback'
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds:
java端
@RestController
public class DHystrixController {
@RequestMapping("/dfallback")
public Map<String,String> dfallback(){
System.out.println("降级了。。。");
Map<String,String> map = new HashMap<String,String>();
map.put("rCode","-1");
map.put("rMsg","出错了");
return map;
}
}
Spring Cloud Gateway 使用的更多相关文章
- 从0开始构建你的api网关--Spring Cloud Gateway网关实战及原理解析
API 网关 API 网关出现的原因是微服务架构的出现,不同的微服务一般会有不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各个微服务通信,会有以下的问题 ...
- Spring cloud gateway
==================================为什么需要API gateway?==================================企业后台微服务互联互通, 因为 ...
- Spring Cloud Gateway Ribbon 自定义负载均衡
在微服务开发中,使用Spring Cloud Gateway做为服务的网关,网关后面启动N个业务服务.但是有这样一个需求,同一个用户的操作,有时候需要保证顺序性,如果使用默认负载均衡策略,同一个用户的 ...
- 简单尝试Spring Cloud Gateway
简单尝试Spring Cloud Gateway 简介 Spring Cloud Gateway是一个API网关,它是用于代替Zuul而出现的.Spring Cloud Gateway构建于Sprin ...
- spring cloud gateway的stripPrefix配置
序 本文主要研究下spring cloud gateway的stripPrefix配置 使用zuul的配置 zuul: routes: demo: sensitiveHeaders: Access-C ...
- Spring Cloud Gateway中异常处理
最近我们的项目在考虑使用Gateway,考虑使用Spring Cloud Gateway,发现网关的异常处理和spring boot 单体应用异常处理还是有很大区别的.让我们来回顾一下异常. 关于异常 ...
- api网关揭秘--spring cloud gateway源码解析
要想了解spring cloud gateway的源码,要熟悉spring webflux,我的上篇文章介绍了spring webflux. 1.gateway 和zuul对比 I am the au ...
- SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断
1.什么是路由网关 截至目前为止的例子中,我们创建了一个service,叫做:HelloService,然后我们把它部署到了两台服务器(即提供了两个provider),然后我们又使用ribbon将其做 ...
- spring cloud gateway - RequestRateLimiter
1. Official website 5.7 RequestRateLimiter GatewayFilter Factory The RequestRateLimiter GatewayFilte ...
- Spring Cloud Gateway入门
1.什么是Spring Cloud GatewaySpring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技 ...
随机推荐
- JavaScript String 字符串方法
JavaScript String 字符串方法汇总 1.str.indexOf() 方法查找字符串中的字符串 返回 字符串中指定文本首次出现的索引(位置) JavaScript ...
- 01-python3基础-基本数据类型
Python3 基本数据类型 标准数据类型 Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Set(集合) Diction ...
- 【转载】Windows api数据类型
最近在接触windows api函数,看到了很多之前没有看到过的数据类型,发现“个人图书馆”中有个帖子说的挺详细的,特地搬运过来 Windows 数据类型 Delphi 数据类型 描述 LPSTR P ...
- 2019nc#6
https://ac.nowcoder.com/acm/contest/886#question 题号 标题 已通过代码 题解/讨论 通过率 团队的状态 A Garbage Classificatio ...
- P3469 [POI2008]BLO-Blockade 割点 tarjan
题意 给定一个无向图,问删掉点i,图中相连的有序对数.(pair<x, y> , x != y);求每个点对应的答案 思路 首先我们可以发现,如果这个点不是割点,那么答案就是n-1,如果是 ...
- LuoGuP1516 青蛙的约会 + 同余方程 拓展欧几里得
题意:有两只青蛙,在一个圆上顺时针跳,问最少的相遇时间. 这个是同余方程的思路.可列出方程:(m-n)* X% L = y-x(mod L) 简化为 a * x = b (mod L) (1 ...
- 杭电多校第九场 HDU6415 Rikka with Nash Equilibrium dp
Rikka with Nash Equilibrium Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K ...
- codeforces 807 D. Dynamic Problem Scoring(贪心+思维)
题目链接:http://codeforces.com/contest/807/problem/D 题意:对于动态计分的 Codeforces Round ,已知每题的 score 是根据 Round ...
- codeforces 459 E. Pashmak and Graph(dp)
题目链接:http://codeforces.com/contest/459/problem/E 题意:给出m条边n个点每条边都有权值问如果两边能够相连的条件是边权值是严格递增的话,最长能接几条边. ...
- 企查猫app数据解密
通过最近几天的对企查猫的研究,目前已经成功将企查猫的数据加密和响应数据加密完成解密. 和之前对启信宝APP的数据解密操作基本一样,不过企查猫对请求和响应都使用aes加密了,抓包的时候可以看到,具体可以 ...