今天就跟大家聊聊有關怎么在SpringCloud中利用Zuul實現一個網關功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
API Gateway,時系統的唯一對外的入口,介于客戶端和服務端之間的中間層,處理非業務功能,
提供路由請求,鑒權,監控,緩存,限流等功能
統一接入
智能路由
AB測試、灰度測試
負載均衡、容災處理
日志埋點(類似 Nignx日志)
流量監控
限流處理
服務降級
安全防護
鑒權處理
監控
機器網終隔離
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配置
默認訪問規則
http://gateway:port/service-id/**
server: port: 9000 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ spring: application: name: zuul-gateway #自定義路由映射 #order-service是訂單服務的名稱,訪問路徑為 #舊: 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/** #忽略整個服務,不對外提供接口 #多個服務用逗號隔開product-service,order-service #即不能用http://localhost:9000/order-serice/api/v1/order/find方式訪問 # ignored-services: product-service #正則表達式忽略多個服務 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網關注意事項
默認情況,請求頭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
看完上述內容,你們對怎么在SpringCloud中利用Zuul實現一個網關功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。