Gateway是基於異步非阻塞模型上進行開發的,性能方面不需要擔心。雖然Netflix早就發布了最新的Zuul 2.x,
但Spring Cloud貌似沒有整合計划。而且Netflix相關組件都宣布進入維護期;不知前景如何?

我們為什么選擇Gatway?
1.neflix不太靠譜,zuul2.0一直跳票,遲遲不發布

2.SpringCloud Gateway具有如下特性

3.SpringCloud Gateway與Zuul的區別

三大核心概念:

原理:

搭建:
POM:( remove spring -boot- starter- web dependency)網關不需要web
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud2020</artifactId> <groupId>com.atguigu.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-gateway-gateway9527</artifactId> <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> </dependencies> </project>
YML:
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud: gateway: routes: - id: payment_routh #路由的ID,沒有固定規則但要求唯一,建議配合服務名(理論上可以順便命名,但不要重復) uri: http://localhost:8001 #匹配后提供服務的路由地址 predicates: - Path=/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: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
主啟動類:
package com.atguigu.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class GateWayMain9527 { public static void main(String[] args) { SpringApplication.run( GateWayMain9527.class,args); } }
分析:


編碼形式的網關配置:
package com.atguigu.springcloud.config; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class GateWayConfig { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) { RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes(); routes.route("path_rote_atguigu", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build(); return routes.build(); } }
結果:

問題和總結

通過微服務名實現動態路由:


CURL 相當關於postman:



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/** #斷言,路徑相匹配的進行路由 #- After=2020-03-08T10:59:34.102+08:00[Asia/Shanghai] #- Cookie=username,zhangshuai #並且Cookie是username=zhangshuai才能訪問 #- Header=X-Request-Id, \d+ #請求頭中要有X-Request-Id屬性並且值為整數的正則表達式 #- Host=**.atguigu.com #- Method=GET #- Query=username, \d+ #要有參數名稱並且是正整數才能路由 eureka: instance: hostname: cloud-gateway-service client: service-url: register-with-eureka: true fetch-registry: true defaultZone: http://eureka7001.com:7001/eureka
Filter的使用:

自定義過濾器

案例代碼:
package com.atguigu.springcloud.filter; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import java.util.Date; @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(username)){ log.info("*****用戶名為Null 非法用戶,(┬_┬)"); exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);//給人家一個回應 return exchange.getResponse().setComplete(); } return chain.filter(exchange); } @Override public int getOrder() { return 0; } }
