SpringCloud(第 018 篇)Zuul 服務 API 網關微服務之代理與反向代理

SpringCloud(第 018 篇)Zuul 服務 API 網關微服務之代理與反向代理

-java

1、大體介紹

一、API 服務網關顧名思義就是統一入口,相似 nginx、F5 等功能同樣,統一代理控制請求入口,弱化各個微服務被客戶端記憶功能;
二、本章節主要講解了使用 zuul 的代理功能與反向代理功能,固然 zuul 還有不少屬性設置,我就沒一一列舉全部的測試方法了;
三、http://localhost:8150/routes 地址能夠查看該zuul微服務網關代理了多少微服務的serviceId;

2、實現步驟

2.1 添加 maven 引用包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springms-gateway-zuul</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.springms.cloud</groupId>
        <artifactId>springms-spring-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <!-- API網關模塊 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

        <!-- 客戶端發現模塊,因爲文檔說 Zuul 的依賴裏面不包括 eureka 客戶端發現模塊,因此這裏還得單獨添加進來 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

</project>

2.2 添加應用配置文件(springms-gateway-zuulsrcmainresourcesapplication.yml)

spring:
  application:
    name: springms-gateway-zuul
server:
  port: 8150
eureka:
  datacenter: SpringCloud   # 修改 http://localhost:8761 地址 Eureka 首頁上面 System Status 的 Data center 顯示信息
  environment: Test         # 修改 http://localhost:8761 地址 Eureka 首頁上面 System Status 的 Environment 顯示信息
  client:
    service-url:
      defaultZone: http://admin:admin@localhost:8761/eureka
#    healthcheck:  # 健康檢查
#      enabled: true
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}


#####################################################################################################
# 測試二,自定義路徑配置,給 springms-provider-user 微服務添加前綴地址,反向代理用戶微服務
#zuul:
#  routes:
#    springms-provider-user: /user/**



## 測試三,自定義路徑配置,給 springms-provider-user 微服務添加前綴地址,反向代理用戶微服務,其它代理路徑一概失效
#zuul:
#  ignoredServices: '*'
#  routes:
#    springms-provider-user: /user/**



# 測試四,自定義路徑配置,給 springms-provider-user 微服務添加前綴地址,代理、反向代理用戶微服務,忽略禁用 springms-consumer-movie 代理、反向代理路徑
#zuul:
#  ignoredServices: springms-consumer-movie
#  routes:
#    springms-provider-user: /user/**
#####################################################################################################

#####################################################################################################
# 打印日誌
logging:
  level:
    root: INFO
    com.springms: DEBUG
#####################################################################################################

#####################################################################################################
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000
#####################################################################################################

#####################################################################################################
# 解決第一次請求報超時異常的方案,由於 hystrix 的默認超時時間是 1 秒,所以請求超過該時間後,就會出現頁面超時顯示 :
#
# 這裏就介紹大概三種方式來解決超時的問題,解決方案以下:
#
# 第一種方式:將 hystrix 的超時時間設置成 5000 毫秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
#
# 或者:
# 第二種方式:將 hystrix 的超時時間直接禁用掉,這樣就沒有超時的一說了,由於永遠也不會超時了
# hystrix.command.default.execution.timeout.enabled: false
#
# 或者:
# 第三種方式:索性禁用feign的hystrix支持
# feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持

# 超時的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
# 超時的解決方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
# hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
#####################################################################################################

2.3 添加zuul服務網關微服務啓動類(springms-gateway-zuulsrcmainjavacomspringmscloudMsGatewayZuulApplication.java)

package com.springms.cloud;

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

/**
 * Zuul 服務 API 網關微服務之代理與反向代理。
 *
 * 注意 EnableZuulProxy 註解能註冊到 eureka 服務上,是由於該註解包含了 eureka 客戶端的註解,該 EnableZuulProxy 是一個複合註解。
 *
 * @EnableZuulProxy --> { @EnableCircuitBreaker、@EnableDiscoveryClient } 包含了 eureka 客戶端註解,同時也包含了 Hystrix 斷路器模塊註解。
 *
 * http://localhost:8150/routes 地址能夠查看該zuul微服務網關代理了多少微服務的serviceId。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/24
 *
 */
@SpringBootApplication
@EnableZuulProxy
public class MsGatewayZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsGatewayZuulApplication.class, args);
        System.out.println("【【【【【【 GatewayZuul微服務 】】】】】】已啓動.");
    }
}

3、測試

