1.前言
經過上一篇博客的Hystrix學習後,接下來我們正式開始網關的學習,目前常用的網關組件有Zuul和Gateway,這裏我們就重點來學習一下這兩個組件的使用。
Zuul和Gateway區別:
雖然在服務網關有了zuul,其本身還是基於servlet實現的,換言之還是同步阻塞方式的實現。就其本身來講它的最根本弊端也是再此。而非阻塞帶來的好處不言而喻,高效利用線程資源進而提高吞吐量,基於此Spring率先拿出針對於web的殺手鐗,對,就是webflux。而Gateway本身就是基於webflux基礎之上實現的。畢竟spring推出的技術,當然要得以推廣嘛。不過就國內的軟件公司而言爲了穩定而選擇保守,因此就這項技術的廣度來說我本身還是在觀望中。
2.Zuul
ZUUL是Netflix開源的微服務網關,它可以和Eureka、Ribbon、Hystrix等組件配合使用,它主要用作反向代理、Filter擴展、動態加載、動態路由、壓力測試、彈性擴展、審查監控、安全檢查等。
- 像之前一樣建立名爲zuul-client的模塊,同樣記得在選擇依賴時選擇Spring Web、Eureka Discovery、Zuul,如圖所示:
- 修改配置文件application.yml,代碼如下:
server:
port: 18767
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:18761/eureka/
spring:
application:
name: service-zuul
zuul:
routes:
service-ribbon:
path: /ribbon/**
serviceId: service-ribbon
service-feign:
path: /openfeign/**
serviceId: service-openfeign
這裏來簡單講解下參數:service-ribbon
、service-feign
這個是路由節點名詞,可以隨便起,但爲了統一,建議和服務名稱一致,path
表示我們可以把服務名稱替換爲這個路徑,最後serviceId
必須和註冊中心一致
- 修改啓動類,使用註解
@EnableZuulProxy
開啓Zuul路由支持。代碼如下:
package com.springclouddemo.zuulclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulClientApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulClientApplication.class, args);
}
}
- 按照順序依次啓動
eureka-server
,eureka-client
,ribbon-client
,openfeign-client
,zuul-client
,訪問註冊中心頁面,可以看到zuul模塊已經啓動,如圖所示:
- 分別訪問
http://localhost:18767/ribbon/hi
,http://localhost:18767/openfeign/hi
可以看到如下顯示結果,說明zuul配置成功,如圖所示:
3.GateWay
spring cloud gateway是基於netty運行的環境,在servlet容器環境或者把它構建爲war包運行的話是不允許的,因此在項目當中沒有必要添加spring-boot-starter-web。其是基於spring webflux技術。
- 像之前一樣建立名爲gatesway-client的模塊,同樣記得在選擇依賴時選擇Spring Web、Eureka Discovery、GateWay,如圖所示:
- 修改配置文件application.yml,Gateway提供了兩種配置方式,一種是直接配置文件即可,另外一種是使用代碼RouteLocator類來配置。這裏演示比較方便的配置文件,代碼如下:
server:
port: 18768
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:18761/eureka/
spring:
application:
name: service-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: ribbon
uri: lb://service-ribbon
predicates:
- Path= /ribbon/**
filters:
- StripPrefix=1
- id: openfeign
uri: lb://service-openfeign
predicates:
- Path= /openfeign/**
filters:
- StripPrefix=1
Eureka管理的服務,我們uri
使用lb協議即可,其他需要使用對應的協議。
Predicates
意思爲斷言,也就是我們/ribbon/**
這樣的路徑會被轉發到相應的
服務。
注意:Gateway默認轉發是全路徑的,設置StripPrefix=1
表示從二級url路徑轉發,即http://localhost:port/activity/test
將會轉發到http://{activity}/test
- 修改啓動類,代碼如下:
package com.springclouddemo.gatewayclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayClientApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayClientApplication.class, args);
}
}
- 按照順序依次啓動
eureka-server
,eureka-client
,ribbon-client
,openfeign-client
,gateway-client
,訪問註冊中心頁面,可以看到gateway模塊已經啓動,如圖所示:
- 分別訪問
http://localhost:18768/ribbon/hi
,http://localhost:18768/openfeign/hi
可以看到如下顯示結果,說明gateway配置成功,如圖所示: