当前位置:网站首页>12.Gateway新一代网关
12.Gateway新一代网关
2022-07-01 10:02:00 【苦 糖 果】
1.概述简介
1.1 是什么?


Spring Cloud Gateway 使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架
gateway官网:
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/
zuul github:
https://github.com/Netflix/zuul/wiki
1.2 能干嘛?
反向代理
鉴权
流量控制
熔断
日志监控
微服务架构中网关在哪里?
1.3 有了Zuul了怎么又出来了gateway
1.3.1.neflix不太靠谱,zuul2.0一直跳票,迟迟不发布

1.3.2.SpringCloud Gateway具有如下特性

1.3.3 SpringCloud Gateway与Zuul的区别
Zuul1.x模型



1.3.4 三大核心概念


1.3.5Gateway工作流程
核心逻辑:路由转发+执行过滤器链


2.新建Module cloud-gateway-gateway9527
2.1改pom
<dependencies>
<!--新增gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.2改yml
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
routes:
- id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
#uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://cloud-payment-service
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由
- id: payment_routh2
#uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://cloud-payment-service
predicates:
- Path=/payment/lb/** #断言,路径相匹配的进行路由
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
Gateway网关路由有两种配置方式:
a:在配置文件yml中配置
b:代码中注入RouteLocator的Bean (不推荐)
2.3主启动类
@SpringBootApplication
@EnableEurekaClient
public class GateWayMain9527 {
public static void main(String[] args) {
SpringApplication.run( GateWayMain9527.class,args);
}
}
2.4测试
我们目前不想暴露8001端口,希望在8001外面套一层9527
启动7001,启动8001,启动9527网关
添加网关前:http://localhost:8001/payment/get/31
添加网关后:http://localhost:9527/payment/get/31
2.5 通过微服务名实现动态路由
默认情况下Gateway会根据注册中心的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能。不需要将ip地址写在配置中,只需写服务名即可。
需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。
lb://serviceName是spring cloud gateway在微服务中自动为我们创建的负载均衡uri
一个eureka7001+两个服务提供者8001/8002,http://localhost:9527/payment/lb 8001/8002两个端口切换

3.Predicate的使用
3.1是什么?



3.2 Predicate的使用
1-3中表达式的获取方法:
public class TimeUtil {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
}
}
2022-06-30T23:06:41.697+08:00[Asia/Shanghai]
- Path:匹配请求路径的最为常用,其他很少用
总结:Predicate就是为了实现一组匹配规则,让请求过来找到对应的Route进行处理。如果匹配不上,就不能访问。
4.Filter的使用

自带的过滤器一般不咋用,都是自定义全局GlobalFilter
实现这两个接口:GlobalFilter ,Ordered
spring的order中。越小的值,优先级越高,越大的值优先级越低,优先级高的先执行。
@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter,Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("*********come in MyLogGateWayFilter: "+new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("username");
if(StringUtils.isEmpty(uname )){
log.info("*****用户名为Null 非法用户,(┬_┬)");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);//给人家一个回应
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
http://localhost:9527/payment/lb?username=z3 正常访问。只有带上username才能正常访问。
边栏推荐
- 主流实时流处理计算框架Flink初体验
- 奇怪,为什么ArrayList初始化容量大小为10?
- I like two men...
- 122. Thread class thread method summary; Why is the thread start method start () not run ()?
- dotnet 控制台 使用 Microsoft.Maui.Graphics 配合 Skia 进行绘图入门
- 炒币,亏了1000万。
- Swift control encapsulation - paging controller
- CSDN's one-stop cloud service is open for internal testing, and new and old users are sincerely invited to grab the fresh
- 一个悄然崛起的国产软件,低调又强大!
- 新数据库时代,不要只学 Oracle、MySQL
猜你喜欢

嵌入式开发用到的一些工具

C# 一行代码计算文件的MD5值 - CodePlus系列

硬件中台项目

京东与腾讯续签三年战略合作协议;起薪涨至26万元!韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条...

The stock position building rate of global funds and asset management reached a new low in 15 years

CSDN一站式云服务开放内测,诚邀新老用户来抢鲜

CSDN's one-stop cloud service is open for internal testing, and new and old users are sincerely invited to grab the fresh

Hololens2 development -6-eyetracking and speech recognition

Spark's action operator

Japanese professor sues Intel FPGA and SOC products for infringing a design patent
随机推荐
BSN long story 10: how to ensure the safety of NFT
CSDN一站式云服务开放内测,诚邀新老用户来抢鲜
哪个券商公司炒股开户佣金低又安全又可靠
High precision factorial
Sleeping second brother...
LVGL V8.2字符串显示在Keil MDK上需要注意的事项(以小熊派为例)
睡了二哥。。。
Is it safe to do fund fixed investment on CICC securities?
[fxcg] large scale job hopping may be one of the driving forces behind the soaring inflation in the United States
PHP code audit and File Inclusion Vulnerability
mysql截取_mysql截取字符串的方法[通俗易懂]
超标量处理器设计 姚永斌 第4章 分支预测 --4.1 小节摘录
Can you afford to buy a house in Beijing, Shanghai, Guangzhou and Shenzhen with an annual salary of 1million?
遇到女司机业余开滴滴,日入500!
日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利
Apple amplification! It's done so well
Floyd repeat
Sqlization is the first step in ETL incremental production. What are the core capabilities of such an architecture?
Win11账号被锁定无法登录怎么办?Win11账号被锁定无法登录
If you meet a female driver and drive didi as an amateur, you can earn 500 a day!