当前位置:网站首页>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才能正常访问。
边栏推荐
- Postgraduate entrance examination vocabulary 2023 sharing (1)
- CentOS configures discuz prompt, please check whether the MySQL module is loaded correctly
- It is interesting to understand MMAP in this way!
- 谁拥有穿越周期的眼光?
- TC8:UDP_USER_INTERFACE_01-08
- C [byte array] and [hexadecimal string] mutual conversion - codeplus series
- 关于#数据库#的问题:GBase 8s中如何避免死锁
- 微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”
- Unity tips for reducing the amount of code -- empty protection extension
- 7-Zip 遭抵制?呼吁者定下“三宗罪”:伪开源、不安全、作者来自俄罗斯!
猜你喜欢

C one line code calculates the MD5 value of the file - codeplus series

Live broadcast management project

华为帐号多端协同,打造美好互联生活

Introduction to expressions and operators in C language

Sleeping second brother...

渗透常用工具-Goby

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

云原生到底是什么?它会是未来发展的趋势吗?

Flinkv1.13实现金融反诈骗案例

Huawei accounts work together at multiple ends to create a better internet life
随机推荐
Upload labs for file upload - white box audit
亿学学堂帮个人开的证券账户安全吗?是不是有套路
项目必用的全局异常处理器,你学会了吗
Comparison between Oracle JDK and openjdk
4hutool实战:DateUtil-格式化时间[通俗易懂]
【无标题】
直播管理项目
苹果放大招!这件事干的太漂亮了……
leetcode:111. 二叉树的最小深度
IPv6 learning notes
预制菜迎来“黄金时代”,谁能领跑下一个万亿市场
Fried money, lost 10million.
The "China Mobile Chain" state secret engine was officially launched on BSN
我喜欢两个男人。。。
Drive away bad emotions and stop worrying
122. Thread class thread method summary; Why is the thread start method start () not run ()?
全球基金和资管的股票建仓率达到15年内新低
The market is relatively weak recently
JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan! Samsung sk of South Korea competes for salary increase to retain semiconductor talents;
What if the win11 account is locked and unable to log in? Win11 account is locked and unable to log in