SpringCloud Zuul的基本使用

Zuul基本使用

Zuul基本認識

Zuul是從設備和網站到後端應用程序全部請求的前門,爲內部的服務提供可配置的對外URL到服務的映射關係,基於JVM的後端路由器,它具有如下的功能:spring

  1. 認證與鑑權
  2. 壓力控制
  3. 金絲雀測試
  4. 動態路由
  5. 負載消減
  6. 靜態響應
  7. 主動流量

Zuul具有上面的功能,它底層是基於Servlet,本質是一系列Filter所構成的責任鏈。編程

Zuul的入門例子

ZuulServer的主要依賴以下:後端

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 </dependency>
複製代碼

配置以下:app

pring:
      application:
        name: zuul-server-1
    server:
      port: 8093
      undertow:
        worker-threads: 4000
        io-threads: 200
    eureka:
      client:
        serviceUrl:
          defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8092}/eureka/
      instance:
        prefer-ip-address: true
    zuul:
      routes:
        zuul-gateway:
          path: /client/**
          serviceId: zuul-client-1
複製代碼

啓動類的代碼以下:負載均衡

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulServerApplication.class,args);
    }
}
複製代碼

Client的代碼以下:

依賴以下:dom

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>
複製代碼

配置以下:測試

server:
  port: 8094
spring:
  application:
    name: zuul-client-1
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8092}/eureka/
  instance:
    prefer-ip-address: true
複製代碼

啓動類以下:網站

@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class ZuulClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulClientApplication.class,args);
    }
}
複製代碼

測試Controllerurl

@RestController
public class TestController {

	@GetMapping("/add")
	public Integer add(Integer a, Integer b){
		return a + b;
	}
}
複製代碼

測試結果:spa

到此一個簡單的Zuul網關的例子已經完成了。

Zuul的配置

  1. 路由配置以下:

    zuul: routes: zuul-client-1: path: /client/** serviceId: zuul-client-1

上面的這段路由配置也能夠寫成這樣:

zuul:
  routes:
    zuul-gateway:
      path: /client/**
      serviceId: zuul-client-1
複製代碼

上面的配置還能夠簡化成這個樣子:

zuul:
  routes:
    zuul-client-1:
      path: /client/**
複製代碼
  1. 單實例url映射 上面的配置主要是針對服務進行路由的,除此以外咱們還能夠針對serviceId替換成爲真實的物理地址。

zuul: routes: zuul-client-1: path: /client/** url: http://localhost:9093 #就是client的物理地址

  1. 多實例的又有配置

在默認的狀況下Zuul會使用Eureka中集成的基本負載均衡功能,若是要是有Ribbon的負載均衡的功能,就須要指定一個serviceId,此操做須要禁止Ribbon是Eureka,在E版以後,新增了負載均衡的策略的配置,配置以下:

ribbon-route: ribbon: NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #Ribbon LB Strategy listOfServers: localhost:8092,localhost:8093 #client services for Ribbon LB

  1. forward本地跳轉

若是須要在訪問某個接口的時候跳轉到這個方法上來處理,就須要用到Zuul的本地跳轉

zuul: routes: zuul-client-1: path: /client/** url: forward:/client

  1. 相同路徑的加載規則

zuul: routes: zuul-client-1: path: /client/** serviceId: zuul-client-1 zuul-client-2: path: /client/** serviceId: zuul-client-2

通過屢次的實驗後面的會將前面的覆蓋掉。

功能配置

  1. 路由前綴的配置 在配置路由規則的時候,我麼不能夠配置一個統一的代理前綴。

    zuul: prefix: /pre routes: zuul-client-1: /client/**

咱們在經過網關訪問後端的接口的時候就須要加上這個前綴,請求的路徑就編程了/pre/client/add,可是實際起做用的是/client/add,也就是Zuul會把代理路徑從請求路徑中移除。可使用stripPrefix=false來關閉這個功能。

zuul:
  routes:
    zuul-client-1:
      path: /client/**
      serviceId: zuul-client-1
      stripPrefix: false
複製代碼

此時請求的路徑是/pre/client/add,實際起做用的仍是/pre/client/add,可是通常的狀況下是選擇無視這個配置。

  1. 服務屏蔽與路由屏蔽

zuul: ignored-services: zuul-client-2 ignored-patterns: /div/ prefix: /pre routes: zuul-client-1: /client/**

上面的配置在Zuul在拉去服務列表,建立映射規則的時候,就會忽略掉zuul-client-2服務和/**/div/**接口

  1. 敏感頭信息 咱們能夠很容易的在頭部存放信息,進行傳遞,這些在咱們的系統內部傳遞是沒有什麼問題的,可是若是系統要和外部打交道就須要考慮防止這些數據的泄露。

zuul: ignored-services: zuul-client-2 ignored-patterns: /div/ prefix: /pre routes: zuul-client-1: /client/** sentiveHeaders: Cookie,Set-Cookie,Authorization

相關文章
相關標籤/搜索