SpringCloud Zuul網(wǎng)關(guān)功能實現(xiàn)解析
簡介
API Gateway,時系統(tǒng)的唯一對外的入口,介于客戶端和服務(wù)端之間的中間層,處理非業(yè)務(wù)功能,
提供路由請求,鑒權(quán),監(jiān)控,緩存,限流等功能
- 統(tǒng)一接入
- 智能路由
- AB測試、灰度測試
- 負載均衡、容災(zāi)處理
- 日志埋點(類似 Nignx日志)
- 流量監(jiān)控
- 限流處理
- 服務(wù)降級
- 安全防護
- 鑒權(quán)處理
- 監(jiān)控
- 機器網(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配置
默認訪問規(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 #正則表達式忽略多個服務(wù) ignored-patterns: /*-service/** sensitive-headers: #zuul使用Ribbon負載均衡,所以要配置ribbon超時時間,否則很短 host: connect-timeout-millis: 15000 #HTTP連接超時要比Hystrix的大 socket-timeout-millis: 60000 #socket超時 ribbon: ReadTimeout: 10000 ConnectTimeout: 10000
4.Zuul網(wǎng)關(guā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)文章
IDEA編譯報錯:Error:(2048,1024) java: 找不到符號的解決方案
在使用 Lombok 的過程中,你是否曾遇到過 IDEA 編譯報錯 Error:(2048,1024) java: 找不到符號?下面就讓我們來深入剖析這一問題的根源,并給出相應(yīng)的解決方案,需要的朋友可以參考下2025-02-02
Spring Security內(nèi)存中認證的實現(xiàn)
本文主要介紹了Spring Security內(nèi)存中認證的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
Java求s=a+aa+aaa+aaaa+aa...a 5個數(shù)相加的值
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數(shù)字。例如2+22+222+2222+22222(此時共有5個數(shù)相加),幾個數(shù)相加有鍵盤控制2017-02-02
Java編程技巧:if-else優(yōu)化實踐總結(jié)歸納
這篇文章主要介紹了Java中避免過多if-else的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2021-06-06

