SpringCloud(第 018 篇)Zuul 服務(wù) API 網(wǎng)關(guān)微服務(wù)之代理與反向代理
一、大致介紹
1麦撵、API 服務(wù)網(wǎng)關(guān)顧名思義就是統(tǒng)一入口溃肪,類似 nginx惫撰、F5 等功能一樣,統(tǒng)一代理控制請求入口扼雏,弱化各個(gè)微服務(wù)被客戶端記憶功能夯膀;
2诱建、本章節(jié)主要講解了使用 zuul 的代理功能與反向代理功能,當(dāng)然 zuul 還有很多屬性設(shè)置茎匠,我就沒一一列舉所有的測試方法了辜荠;
3伯病、http://localhost:8150/routes 地址可以查看該zuul微服務(wù)網(wǎng)關(guān)代理了多少微服務(wù)的serviceId否过;
二苗桂、實(shí)現(xiàn)步驟
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網(wǎng)關(guān)模塊 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!-- 客戶端發(fā)現(xiàn)模塊煤伟,由于文檔說 Zuul 的依賴?yán)锩娌话?eureka 客戶端發(fā)現(xiàn)模塊木缝,所以這里還得單獨(dú)添加進(jìn)來 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>
2.2 添加應(yīng)用配置文件(springms-gateway-zuul\src\main\resources\application.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 微服務(wù)添加前綴地址,反向代理用戶微服務(wù)
#zuul:
# routes:
# springms-provider-user: /user/**
## 測試三友雳,自定義路徑配置押赊,給 springms-provider-user 微服務(wù)添加前綴地址伊群,反向代理用戶微服務(wù)舰始,其它代理路徑一律失效
#zuul:
# ignoredServices: '*'
# routes:
# springms-provider-user: /user/**
# 測試四咽袜,自定義路徑配置,給 springms-provider-user 微服務(wù)添加前綴地址谜嫉,代理沐兰、反向代理用戶微服務(wù)蔽挠,忽略禁用 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
#####################################################################################################
#####################################################################################################
# 解決第一次請求報(bào)超時(shí)異常的方案插佛,因?yàn)?hystrix 的默認(rèn)超時(shí)時(shí)間是 1 秒量窘,因此請求超過該時(shí)間后蚌铜,就會出現(xiàn)頁面超時(shí)顯示 :
#
# 這里就介紹大概三種方式來解決超時(shí)的問題,解決方案如下:
#
# 第一種方式:將 hystrix 的超時(shí)時(shí)間設(shè)置成 5000 毫秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
#
# 或者:
# 第二種方式:將 hystrix 的超時(shí)時(shí)間直接禁用掉识腿,這樣就沒有超時(shí)的一說了渡讼,因?yàn)橛肋h(yuǎn)也不會超時(shí)了
# hystrix.command.default.execution.timeout.enabled: false
#
# 或者:
# 第三種方式:索性禁用feign的hystrix支持
# feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持
# 超時(shí)的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
# 超時(shí)的解決方案: 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服務(wù)網(wǎng)關(guān)微服務(wù)啟動(dòng)類(springms-gateway-zuul\src\main\java\com\springms\cloud\MsGatewayZuulApplication.java)
package com.springms.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
* Zuul 服務(wù) API 網(wǎng)關(guān)微服務(wù)之代理與反向代理成箫。
*
* 注意 EnableZuulProxy 注解能注冊到 eureka 服務(wù)上旨枯,是因?yàn)樵撟⒔獍?eureka 客戶端的注解攀隔,該 EnableZuulProxy 是一個(gè)復(fù)合注解。
*
* @EnableZuulProxy --> { @EnableCircuitBreaker明刷、@EnableDiscoveryClient } 包含了 eureka 客戶端注解满粗,同時(shí)也包含了 Hystrix 斷路器模塊注解映皆。
*
* http://localhost:8150/routes 地址可以查看該zuul微服務(wù)網(wǎng)關(guān)代理了多少微服務(wù)的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微服務(wù) 】】】】】】已啟動(dòng).");
}
}
三组去、測試
/****************************************************************************************
一从隆、Zuul 服務(wù) API 網(wǎng)關(guān)微服務(wù)之代理與反向代理(正常情況測試):
1、編寫 application.yml 文件砾脑,添加應(yīng)用程序的注解 EnableZuulProxy 配置艾杏;
2购桑、啟動(dòng) springms-discovery-eureka 模塊服務(wù),啟動(dòng)1個(gè)端口硕噩;
3炉擅、啟動(dòng) springms-provider-user 模塊服務(wù)阳惹,啟動(dòng)1個(gè)端口(application.yml 文件中的 appname 屬性不去掉的話莹汤,測試一是無法測試通過的);
4抹竹、啟動(dòng) springms-consumer-movie 模塊服務(wù)止潮,啟動(dòng)1個(gè)端口沽翔;
5、啟動(dòng) springms-gateway-zuul 模塊服務(wù);
6橘沥、新起網(wǎng)頁頁簽夯秃,輸入 http://localhost:7900/simple/3 正常情況下是能看到 ID != 0 一堆用戶信息被打印出來;
7堤舒、新起網(wǎng)頁頁簽哺呜,輸入 http://localhost:7901/movie/3 正常情況下是能看到 ID != 0 一堆用戶信息被打印出來某残;
總結(jié)一:第6、7步正常介牙,說明 springms-provider-user澳厢、springms-consumer-movie 兩個(gè)服務(wù)目前正常剩拢;
8、新起網(wǎng)頁頁簽框都,然后輸入 http://localhost:8150/springms-provider-user/simple/3魏保,正常情況下是能看到 ID != 0 一堆用戶信息被打印出來摸屠;
9季二、新起網(wǎng)頁頁簽,然后輸入 http://localhost:8150/springms-consumer-movie/movie/4刻蚯,正常情況下是能看到 ID != 0 一堆用戶信息被打印出來炊汹;
總結(jié)二:第8逃顶、9步也能正常打印用戶信息,說明 API 網(wǎng)關(guān)已經(jīng)生效了霸褒,可以通過API服務(wù)器地址鏈接各個(gè)微服務(wù)的 http://localhost:8150/serviceId/path 這樣的路徑來訪問了废菱;
****************************************************************************************/
/****************************************************************************************
二昙啄、Zuul 服務(wù) API 網(wǎng)關(guān)微服務(wù)之代理與反向代理(自定義路徑配置,給 springms-provider-user 微服務(wù)添加前綴地址耿币,反向代理用戶微服務(wù)):
1淹接、編寫 application.yml 文件叛溢,添加應(yīng)用程序的注解 EnableZuulProxy 配置楷掉;
# 測試二,自定義路徑配置斑鸦,給 springms-provider-user 微服務(wù)添加前綴地址,反向代理用戶微服務(wù)
zuul:
routes:
springms-provider-user: /user/**
2草雕、啟動(dòng) springms-discovery-eureka 模塊服務(wù)巷屿,啟動(dòng)1個(gè)端口;
3墩虹、啟動(dòng) springms-provider-user 模塊服務(wù)嘱巾,啟動(dòng)1個(gè)端口(application.yml 文件中的 appname 屬性不去掉的話,測試一是無法測試通過的)诫钓;
4旬昭、啟動(dòng) springms-consumer-movie 模塊服務(wù),啟動(dòng)1個(gè)端口菌湃;
5、啟動(dòng) springms-gateway-zuul 模塊服務(wù)慢味;
6场梆、新起網(wǎng)頁頁簽,輸入 http://localhost:7900/simple/3 正常情況下是能看到 ID != 0 一堆用戶信息被打印出來纯路;
7或油、新起網(wǎng)頁頁簽,輸入 http://localhost:7901/movie/3 正常情況下是能看到 ID != 0 一堆用戶信息被打印出來驰唬;
總結(jié)一:第6顶岸、7步正常,說明 springms-provider-user叫编、springms-consumer-movie 兩個(gè)服務(wù)目前正常辖佣;
8、新起網(wǎng)頁頁簽搓逾,然后輸入 http://localhost:8150/springms-provider-user/simple/3卷谈,正常情況下是能看到 ID != 0 一堆用戶信息被打印出來;
9霞篡、新起網(wǎng)頁頁簽世蔗,然后輸入 http://localhost:8150/springms-consumer-movie/movie/4,正常情況下是能看到 ID != 0 一堆用戶信息被打印出來朗兵;
總結(jié)二:第8污淋、9步也能正常打印用戶信息,說明 API 網(wǎng)關(guān)已經(jīng)生效了余掖,可以通過API服務(wù)器地址鏈接各個(gè)微服務(wù)的 http://localhost:8150/serviceId/path 這樣的路徑來訪問了寸爆;
10、新起網(wǎng)頁頁簽盐欺,然后輸入 http://localhost:8150/user/simple/3赁豆,正常情況下是能看到 ID != 0 一堆用戶信息被打印出來,可見【用戶微服務(wù)】的地址被改變生效了找田,同時(shí)被 API 網(wǎng)關(guān)反向代理了歌憨,也就是說 http 的請求 /user 將被發(fā)送到【用戶微服務(wù)】;
11墩衙、新起網(wǎng)頁頁簽务嫡,然后輸入 http://localhost:8150/user/movie/4,正常情況下訪問不通漆改,理應(yīng)訪問不通的心铃;
總結(jié)三:zuul.routes 屬性僅僅只是為了給 springms-provider-user 微服務(wù)添加了 user 前綴,所以電影微服務(wù)加 user 前綴當(dāng)然訪問不通的挫剑;
****************************************************************************************/
/****************************************************************************************
三去扣、Zuul 服務(wù) API 網(wǎng)關(guān)微服務(wù)之代理與反向代理(自定義路徑配置,給 springms-provider-user 微服務(wù)添加前綴地址樊破,反向代理用戶微服務(wù)愉棱,其它代理路徑一律失效):
1唆铐、編寫 application.yml 文件,添加應(yīng)用程序的注解 EnableZuulProxy 配置奔滑;
# 測試三艾岂,自定義路徑配置,給User微服務(wù)添加前綴地址朋其,反向代理User微服務(wù)王浴,不反向代理電影微服務(wù)
zuul:
ignoredServices: '*'
routes:
springms-provider-user: /user/**
2、啟動(dòng) springms-discovery-eureka 模塊服務(wù)梅猿,啟動(dòng)1個(gè)端口氓辣;
3、啟動(dòng) springms-provider-user 模塊服務(wù)袱蚓,啟動(dòng)1個(gè)端口(application.yml 文件中的 appname 屬性不去掉的話钞啸,測試一是無法測試通過的);
4癞松、啟動(dòng) springms-consumer-movie 模塊服務(wù)爽撒,啟動(dòng)1個(gè)端口;
5响蓉、啟動(dòng) springms-gateway-zuul 模塊服務(wù)硕勿;
6、新起網(wǎng)頁頁簽枫甲,輸入 http://localhost:7900/simple/3 正常情況下是能看到 ID != 0 一堆用戶信息被打印出來源武;
7、新起網(wǎng)頁頁簽想幻,輸入 http://localhost:7901/movie/3 正常情況下是能看到 ID != 0 一堆用戶信息被打印出來粱栖;
總結(jié)一:第6、7步正常脏毯,說明 springms-provider-user闹究、springms-consumer-movie 兩個(gè)服務(wù)目前正常;
8食店、新起網(wǎng)頁頁簽渣淤,然后輸入 http://localhost:8150/springms-provider-user/simple/3,正常情況下不能被代理了吉嫩,訪問頁面不存在价认,出現(xiàn)404錯(cuò)誤碼;
9自娩、新起網(wǎng)頁頁簽用踩,然后輸入 http://localhost:8150/springms-consumer-movie/movie/4,正常情況下不能被代理了,訪問頁面不存在脐彩,出現(xiàn)404錯(cuò)誤碼碎乃;
總結(jié)二:第8、9步訪問出現(xiàn)404錯(cuò)誤碼丁屎,說明通過 http://localhost:8150/serviceId/path 代理路徑訪問 API 網(wǎng)關(guān)已經(jīng)失效了荠锭;
10、新起網(wǎng)頁頁簽晨川,然后輸入 http://localhost:8150/user/simple/3,正常情況下是能看到 ID != 0 一堆用戶信息被打印出來删豺,可見【用戶微服務(wù)】的地址被改變生效了共虑,同時(shí)被 API 網(wǎng)關(guān)反向代理了,也就是說 http 的請求 /user 將被發(fā)送到【用戶微服務(wù)】呀页;
11妈拌、新起網(wǎng)頁頁簽,然后輸入 http://localhost:8150/user/movie/4蓬蝶,正常情況下訪問不通尘分,理應(yīng)訪問不通的;
總結(jié)三:zuul.routes ignoredServices 忽略禁用了所有代理路徑丸氛,但僅僅只是為了給 springms-provider-user 微服務(wù)添加了 user 前綴供反向代理路徑訪問培愁,所以電影微服務(wù)加 user 前綴當(dāng)然訪問不通的;
****************************************************************************************/
/****************************************************************************************
四缓窜、Zuul 服務(wù) API 網(wǎng)關(guān)微服務(wù)之代理與反向代理(自定義路徑配置定续,給 springms-provider-user 微服務(wù)添加前綴地址,代理禾锤、反向代理用戶微服務(wù)私股,忽略禁用 springms-consumer-movie 代理、反向代理路徑):
1恩掷、編寫 application.yml 文件倡鲸,添加應(yīng)用程序的注解 EnableZuulProxy 配置;
# 測試四黄娘,自定義路徑配置峭状,給 springms-provider-user 微服務(wù)添加前綴地址,代理寸宏、反向代理用戶微服務(wù)宁炫,忽略禁用 springms-consumer-movie 代理、反向代理路徑
zuul:
ignoredServices: springms-consumer-movie
routes:
springms-provider-user: /user/**
2氮凝、啟動(dòng) springms-discovery-eureka 模塊服務(wù)羔巢,啟動(dòng)1個(gè)端口;
3、啟動(dòng) springms-provider-user 模塊服務(wù)竿秆,啟動(dòng)1個(gè)端口(application.yml 文件中的 appname 屬性不去掉的話启摄,測試一是無法測試通過的);
4幽钢、啟動(dòng) springms-consumer-movie 模塊服務(wù)歉备,啟動(dòng)1個(gè)端口;
5匪燕、啟動(dòng) springms-gateway-zuul 模塊服務(wù)蕾羊;
6、新起網(wǎng)頁頁簽帽驯,輸入 http://localhost:7900/simple/3 正常情況下是能看到 ID != 0 一堆用戶信息被打印出來龟再;
7、新起網(wǎng)頁頁簽尼变,輸入 http://localhost:7901/movie/3 正常情況下是能看到 ID != 0 一堆用戶信息被打印出來利凑;
總結(jié)一:第6、7步正常嫌术,說明 springms-provider-user哀澈、springms-consumer-movie 兩個(gè)服務(wù)目前正常;
8度气、新起網(wǎng)頁頁簽割按,然后輸入 http://localhost:8150/springms-provider-user/simple/3,正常情況下是能看到 ID != 0 一堆用戶信息被打印出來蚯嫌,可見【用戶微服務(wù)】的地址被改變生效了哲虾,同時(shí)被 API 網(wǎng)關(guān)反向代理了,也就是說 http 的請求 /user 將被發(fā)送到【用戶微服務(wù)】择示;
9束凑、新起網(wǎng)頁頁簽,然后輸入 http://localhost:8150/springms-consumer-movie/movie/4栅盲,正常情況下不能被代理了汪诉,訪問頁面不存在,出現(xiàn)404錯(cuò)誤碼谈秫;
總結(jié)二:zuul.routes ignoredServices 忽略禁用了 springms-consumer-movie 【電影微服務(wù)】的代理路徑扒寄,所以電影微服務(wù)的代理路徑當(dāng)然訪問不通的;
10拟烫、新起網(wǎng)頁頁簽该编,然后輸入 http://localhost:8150/user/simple/3,正常情況下是能看到 ID != 0 一堆用戶信息被打印出來硕淑,可見【用戶微服務(wù)】的地址被改變生效了课竣,同時(shí)被 API 網(wǎng)關(guān)反向代理了嘉赎,也就是說 http 的請求 /user 將被發(fā)送到【用戶微服務(wù)】;
11于樟、新起網(wǎng)頁頁簽公条,然后輸入 http://localhost:8150/user/movie/4,正常情況下訪問不通迂曲,理應(yīng)訪問不通的靶橱;
總結(jié)三:zuul.routes ignoredServices 忽略禁用了 springms-consumer-movie 【電影微服務(wù)】的代理路徑,所以電影微服務(wù)的代理路徑當(dāng)然訪問不通的路捧;
注意:測試三关霸、測試四的區(qū)別在于,ignoredServices 屬性的設(shè)置鬓长,影響的是 springms-consumer-movie 微服務(wù)的代理路徑是否可以訪問谒拴;
****************************************************************************************/
四、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關(guān)注涉波,您的肯定是對我最大的支持!!!