SpringCloud Gateway
SpringCloud Gateway作为SpringCloud生态系中的网关,目标是代替Zuul,在SpringCloud2.0以上的版本中,没有对新版本Zuul2.0以上最新高性能版本进行继承,仍然使用的Zuul1.x非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WEbFlux框架底层则使用了高性能的Reactor模式通信框架Netty。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EYj5cA1I-1658484002382)(C:\Users\圆圆\AppData\Roaming\Typora\typora-user-images\image-20210202102126305.png)]
为什么选择Gateway?
因为Zuul公司跳票原因,SpringCloud公司自己研制了网关。
Gateway是基于异步非阻塞模型上进行开发的,性能方面不需要担心。
Gateway的基础使用
配置Gateway网关有两种方式一种配置文件方式,一种硬编码模式(@bean),因为配置文件更为灵活所以使用配置文件模式
1.导入依赖
<!--gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2.编写配置文件
server:
port: 9420
spring:
application:
name: cloud-geteway
cloud:
gateway:
#网关路由
routes:
- id: payment_routh #映射
uri: http://localhost:8001 #映射地址
#举例:如我在浏览器输入:localhost:9420/payment_routh/payment/get/1
#他就会映射出: localhost:8001/payment/get/1
predicates:
- Path=/payment/get/** #路径规则必须含有payment/get 包括它的子类
- id: payment_routh2
uri: http://localhost:8001
predicates:
- Path=/payment/lb/**
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
register-with-eureka: ture
fetch-registry: true
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
3.编写主启动类
package com.yuan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableDiscoveryClient
public class Gateway_9420 {
public static void main(String[] args) {
SpringApplication.run(Gateway_9420.class,args);
}
}
由上面我们发现,路由都是写死的,更不能像消费端一样实现负载均衡所以我们要对上面进行优化升级
Gateway的动态路由
spring:
application:
name: cloud-geteway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心创建动态路由功能,利用微服务名称进行路由
routes:
- id: payment_routh
#uri: http://localhost:8001
#lb://是固定的负载均衡 后面的是名称
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/**
这样后我们发先不仅实现了负载均衡,以后如果我们添加更多的服务端口也不需要修改代码
predicates(断言)
说直白的就是规则
如我们一直常用的- Path
路径要匹配
-
-After
-
- After=2021-02-02T15:41:14.218+08:00[Asia/Shanghai]
表示在2021-02-02T15:41:14.218+08:00[Asia/Shanghai]之后才能访问
-
-
-Before
-
- Before=2021-02-02T15:41:14.218+08:00[Asia/Shanghai]
表示在2021-02-02T15:41:14.218+08:00[Asia/Shanghai]之前才能访问
-
-
-Between
-
- Between=2021-02-02T15:41:14.218+08:00[Asia/Shanghai],2021-02-02T16:41:14.218+08:00[Asia/Shanghai]
表示在2021-02-02T15:41:14.218+08:00[Asia/Shanghai]-2021-02-02T16:41:14.218+08:00[Asia/Shanghai]之间才能访问
-
-
-Cookie
-
- Cookie=username,yuan
表示必须带有cookie而且key是username value是yuan的才能访问
CMD使用 => curl http://localhost:9420/payment/lb --cookie “username=yuan”
测试即可
-
-
-Header
-
- Header=X-Request-Id,\d+ #
表示必须带有Header而且key是X-Request-Id value是数字开头的才能访问
CMD使用 => curl http://localhost:9420/payment/lb -H “X-Request-Id:123”
测试即可
-
Filter过滤
过滤器只用添加一个类,在里面写方法即可
@Component
@Slf4j
public class MyLogGatewayFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if(uname == null){
//如果为(uname)null 日志打出=>用户名为空
log.info("用户名为空");
//并且状态码HTTP_NOT_ACCEPTABLE(不成功)
exchange.getResponse().setRawStatusCode(HttpStatus.HTTP_NOT_ACCEPTABLE);
//返回抛出不成功
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
//加载顺序 数字越小优先级越高
return 0;
}
}
我是本期小编⚪⚪
遇到Bug需要帮助,
欢迎加wx:
xmzl1988
备注"csdn博客“
温馨提示此为有偿服务;