01 基礎環境準備
02 一文讀懂Eureka
03 Zookeeper註冊中心
04 Consule註冊中心
05 Ribbon
06 OpenFegin
07 Hystrix全面解析
08 Gateway全面解析
09 Config配置中心
10 Bus消息總線
1 GateWay簡介
Spring Cloud Gateway是 Spring Cloud的個全新項目,基於 Spring5.0+ Spring Boot2.0和 Project Reactor等技術開發的網關,它旨在爲微服務架構提供一種簡單有效的統一的API路由管理方式。
Spring Cloud Gateway作爲 Spring Cloud生態系統中的網關,目標是替代Zuul,在 Spring Cloud2.0以上版本中,沒有對新版本的Zuul2.0以上最新高性能版本進行集成,仍然還是使用的Zu1x非 Reactor模式的老版本。而爲了提升網關的性能, Spring Cloud Gateway是基於 Webfluxi框架實現的,而 Nebflux框架底層則使用了高性能的 Reactor模式通信框架Netty。Spring Cloud Gatewayf的目標提供統一的路由方式目基於 Filter鏈的方式提供了網關基本的功能,例如:安全,監控/指標,和限流。
2 三大核心概念
路由:路由是網關最基礎的部分,路由信息有一個ID、一個目的URL、一組斷言和一組Filter組成。如果斷言路由爲真,則說明請求的URL和配置匹配。
斷言:Java8中的斷言函數。Spring Cloud Gateway中的斷言函數輸入類型是Spring5.0框架中的ServerWebExchange。Spring Cloud Gateway中的斷言函數允許開發者去定義匹配來自於http request中的任何信息,比如請求頭和參數等。
過濾器:一個標準的Spring webFilter。Spring cloud gateway中的filter分爲兩種類型的Filter,分別是Gateway Filter和Global Filter。過濾器Filter將會對請求和響應進行修改處理。
3 新建工程
(1) pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</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>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.zrs.springcloud</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
(2) application.yml
server:
port: 9002
spring:
application:
name: gateway-cloud
cloud:
gateway:
routes:
#id:實例唯一ID;uri:真實服務地址;predicates:斷言匹配;
- id: provider-route
uri: http://localhost:9000
predicates:
- Path=/ribbon/**
eureka:
instance:
#服務名稱
hostname: gateway-cloud
client:
serviceUrl:
#eureka註冊中心地址
defaultZone: http://localhost:7000/eureka/ #配置URL
#是否將自己註冊到EurekaServer
register-with-eureka: true
#是否從EurekaServer獲取已有的註冊服務
fetch-registry: true
(3) 啓動類
@SpringBootApplication
@EnableEurekaClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class);
}
}
(4) 啓動 Eureka、payment、paymentSlave、gateway
(5) 測試
4 路由編碼實現
創建GatewayConfig配置文件,實現訪問跳轉騰訊Nba
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customerRouteLocator(RouteLocatorBuilder builder){
RouteLocatorBuilder.Builder routes = builder.routes();
routes.route("nba",r->r.path("/nba").uri("https://sports.qq.com/nba/")).build();
return routes.build();
}
}
5 動態路由
5.1 修改網關的application.yml
server:
port: 9002
spring:
application:
name: gateway-cloud
cloud:
#從註冊中心動態創建路由
gateway:
discovery:
locator:
enabled: true
routes:
#id:實例唯一ID;uri:真實服務地址;predicates:斷言匹配;
- id: provider-route
uri: lb://PROVIDER-PAYMENT-SERVICE
predicates:
- Path=/ribbon/**
eureka:
instance:
#服務名稱
hostname: gateway-cloud
client:
serviceUrl:
#eureka註冊中心地址
defaultZone: http://localhost:7000/eureka/ #配置URL
#是否將自己註冊到EurekaServer
register-with-eureka: true
#是否從EurekaServer獲取已有的註冊服務
fetch-registry: true
5.2 啓動provider-slave、gateway服務
5.3 測試
6 predicates配置
6.1 時間
(1) 配置解析
-After : 之後訪問 例:- After=2017-01-20T17:42:47.789-07:00[America/Denver]
-Before: 之前訪問 例:- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
-Between: 之間訪問 例:- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
(2) 編寫中文獲取時間測試類
public class TimeTest {
public static void main(String[] args) {
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);
}
}
(3) 測試Before
修改application.yml配置文件
predicates:
- Path=/ribbon/**
- Before=2020-03-15T14:06:31.701+08:00[Asia/Shanghai]
6.2 Cookie
(1) 配置,此規則會根據Cookie name的值和正則表達式去匹配,匹配上執行路由,匹配不上不執行。
(2) 修改application.yml
predicates:
- Path=/ribbon/**
- Cookie=username,zrs
(3) 測試
curl http://localhost:9002/ribbon/server --cookie "username=zrs"
curl http://localhost:9002/ribbon/server
6.3 Header
(1) 如果請求有一個名爲X-request-Id的頭,並且該頭的值與\d+正則表達式匹配
(2) 修改application.yml
predicates:
- Path=/ribbon/**
- Header=X-Request-Id, \d+
(3) 測試
curl http://localhost:9002/ribbon/server -H "X-Request-Id:1234"
curl http://localhost:9002/ribbon/server
7 過濾器
路由過濾器可用於修改進入的HTTP請求和返回的HTTP響應,路由過滅器只能指定路由進行使用。Spring Cloud Gateway內置了多種路由過濾器,他們都由 Gateway Filterf的工廠類來產生。
7.1 創建配置文件
@Component
public class GatewayFilterConfig implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String username = exchange.getRequest().getQueryParams().getFirst("username");
if (StringUtils.isBlank(username)){
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
7.2 重啓測試
GITHUB
#分支Gateway-enviroment-release-v1.0
https://github.com/zhurongsheng666/spring-cloud-hoxton