Spring Cloud--zuul網關

前言

Github:https://github.com/yihonglei/SpringCloud-Study

Eureka註冊中心:eureka-server

服務提供者(訂單服務):eureka-provider-order

zuul網關工程:eureka-gateway-zuul

一 eureka-gateway-zuul

Zuul可以通過加載動態過濾機制,從而實現以下各項功能:

  • 驗證與安全保障: 識別面向各類資源的驗證要求並拒絕那些與要求不符的請求。
  • 審查與監控: 在邊緣位置追蹤有意義數據及統計結果,從而爲我們帶來準確的生產狀態結論。
  • 動態路由: 以動態方式根據需要將請求路由至不同後端集羣處。
  • 壓力測試: 逐漸增加指向集羣的負載流量,從而計算性能水平。
  • 負載分配: 爲每一種負載類型分配對應容量,並棄用超出限定值的請求。
  • 靜態響應處理: 在邊緣位置直接建立部分響應,從而避免其流入內部集羣。
  • 多區域彈性: 跨越AWS區域進行請求路由,旨在實現ELB使用多樣化並保證邊緣位置與使用者儘可能接近。

1、項目結構

2、pom.xml(jar包依賴)

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

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

3、application.yml(配置文件)

server:
  port: 9003
spring:
  application:
    name: eureka-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    instance-id: eureka-gateway-zuul-9003
    prefer-ip-address: true

server.port 服務端口;

spring.application.name 應用程序名稱;

eureka.client.service-url.defaultZone 註冊中心;

eureka.instance.instance-id 服務id; 

4、EurekaGatewayZuulApplication(啓動類)

package com.lanhuigu.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * 網關工程,@EnableZuulProxy開啓網關支持
 *
 * @auther: yihonglei
 * @date: 2019-07-12 18:57
 */
@SpringBootApplication
@EnableZuulProxy
public class EurekaGatewayZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaGatewayZuulApplication.class, args);
    }
}

@EnableZuulProxy註解開啓網關支持。

二 服務啓動

1、啓動註冊中心

eureka-server工程EurekaServerApplication類。

2、啓動服務提供者

eureka-provider-order工程EurekaOrderApplication類。

3、啓動網關工程

eureka-gateway-zuul工程EurekaGatewayZuulApplication類。

三 網關測試

1、訪問註冊中心

http://localhost:9000/

註冊中心可以看到zuul和order服務實例。

2、訪問order服務

通過網關調用前,先保證order服務是通的。

http://localhost:8001/order/queryOrderInfo

3、通過網關訪問order服務

http://localhost:9003/eureka-provider-order/order/queryOrderInfo

http:ip:port/服務實例/服務地址。

網關根據服務請求實例去尋找服務,併發送請求到相應服務。

四 zuul高級配置

1、默認通過服務實例訪問

服務實例對應服務的spring.application.name的值。

默認通過服務實例訪問服務,比如訂單服務服務實例,會暴露服務名稱,不安全。

http://localhost:9003/eureka-provider-order/order/queryOrderInfo

給服務訪問起一個映射名稱。

增加配置:

zuul:
  routes:
    # 1、指定微服務名稱和路徑映射
    eureka-provider-order: /order-service/**

完整配置:

server:
  port: 9003
spring:
  application:
    name: eureka-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    instance-id: eureka-gateway-zuul-9003
    prefer-ip-address: true
zuul:
  routes:
    # 1、指定微服務名稱和路徑映射
    eureka-provider-order: /order-service/**

重啓eureka-gateway-zuul服務,訪問地址由服務實例變爲映射地址。

http://localhost:9003/order-service/order/queryOrderInfo

2、關閉通過服務實例名稱訪問

我們配置了服務實例映射後,爲了安全,把服務實例訪問關閉。

增加配置:

zuul:
  ignored-services: "*"

完整配置:

server:
  port: 9003
spring:
  application:
    name: eureka-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    instance-id: eureka-gateway-zuul-9003
    prefer-ip-address: true
zuul:
  routes:
    # 1、指定微服務名稱和路徑映射
    eureka-provider-order: /order-service/**

  # 表示關閉所有通過微服務名稱映射訪問
  ignored-services: "*"

重啓eureka-gateway-zuul服務,再次通過服務實例訪問報404,

http://localhost:9003/eureka-provider-order/order/queryOrderInfo訪問報404。

3、zuul配置大全參考

zuul裏面有着大量配置,根據實際需要配置即可。可以參考官網關於zuul的配置:

https://cloud.spring.io/spring-cloud-static/Finchley.SR3/single/spring-cloud.html#_router_and_filter_zuul

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