【夯實Spring Cloud】Spring Cloud中使用Zuul實現路由網關詳解

本文屬於【夯實Spring Cloud】系列文章,該系列旨在用通俗易懂的語言,帶大家瞭解和學習Spring Cloud技術,希望能給讀者帶來一些乾貨。系列目錄如下:

【夯實Spring Cloud】Dubbo沉睡5年,Spring Cloud開始崛起!
【夯實Spring Cloud】Spring Cloud中基於maven的分佈式項目框架的搭建
【夯實Spring Cloud】Spring Cloud中的Eureka服務註冊與發現詳解
【夯實Spring Cloud】Spring Cloud中如何完善Eureka中的服務信息
【夯實Spring Cloud】Spring Cloud中使用Eureka集羣搭建高可用服務註冊中心
【夯實Spring Cloud】Spring Cloud中的Eureka和Zookeeper的區別在哪?
【夯實Spring Cloud】Spring Cloud中使用Ribbon實現負載均衡詳解(上)
【夯實Spring Cloud】Spring Cloud中使用Ribbon實現負載均衡詳解(下)
【夯實Spring Cloud】Spring Cloud中自定義Ribbon負載均衡策略
【夯實Spring Cloud】Spring Cloud中使用Feign實現負載均衡詳
【夯實Srping Cloud】Spring Cloud中使用Hystrix實現斷路器原理詳解(上)
【夯實Srping Cloud】Spring Cloud中使用Hystrix實現斷路器原理詳解(下)
【夯實Spring Cloud】Spring Cloud中使用Zuul實現路由網關詳解
【夯實Spring Cloud】Spring Cloud分佈式配置中心詳解(正在寫……)
【夯實Spring Cloud】未完待續


前面對 Hystrix 的知識做了比較詳細的分析,這篇文章主要對 Zuul 來做一下講解。

1. 什麼是 Zuul?

官方 GitHub wiki:https://github.com/Netflix/zuul/wiki

Zuul 包含了對請求的路由和過濾兩個最主要的功能。其中,路由功能負責將外部請求轉發到具體的微服務實例上,是實現外部訪問統一入口的基礎。而過濾功能則是負責對請求的處理過程進行干預,是實現請求校驗、服務聚合等功能的基礎。

Zuul 和 eureka 進行整合,將 zuul 自身註冊爲eureka服務治理下的應用,同時從eureka中獲得其他微服務的消息,也即以後的訪問微服務都是通過zuul跳轉之後獲得。

所以,Zuul 提供:代理+路由+過濾三大功能。接下來,我們來看一看如何實現。

2. 落地實現

我們自定義一個新的項目工程:microservice-zuul-gateway。

2.1 依賴導入

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

我們需要將 zuul 註冊到 eureka,所以也需要導入 eureka client 依賴。

2.2 主啓動類註解

在主啓動類中需要添加:@EnableZuulProxy 註解,表示開啓 zuul 代理。

@SpringBootApplication
@EnableZuulProxy
public class ZuulGateway {
    public static void main(String[] args) {
        SpringApplication.run(ZuulGateway.class, args);
    }
}

2.3 application.yml 配置文件

接下來看看 application.xml 配置文件,由於要註冊到 eureka,所以和 eureka 相關的配置,也是需要的,如下:

# 服務端口號
server:
  port: 6001

spring:
  application:
    name: microservice-zuul-gateway # 對外暴露的服務名稱

# 客戶端註冊進eureka服務列表裏
eureka:
  client:
    service-url:
      defaultZone: http://eureka01:7001/eureka/,http://eureka02:7002/eureka/,http://eureka03:7003/eureka/,
    healthcheck:
      enabled: true
  instance:
    instance-id: zuul網關服務-6001  # 人性化顯示出服務的信息
    prefer-ip-address: true    # 訪問路徑可顯示ip地址
    lease-renewal-interval-in-seconds: 2
    lease-expiration-duration-in-seconds: 5

# 使用actuator來展示項目的基本信息
info:
  author.name: shengwu ni
  app.name: microservice
  server.port: ${server.port}
  application.name: ${spring.application.name}

2.4 測試一下

啓動 eureka 集羣、訂單服務(8001)和 zuul-gateway(6001)。在瀏覽器中輸入 http://eureka7001:7001 ,可以看到如下信息,說明服務都正常註冊到 eureka。
eureka
我們首先直接訪問一下訂單服務,確保服務可用:http://localhost:8001/provider/order/get/1
然後我們通過 zuu l來訪問該訂單服務:http://localhost:6001/microservice-order/provider/order/get/1 ,也可以正常訪問。microservice-order 是訂單服務的服務名。即 zuul 在 eureka 裏找到了一個叫 microservice-order 的訂單服務,然後去請求數據。所以說,zuul 是可以根據註冊到 eureka 中的服務名稱來訪問服務的。

那麼我們自然會想到,如果我把三個訂單服務都啓動起來,因爲它們的服務名稱都是 microservice-order,zuul 到底會將請求轉發給哪個服務呢?

啓動下 8001、8002 和 8003,然後還是訪問http://localhost:6001/microservice-order/provider/order/get/1 ,查看輸出的信息,可以知道,zuul 中默認集成了輪詢的規則,三個服務輪流調用。

3. 自定義路由規則

如果我們不想像上面那樣在 url 中直接暴露微服務名稱,可以在配置文件中配一下路由規則。

# 配置路由規則
zuul:
  routes:
    order:
      serviceId: microservice-order
      path: /order/**

這樣的話,在配置了路由規則之後,就可以使用:http://localhost:6001/order/provider/order/get/1 來訪問訂單服務了。

但是這樣的話,原來使用微服務名稱的方式還是可以訪問,所以我們可以禁用原來使用微服務名稱的方式訪問。如下:

# 配置路由規則
zuul:
  ignored-services: microservice-order # 不允許用微服務名訪問了,如果禁用所有的,可以使用 "*"
  routes:
    # 如下指定新的映射
    order:
      serviceId: microservice-order
      path: /order/**

我們還可以給路由加一個統一的前綴:

# 配置路由規則
zuul:
  ignored-services: microservice-order # 不允許用微服務名訪問了,如果禁用所有的,可以使用 "*"
  routes:
    prefix: /zuul # 給路由加一個統一的前綴
    # 如下指定新的映射
    order:
      serviceId: microservice-order
      path: /order/**

這樣的話,就可以使用:http://localhost:6001/zuul/order/provider/order/get/1 來訪問訂單服務了。

OK,Spring Cloud 中使用 zuul 實現路由就介紹這麼多。


源碼下載地址:https://gitee.com/eson15/springcloud_study
更多優質文章請關注我的微信公衆號【程序員私房菜】,回覆“資源”和“架構”可以領取優質的視頻學習資源。
程序員私房菜

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章