/****************************************************************************************
 1、Zuul 服務 API 網關微服務之代理與反向代理(正常狀況測試):

 一、編寫 application.yml 文件,添加應用程序的註解 EnableZuulProxy 配置;
 二、啓動 springms-discovery-eureka 模塊服務,啓動1個端口;
 三、啓動 springms-provider-user 模塊服務,啓動1個端口(application.yml 文件中的 appname 屬性不去掉的話,測試一是沒法測試經過的);
 四、啓動 springms-consumer-movie 模塊服務,啓動1個端口;
 五、啓動 springms-gateway-zuul 模塊服務;

 六、新起網頁頁籤,輸入 http://localhost:7900/simple/3 正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;
 七、新起網頁頁籤,輸入 http://localhost:7901/movie/3 正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;

 總結一:第六、7步正常,說明 springms-provider-user、springms-consumer-movie 兩個服務目前正常;

 八、新起網頁頁籤,而後輸入 http://localhost:8150/springms-provider-user/simple/3,正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;
 九、新起網頁頁籤,而後輸入 http://localhost:8150/springms-consumer-movie/movie/4,正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;

 總結二:第八、9步也能正常打印用戶信息,說明 API 網關已經生效了,能夠經過API服務器地址連接各個微服務的 http://localhost:8150/serviceId/path 這樣的路徑來訪問了;
 ****************************************************************************************/

/****************************************************************************************
 2、Zuul 服務 API 網關微服務之代理與反向代理(自定義路徑配置,給 springms-provider-user 微服務添加前綴地址,反向代理用戶微服務):

 一、編寫 application.yml 文件,添加應用程序的註解 EnableZuulProxy 配置;
     # 測試二,自定義路徑配置,給 springms-provider-user 微服務添加前綴地址,反向代理用戶微服務
     zuul:
        routes:
            springms-provider-user: /user/**
 二、啓動 springms-discovery-eureka 模塊服務,啓動1個端口;
 三、啓動 springms-provider-user 模塊服務,啓動1個端口(application.yml 文件中的 appname 屬性不去掉的話,測試一是沒法測試經過的);
 四、啓動 springms-consumer-movie 模塊服務,啓動1個端口;
 五、啓動 springms-gateway-zuul 模塊服務;

 六、新起網頁頁籤,輸入 http://localhost:7900/simple/3 正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;
 七、新起網頁頁籤,輸入 http://localhost:7901/movie/3 正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;

 總結一:第六、7步正常,說明 springms-provider-user、springms-consumer-movie 兩個服務目前正常;

 八、新起網頁頁籤,而後輸入 http://localhost:8150/springms-provider-user/simple/3,正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;
 九、新起網頁頁籤,而後輸入 http://localhost:8150/springms-consumer-movie/movie/4,正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;

 總結二:第八、9步也能正常打印用戶信息,說明 API 網關已經生效了,能夠經過API服務器地址連接各個微服務的 http://localhost:8150/serviceId/path 這樣的路徑來訪問了;

 十、新起網頁頁籤,而後輸入 http://localhost:8150/user/simple/3,正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來,可見【用戶微服務】的地址被改變生效了,同時被 API 網關反向代理了,也就是說 http 的請求 /user 將被髮送到【用戶微服務】;
 十一、新起網頁頁籤,而後輸入 http://localhost:8150/user/movie/4,正常狀況下訪問不通,理應訪問不通的;

 總結三:zuul.routes 屬性僅僅只是爲了給 springms-provider-user 微服務添加了 user 前綴,因此電影微服務加 user 前綴固然訪問不通的;
 ****************************************************************************************/

