日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringCloud教程- 路由网关Zuul (SpringCloud版本Greenwich.SR4)

發(fā)布時間:2025/1/21 javascript 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloud教程- 路由网关Zuul (SpringCloud版本Greenwich.SR4) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • Zuul簡介
    • 創(chuàng)建zuul-gateway工程
    • 服務(wù)過濾

代碼地址:github-spring-cloud地址

Zuul簡介

Zuul是NetFlix開源的微服務(wù)網(wǎng)關(guān),它可以和Eureka、Ribbon、Hystrix等組件配合使用。Zuul的核心是一些列的過濾器,這些過濾器可以完成以下功能。

  • 身份認證與安全:識別每個資源的驗證要求,并拒絕那些與要求不符合的請求。
  • 審查與監(jiān)控:在邊緣位置追蹤有意義的數(shù)據(jù)和統(tǒng)計結(jié)果,從而帶來精確的生產(chǎn)視圖。
  • 動態(tài)路由:動態(tài)的將請求路由到不同的后端集群。
  • 壓力測試:逐漸增加指向集群的流量,以了解性能。
  • 負載分配:為每一種負載類型分配對應(yīng)容量,并棄用超出限定值的請求。
  • 靜態(tài)響應(yīng)處理:在邊緣位置直接建立部分響應(yīng),從而避免其轉(zhuǎn)發(fā)到內(nèi)部集群。
  • 多區(qū)域彈性:跨越AWS Region進行請求路由,旨在實現(xiàn)ELB(Elastic Load Balancing)使用的多樣化,以及讓系統(tǒng)的便越更貼近系統(tǒng)的使用者。

SpringCloud對zuul進行了整合和增強,Zuul使用的默認HTTP客戶端是Apache HTTP Client,也可以使用使用其他的。
使用Zuul之后的架構(gòu)如圖所示(圖片轉(zhuǎn)載方志朋博客):

創(chuàng)建zuul-gateway工程

pom文件如下:

<?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"><parent><artifactId>spring-cloud-learn</artifactId><groupId>com.sl.learn.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.sl.learn.cloud</groupId><artifactId>zuul-gateway</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId></dependency></dependencies></project>

配置文件application.yml

server:port: 8089spring:application:name: zuul-gatewayeureka:client:serviceUrl:defaultZone: http://localhost:8080/eureka/management:endpoints:web:exposure:include: '*'endpoint:health:show-details: alwaysshutdown:enabled: true zuul:routes:## 名字任意寫api-feign:path: /api-feign/**## 注冊到eureka IdserviceId: eureka-feignapi-ribbon:path: /api-ribbon/**## 注冊到eureka IdserviceId: server-ribbon

啟動類ZuulGatewayApplication

@SpringBootApplication @EnableZuulProxy @EnableDiscoveryClient public class ZuulGatewayApplication {public static void main(String[] args) {SpringApplication.run(ZuulGatewayApplication.class,args);} }

此工程依賴前面工程分別依次啟動,
訪問路徑為A:http://localhost:8089/api-feign/hi?name=123

訪問路徑為B:http://localhost:8089//api-ribbon/hi?name=123

這說明zuul起到了路由的作用

服務(wù)過濾

自定義MyZuulFilter

@Component public class MyZuulFilter extends ZuulFilter {private static Logger log = LoggerFactory.getLogger(MyZuulFilter.class);@Overridepublic String filterType() {return "pre";}@Overridepublic int filterOrder() {return 0;}@Overridepublic boolean shouldFilter() {return false;}@Overridepublic Object run() throws ZuulException {RequestContext ctx = RequestContext.getCurrentContext();HttpServletRequest request = ctx.getRequest();log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));Object accessToken = request.getParameter("token");if(accessToken == null) {log.warn("token is empty");ctx.setSendZuulResponse(false);ctx.setResponseStatusCode(401);try {ctx.getResponse().getWriter().write("token is empty");}catch (Exception e){}return null;}log.info("ok");return null;} }
  • filterType:返回一個字符串代表過濾器的類型,在zuul中定義了四種不同生命周期的過濾器類型,具體如下:
    pre:路由之前
    routing:路由之時
    post: 路由之后
    error:發(fā)送錯誤調(diào)用
  • filterOrder:過濾的順序
  • shouldFilter:這里可以寫邏輯判斷,是否要過濾,本文true,永遠過濾。
  • run:過濾器的具體邏輯。可用很復(fù)雜,包括查sql,nosql去判斷該請求到底有沒有權(quán)限訪問。
    訪問地址不加token

    訪問地址加上token

總結(jié)

以上是生活随笔為你收集整理的SpringCloud教程- 路由网关Zuul (SpringCloud版本Greenwich.SR4)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。