SpringCloud Zuul網(wǎng)關(guān)功能實現(xiàn)解析
簡介
API Gateway,時系統(tǒng)的唯一對外的入口,介于客戶端和服務(wù)端之間的中間層,處理非業(yè)務(wù)功能,
提供路由請求,鑒權(quán),監(jiān)控,緩存,限流等功能
- 統(tǒng)一接入
- 智能路由
- AB測試、灰度測試
- 負(fù)載均衡、容災(zāi)處理
- 日志埋點(diǎn)(類似 Nignx日志)
- 流量監(jiān)控
- 限流處理
- 服務(wù)降級
- 安全防護(hù)
- 鑒權(quán)處理
- 監(jiān)控
- 機(jī)器網(wǎng)終隔離
1.添加依賴
注意SpringBoot和SpringCloud版本兼容
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency>
2.添加啟動類注解@EnableZuulProxy
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class ZuulgatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulgatewayApplication.class, args);
}
}
3.修改application.yml配置
默認(rèn)訪問規(guī)則
http://gateway:port/service-id/**
server: port: 9000 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ spring: application: name: zuul-gateway #自定義路由映射 #order-service是訂單服務(wù)的名稱,訪問路徑為 #舊: http://localhost:9000/order-serice/api/v1/order/find #新: http://localhost:9000/apigateway/order/api/v1/order/find zuul: routes: #方法一: # product-service: /apigateway/product/** # order-service: /apigateway/order/** #方法二: product-route: #路由名稱,可以任意取 service-id: product-service path: /apigateway/product/** order-route: service-id: order-service path: /apigateway/order/** #忽略整個服務(wù),不對外提供接口 #多個服務(wù)用逗號隔開product-service,order-service #即不能用http://localhost:9000/order-serice/api/v1/order/find方式訪問 # ignored-services: product-service #正則表達(dá)式忽略多個服務(wù) ignored-patterns: /*-service/** sensitive-headers: #zuul使用Ribbon負(fù)載均衡,所以要配置ribbon超時時間,否則很短 host: connect-timeout-millis: 15000 #HTTP連接超時要比Hystrix的大 socket-timeout-millis: 60000 #socket超時 ribbon: ReadTimeout: 10000 ConnectTimeout: 10000
4.Zuul網(wǎng)關(guān)注意事項
默認(rèn)情況,請求頭header不會傳遞Cookie,Set-Cookie,Authorization信息,這些信息會顯示為空
如果需要傳遞,則修改application.yml配置
zuul: sensitive-headers:
5.訪問路徑
http://127.0.0.1:9000/apigateway/product/api/v1/product/find?id=1
http://127.0.0.1:9000/apigateway/order/api/v1/order/test?product_id=1
圖1

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解讀System.getProperty("ENM_HOME")中的值從哪獲取的
這篇文章主要介紹了解讀System.getProperty("ENM_HOME")中的值從哪獲取的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Java中的@RequiredArgsConstructor注解詳解
這篇文章主要介紹了Java中的@RequiredArgsConstructor注解詳解,@RequiredArgsConstructor是Lombok的一個注解,簡化了我們對@Autowired書寫,@RequiredArgsConstructor注解可以代替@Autowired注解,需要的朋友可以參考下2024-01-01
基于java中byte數(shù)組與int類型的轉(zhuǎn)換(兩種方法)
下面小編就為大家?guī)硪黄趈ava中byte數(shù)組與int類型的轉(zhuǎn)換(兩種方法)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
ServletContext讀取web資源_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了ServletContext讀取web資源,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
基于JSON實現(xiàn)傳輸byte數(shù)組過程解析
這篇文章主要介紹了基于JSON實現(xiàn)傳輸byte數(shù)組過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06