/****************************************************************************************
 3、Zuul 服務 API 網關微服務之代理與反向代理(自定義路徑配置,給 springms-provider-user 微服務添加前綴地址,反向代理用戶微服務,其它代理路徑一概失效):

 一、編寫 application.yml 文件,添加應用程序的註解 EnableZuulProxy 配置;
     # 測試三,自定義路徑配置,給User微服務添加前綴地址,反向代理User微服務,不反向代理電影微服務
     zuul:
        ignoredServices: '*'
        routes:
            springms-provider-user: /user/**
 二、啓動 springms-discovery-eureka 模塊服務,啓動1個端口;
 三、啓動 springms-provider-user 模塊服務,啓動1個端口(application.yml 文件中的 appname 屬性不去掉的話,測試一是沒法測試經過的);
 四、啓動 springms-consumer-movie 模塊服務,啓動1個端口;
 五、啓動 springms-gateway-zuul 模塊服務;

 六、新起網頁頁籤,輸入 http://localhost:7900/simple/3 正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;
 七、新起網頁頁籤,輸入 http://localhost:7901/movie/3 正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;

 總結一:第六、7步正常,說明 springms-provider-user、springms-consumer-movie 兩個服務目前正常;

 八、新起網頁頁籤,而後輸入 http://localhost:8150/springms-provider-user/simple/3,正常狀況下不能被代理了,訪問頁面不存在,出現404錯誤碼;
 九、新起網頁頁籤,而後輸入 http://localhost:8150/springms-consumer-movie/movie/4,正常狀況下不能被代理了,訪問頁面不存在,出現404錯誤碼;

 總結二:第八、9步訪問出現404錯誤碼,說明經過 http://localhost:8150/serviceId/path 代理路徑訪問 API 網關已經失效了;

 十、新起網頁頁籤,而後輸入 http://localhost:8150/user/simple/3,正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來,可見【用戶微服務】的地址被改變生效了,同時被 API 網關反向代理了,也就是說 http 的請求 /user 將被髮送到【用戶微服務】;
 十一、新起網頁頁籤,而後輸入 http://localhost:8150/user/movie/4,正常狀況下訪問不通,理應訪問不通的;

 總結三:zuul.routes ignoredServices 忽略禁用了全部代理路徑,但僅僅只是爲了給 springms-provider-user 微服務添加了 user 前綴供反向代理路徑訪問,因此電影微服務加 user 前綴固然訪問不通的;
 ****************************************************************************************/

/****************************************************************************************
 4、Zuul 服務 API 網關微服務之代理與反向代理(自定義路徑配置,給 springms-provider-user 微服務添加前綴地址,代理、反向代理用戶微服務,忽略禁用 springms-consumer-movie 代理、反向代理路徑):

 一、編寫 application.yml 文件,添加應用程序的註解 EnableZuulProxy 配置;
     # 測試四,自定義路徑配置,給 springms-provider-user 微服務添加前綴地址,代理、反向代理用戶微服務,忽略禁用 springms-consumer-movie 代理、反向代理路徑
     zuul:
         ignoredServices: springms-consumer-movie
         routes:
            springms-provider-user: /user/**
 二、啓動 springms-discovery-eureka 模塊服務,啓動1個端口;
 三、啓動 springms-provider-user 模塊服務,啓動1個端口(application.yml 文件中的 appname 屬性不去掉的話,測試一是沒法測試經過的);
 四、啓動 springms-consumer-movie 模塊服務,啓動1個端口;
 五、啓動 springms-gateway-zuul 模塊服務;

 六、新起網頁頁籤,輸入 http://localhost:7900/simple/3 正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;
 七、新起網頁頁籤,輸入 http://localhost:7901/movie/3 正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來;

 總結一:第六、7步正常,說明 springms-provider-user、springms-consumer-movie 兩個服務目前正常;

 八、新起網頁頁籤,而後輸入 http://localhost:8150/springms-provider-user/simple/3,正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來,可見【用戶微服務】的地址被改變生效了,同時被 API 網關反向代理了,也就是說 http 的請求 /user 將被髮送到【用戶微服務】;
 九、新起網頁頁籤,而後輸入 http://localhost:8150/springms-consumer-movie/movie/4,正常狀況下不能被代理了,訪問頁面不存在,出現404錯誤碼;

 總結二:zuul.routes ignoredServices 忽略禁用了 springms-consumer-movie 【電影微服務】的代理路徑,因此電影微服務的代理路徑固然訪問不通的;

 十、新起網頁頁籤,而後輸入 http://localhost:8150/user/simple/3,正常狀況下是能看到 ID != 0 一堆用戶信息被打印出來,可見【用戶微服務】的地址被改變生效了,同時被 API 網關反向代理了,也就是說 http 的請求 /user 將被髮送到【用戶微服務】;
 十一、新起網頁頁籤,而後輸入 http://localhost:8150/user/movie/4,正常狀況下訪問不通,理應訪問不通的;

 總結三:zuul.routes ignoredServices 忽略禁用了 springms-consumer-movie 【電影微服務】的代理路徑,因此電影微服務的代理路徑固然訪問不通的;

 注意:測試3、測試四的區別在於,ignoredServices 屬性的設置,影響的是 springms-consumer-movie 微服務的代理路徑是否能夠訪問;
 ****************************************************************************************/

4、下載地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.gitnginx

SpringCloudTutorial交流QQ羣: 235322432git

SpringCloudTutorial交流微信羣: 微信溝通羣二維碼圖片連接github

歡迎關注,您的確定是對我最大的支持!!!spring

相關文章
相關標籤/搜索