springboot2.0下的zuul路由網關初探

Zuul作爲微服務系統的網關組件,用於構建邊界服務,致力於動態路由、過濾、監控、彈性伸縮和安全。

爲什麼需要Zuul

Zuul、Ribbon以及Eureka結合可以實現智能路由和負載均衡的功能;網關將所有服務的API接口統一聚合,統一對外暴露。外界調用API接口時,不需要知道微服務系統中各服務相互調用的複雜性,保護了內部微服務單元的API接口;網關可以做用戶身份認證和權限認證,防止非法請求操作API接口;網關可以實現監控功能,實時日誌輸出,對請求進行記錄;網關可以實現流量監控,在高流量的情況下,對服務降級;API接口從內部服務分離出來,方便做測試。

Zuul通過Servlet來實現,通過自定義的ZuulServlet來對請求進行控制。核心是一系列過濾器,可以在Http請求的發起和響應返回期間執行一系列過濾器。Zuul採取了動態讀取、編譯和運行這些過濾器。過濾器之間不能直接通信,而是通過RequestContext對象來共享數據,每個請求都會創建一個RequestContext對象。

Zuul生命週期如下圖。 當一個客戶端Request請求進入Zuul網關服務時,網關先進入”pre filter“,進行一系列的驗證、操作或者判斷。然後交給”routing filter“進行路由轉發,轉發到具體的服務實例進行邏輯處理、返回數據。當具體的服務處理完成後,最後由”post filter“進行處理,該類型的處理器處理完成之後,將Request信息返回客戶端。 

Zuul是Netflix出品的一個基於JVM路由和服務端的負載均衡器.

  Zuul功能:

  • 認證
  • 壓力測試
  • 金絲雀測試
  • 動態路由
  • 負載削減
  • 安全
  • 靜態響應處理
  • 主動/主動交換管理

Zuul的規則引擎允許通過任何JVM語言來編寫規則和過濾器, 支持基於Java和Groovy的構建。

現在我們簡單的先搭建一個網關服務器

首先單獨建一個項目,用來做網關服務器

引入相關的主要jar包:

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

引入spring-cloud-starter-netflix-eureka-client的目的是本身通過url映射的方式來實現zuul的轉發有侷限性,比如每增加一個服務就需要配置一條內容,另外後端的服務如果是動態來提供,就不能採用這種方案來配置了。實際上在實現微服務架構時,服務名與服務實例地址的關係在eureka server中已經存在了,所以只需要將Zuul註冊到eureka server上去發現其他服務,就可以實現對serviceId的映射。

加入yml的配置文件,主要用來連接eureka,及配置網關路由規則

server:
  port: 8088
spring.application.name: startGateway
eureka:
  client:
    service-url: 
      defaultZone: http://root:root@localhost:10000/eureka
zuul.routes.three.path: /three/**
zuul.routes.three.service-id: three
zuul.routes.three.stripPrefix: false
zuul.routes.five.path: /five/**
zuul.routes.five.service-id: five
zuul.routes.five.stripPrefix: false

這邊有個坑:

    設置 zuul.prefix 可以爲所有的匹配增加前綴, 例如 /api,代理前綴默認會從請求路徑中移除(通過 zuul.stripPrefix=false 可以關閉這個功能).

  1. 反響代理配置  
  2. 這裏的配置類似nginx的反響代理  
  3. 當請求/api/**會直接交給listOfServers配置的服務器處理  
  4. 當stripPrefix=true的時候 (http://127.0.0.1:3333/api/user/list -> http://192.168.1.100:8080/user/list)  
  5. 當stripPrefix=false的時候(http://127.0.0.1:5555/api/user/list -> http://192.168.1.100:8080/api/user/list)  
  6. zuul.routes.api.path=/api/**  
  7. zuul.routes.api.stripPrefix=false  
  8. api.ribbon.listOfServers=192.168.1.100:8080,192.168.1.101:8080,192.168.1.102:8080

你也可以在指定服務中關閉這個功能:

zuul.routes.five.path: /five/**
zuul.routes.five.service-id: five
zuul.routes.five.stripPrefix: false
在這個例子中, 請求"/five/a"將被跳轉到"five"服務的"/five/a"上.如果不配置stripPrefix: false的話就會默認路由到/a上,忽略/five這個前綴,導致404找不到資源

之後在啓動類上配置

package cn.chinotan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * @program: test
 * @description: 啓動類
 * @author: xingcheng
 * @create: 2018-12-9 15:39
 **/
