搭建微服務框架(服務網關處理)
本篇來進行介紹微服務網關集成的使用操作,基於SpringCloudGateway。
本文源地址:搭建微服務框架(服務網關處理)
Github地址:SQuid
Spring-Cloud-Gateway
由於SpringCloud-Netflix的Zuul組件不再維護,而Spring官方推出了Gateway的新組件,並且支持了SringCloud2.0的版本,所以在選型方面,直接就選擇了Spring官方的Gateway。
介紹Gateway,不得不將它與Zuul進行比較。
的確,Zuul的網關處理流程很一目了然,基於一個 ZuulFilter
,而后可以定義 preRoute()
route()
postRoute()
error()
,類似於Spring的前置通知,后置通知,環繞通知,算得上是不錯的網關處理組件,
比較可惜的是,SpringCloud-Netfilx的停止更新,使得SpringCloud的網關處理選擇為了 Spring-Cloud-Gateway
。
眾所周知,Zuul是基於Serverlet,而Gateway是基於Netty,兩個誰更優秀,這個目前也是進行不了一個定義,廣義上來說也是傳統的 Http和TCP
的比較。
下面介紹Gateway的使用,👇
使用Spring-Cloud-Gateway
首先貼一下工程截圖:
很簡單的一個例子,先新建一個 squid-gateway
的工程,引入如下依賴:
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.isomorphism</groupId>
<artifactId>token-bucket</artifactId>
<version>1.7</version>
</dependency>
GatewayApplication
@SpringBootApplication
@EnableDiscoveryClient
public class GateWayApplication {
public static void main(String[] args) {
SpringApplication.run(GateWayApplication.class, args);
}
}
application.yaml
最主要的就是application.yaml文件了,里面可以配置路由轉發規則,當然也可以直接在 GatewayApplication.java
文件中寫java代碼來進行轉發,不過我總認為這樣不太直觀,下面來看一下文件:
server:
port: 8006
spring:
application:
name: squid-gateway
cloud:
nacos:
discovery:
server-addr: yanzhenyidai.com:8848
gateway:
# default-filters:
# - PrefixPath=/gateway
# - AddResponseHeader=X-Response-Default-Foo, Default-Bar
routes:
- id: squid-example-dubbo-provider
uri: lb://squid-example-dubbo-provider
predicates:
- Path=/dubbo/**
filters:
- StripPrefix=1
- id: squid-oauth2
uri: lb://squid-oauth2
predicates:
- Path=/oauth/**
關鍵的兩點:
- Nacos
Nacos不用多說,之前介紹的本次使用的注冊服務中心,配置好Nacos的信息,在routes下轉發會直接發現到注冊好的實例ID服務,進行請求訪問並返回處理結果信息。
- Gateway-routes
routes中配置的信息大致如下:
- id 為唯一值,可以使用項目模塊名配置。
- uri 為路徑,lb代表
loadblance
的意思,后續跟隨的可以為 IP 請求地址,也可以為注冊中心中的示例ID名稱。 - predicates 代表正則匹配規則。
- Path 為請求時的簡寫路徑。
- StripPrefix 請求過濾的地方,1則表示過濾簡稱的第一項。
總結
當然Gateway遠遠不止以上這么多的配置信息,因為目前對Gateway還是處於了解中,並沒有深入的讀它的設計理念。
對於Gateway,個人覺得,如果服務器上已經有了Nginx,並且工程中外部的請求沒有很多需要特別邏輯處理,直接用Nginx做網關就可以了,而這個也正是我沒有深入去了解Gateway的原因。😜
參考資料:
Spring-Cloud-Gateway 2.2.2.RELEASE(WIKI)
Spring-Cloud-Gateway (GITHUB)
Spring-Cloud-Gateway (Spring.io)
BAELDUNG.com (GateWay)