@SpringCloudApplication
@EnableEurekaClient
@EnableZuulProxy
public class StartGateway {

    public static void main(String[] args) {
        SpringApplication.run(StartGateway.class, args);
    }

}

就完成了一個簡單的網關路由

其中@EnableZuulProxy是@EnableZuulServer的加強,@SpringCloudApplication會包含@EnableEurekaClient,所以其實@EnableEurekaClient不需要寫

之後在寫兩個測試controller進行路由判斷:

package cn.chinotan.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: test
 * @description: zuul測試控制器
 * @author: xingcheng
 * @create: 2018-12-08 18:09
 **/
@RestController
@RequestMapping("/five")
public class ZuulTestFiveController {
    
    @GetMapping("/hello/{name}")
    public String ZuulTestFive(@PathVariable("name") String name) {
        return "hello " + name + "  this is ZuulTestFive";
    }
    
}

 

package cn.chinotan.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: test
 * @description: zuul測試控制器
 * @author: xingcheng
 * @create: 2018-12-08 18:09
 **/
@RestController
@RequestMapping("/three")
public class ZuulTestThreeController {
    
    @GetMapping("/hello/{name}")
    public String ZuulTestFive(@PathVariable("name") String name) {
        return "hello " + name + " this is ZuulTestThree";
    }
    
}

當然這些服務也得連接到euerka上,好讓代理網關可以根據service-id進行服務發現

其中,euerka上的連接情況如下:

可以看到網關服務器的端口是8088,兩個服務的網關是5555-five和3333-three

接下來,我們不直接請求提供服務的three和five服務器,而是請求網關代理

可以看到網關服務成功的路由了這兩次請求

服務過濾

Zuul還有一個主要的功能,便是服務過濾,比如,用戶在登錄前,可以將服務請求過濾到指定的頁面去。 
在項目中,新增一個MyFilter類。代碼如下:

@Component
public class MyFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run(){
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        Object accessToken = request.getParameter("token");
        if(accessToken == null) {
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            try {
                ctx.getResponse().setHeader("Content-Type", "text/html;charset=UTF-8");
                ctx.getResponse().getWriter().write("登錄信息爲空!");
            }catch (Exception e){}
            return null;
        }
        return null;
    }
}

其中,filterType方法,返回一個字符串代表過濾器的類型,在zuul中定義了四種不同生命週期的過濾器類型,具體如下:pre:路由之前 
routing:路由之時 
post: 路由之後 
error:發送錯誤調用 
filterOrder:過濾的順序 
shouldFilter:這裏可以寫邏輯判斷,是否要開啓過濾 
run:過濾器的具體邏輯。可用很複雜,包括查sql,nosql去判斷該請求到底有沒有權限訪問。
一般我們在使用時,不手打“pre”這些類型,而是通過調用Zuul中已寫好的FilterConstants類,其中封裝了所有的過濾器類型。這樣可以避免打錯字符而導致錯誤的發生。 
接下來,我們訪問http://localhost:9005/feign/welcome?name=Cheng 顯示:

登錄信息爲空!

在後面加上token,http://localhost:9005/feign/welcome?name=Cheng&token=111就可以正常訪問了

類似的過濾器很多,我們可以自定義,從而實現統一的網關控制、監控、跨域、流量控制、負載均衡,身份認證,服務降級等等。

 

 

 

 

 

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