SpringCloud Zuul和Gateway的實(shí)例代碼(搭建方式)
一、 Spring Cloud Zuul和Spring Cloud Gateway是什么
Spring Cloud Zuul和Spring Cloud Gateway都是Spring Cloud框架提供的用于構(gòu)建微服務(wù)架構(gòu)中的API網(wǎng)關(guān)的組件。
- Spring Cloud Zuul:Spring Cloud Zuul是基于Netflix Zuul構(gòu)建的微服務(wù)網(wǎng)關(guān)組件。它提供了路由、負(fù)載均衡、容錯、安全性等功能。Zuul使用同步阻塞模型,適用于較小規(guī)模的微服務(wù)架構(gòu)。然而,需要注意的是,Spring Cloud Zuul目前已經(jīng)進(jìn)入維護(hù)模式,Spring Cloud官方推薦使用Spring Cloud Gateway作為替代方案。
- Spring Cloud Gateway:Spring Cloud Gateway是Spring Cloud官方推薦的API網(wǎng)關(guān)解決方案。它基于Spring Framework 5和Project Reactor構(gòu)建,并采用異步非阻塞模型,具有更高的性能和吞吐量。Spring Cloud Gateway提供了動態(tài)路由、過濾器鏈、集成服務(wù)發(fā)現(xiàn)、斷路器等功能特性,同時支持Java和函數(shù)式編程的API,具有更高級的定制和擴(kuò)展能力。
這兩個組件都可以用于構(gòu)建微服務(wù)架構(gòu)中的API網(wǎng)關(guān),它們的選擇取決于具體的需求和場景。
如果需要更高的性能、更靈活的定制能力以及與Spring生態(tài)系統(tǒng)更好的集成,推薦使用Spring Cloud Gateway。
如果已經(jīng)在使用Netflix的生態(tài)系統(tǒng)組件,并且對性能要求不是特別高,可以考慮使用Spring Cloud Zuul。
二、Spring Cloud Zuul的簡單示例
- 創(chuàng)建一個Spring Boot項目并添加依賴:在你的項目中創(chuàng)建一個新的Spring Boot應(yīng)用程序,并添加以下依賴。如果你使用的是Maven,可以在pom.xml文件中添加以下依賴:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> </dependencies>
- 啟用Zuul網(wǎng)關(guān):在你的Spring Boot應(yīng)用程序的主類上添加@EnableZuulProxy注解,以啟用Zuul網(wǎng)關(guān)。例如:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } }
- 配置Zuul路由規(guī)則:在application.properties或application.yml文件中配置Zuul的路由規(guī)則。例如,以下是一個將/api/**路徑下的請求轉(zhuǎn)發(fā)到http://example.com/api/**的示例配置:
zuul: routes: example-service: path: /api/** url: http://example.com/api
在上面的示例中,example-service是一個自定義的路由名稱,path指定了匹配的路徑模式,url指定了要轉(zhuǎn)發(fā)到的目標(biāo)URL。
- 運(yùn)行應(yīng)用程序并訪問Zuul網(wǎng)關(guān):啟動你的應(yīng)用程序,并使用Zuul網(wǎng)關(guān)轉(zhuǎn)發(fā)請求。例如,如果你的應(yīng)用程序運(yùn)行在http://localhost:8080,你可以發(fā)送一個請求到http://localhost:8080/api/example,該請求將被Zuul網(wǎng)關(guān)轉(zhuǎn)發(fā)到http://example.com/api/example。
這就是一個使用Zuul網(wǎng)關(guān)的簡單示例。
通過配置不同的路由規(guī)則,你可以實(shí)現(xiàn)請求的轉(zhuǎn)發(fā)、負(fù)載均衡、路由過濾等功能。
你還可以添加自定義的過濾器來對請求和響應(yīng)進(jìn)行處理。
請注意,Zuul目前已被Spring Cloud宣布為不推薦使用,建議使用Spring Cloud Gateway作為替代方案。
三、Spring Cloud Gateway(推薦使用)的簡單示例
- 創(chuàng)建一個Spring Boot項目并添加依賴:在你的項目中創(chuàng)建一個新的Spring Boot應(yīng)用程序,并添加以下依賴。如果你使用的是Maven,可以在pom.xml文件中添加以下依賴:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> </dependencies>
- 配置Spring Cloud Gateway路由規(guī)則:在application.properties或application.yml文件中配置Spring Cloud Gateway的路由規(guī)則。例如,以下是一個將/api/**路徑下的請求轉(zhuǎn)發(fā)到http://example.com/api/**的示例配置:
spring: cloud: gateway: routes: - id: example-service uri: http://example.com/api predicates: - Path=/api/**
在上面的示例中,example-service是一個自定義的路由ID,uri指定了要轉(zhuǎn)發(fā)到的目標(biāo)URL,predicates指定了匹配的路徑模式。
- 運(yùn)行應(yīng)用程序并訪問Spring Cloud Gateway:啟動你的應(yīng)用程序,并使用Spring Cloud Gateway轉(zhuǎn)發(fā)請求。例如,如果你的應(yīng)用程序運(yùn)行在http://localhost:8080,你可以發(fā)送一個請求到http://localhost:8080/api/example,該請求將被Spring Cloud Gateway轉(zhuǎn)發(fā)到http://example.com/api/example。
- 添加自定義過濾器(可選):你可以添加自定義的過濾器來對請求和響應(yīng)進(jìn)行處理。例如,你可以創(chuàng)建一個實(shí)現(xiàn)GlobalFilter接口的自定義過濾器類,并在應(yīng)用程序中進(jìn)行注冊。過濾器可以用于鑒權(quán)、日志記錄、請求轉(zhuǎn)換等操作。運(yùn)行應(yīng)用程序并訪問Zuul網(wǎng)關(guān):啟動你的應(yīng)用程序,并使用Zuul網(wǎng)關(guān)轉(zhuǎn)發(fā)請求。例如,如果你的應(yīng)用程序運(yùn)行在http://localhost:8080,你可以發(fā)送一個請求到http://localhost:8080/api/example,該請求將被Zuul網(wǎng)關(guān)轉(zhuǎn)發(fā)到http://example.com/api/example。
import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; @Component public class CustomFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { // 在這里編寫你的過濾器邏輯 // 可以通過exchange對象獲取請求和響應(yīng)信息,并進(jìn)行相關(guān)處理 // 示例:檢查請求頭中是否包含特定的認(rèn)證信息 String authHeader = exchange.getRequest().getHeaders().getFirst("Authorization"); if (authHeader == null || authHeader.isEmpty()) { exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED); return exchange.getResponse().setComplete(); } // 繼續(xù)執(zhí)行后續(xù)的過濾器和路由處理 return chain.filter(exchange); } @Override public int getOrder() { // 設(shè)置過濾器的執(zhí)行順序 return Ordered.HIGHEST_PRECEDENCE; } }
在上面的示例中,CustomFilter是一個自定義的過濾器類,實(shí)現(xiàn)了GlobalFilter接口和Ordered接口。你可以在filter方法中編寫自己的過濾器邏輯,并在getOrder方法中設(shè)置過濾器的執(zhí)行順序。
這就是一個使用Spring Cloud Gateway的簡單示例。通過配置路由規(guī)則和添加自定義過濾器,你可以實(shí)現(xiàn)請求的轉(zhuǎn)發(fā)、負(fù)載均衡、路由過濾等功能。Spring Cloud Gateway還提供了許多其他功能,如斷路器、限流、重試等,你可以根據(jù)具體需求進(jìn)行配置和使用。
四、Spring Cloud Gateway負(fù)載均衡的簡單示例
在Spring Cloud Gateway中,你可以使用LoadBalancerClient或DiscoveryClient來實(shí)現(xiàn)負(fù)載均衡。下面是一個使用LoadBalancerClient實(shí)現(xiàn)負(fù)載均衡的示例:
- 添加依賴:在你的Spring Boot項目中添加以下依賴。如果你使用的是Maven,可以在pom.xml文件中添加以下依賴:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> </dependencies>
- 配置路由規(guī)則:在application.properties或application.yml文件中配置Spring Cloud Gateway的路由規(guī)則,指定負(fù)載均衡的目標(biāo)服務(wù)。例如:
spring: cloud: gateway: routes: - id: example-service uri: lb://example-service predicates: - Path=/api/**
在上面的示例中,example-service是一個服務(wù)的名稱,lb://example-service表示通過負(fù)載均衡調(diào)用example-service服務(wù)。
- 創(chuàng)建一個自定義的LoadBalancerClient配置類:創(chuàng)建一個自定義的LoadBalancerClient配置類,用于配置負(fù)載均衡的策略。例如:
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.gateway.config.GatewayLoadBalancerProperties; import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter; import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class LoadBalancerConfig { @Bean public LoadBalancerClientFilter loadBalancerClientFilter(LoadBalancerClient loadBalancerClient, GatewayLoadBalancerProperties loadBalancerProperties) { return new LoadBalancerClientFilter(loadBalancerClient, loadBalancerProperties); } @Bean public LoadBalancerClient loadBalancerClient() { return new MyLoadBalancerClient(); } private static class MyLoadBalancerClient implements LoadBalancerClient { @Override public <T> T execute(String serviceId, LoadBalancerRequest<T> request) { // 在這里實(shí)現(xiàn)你的負(fù)載均衡邏輯 // 可以使用負(fù)載均衡算法選擇目標(biāo)服務(wù)的實(shí)例 // 這里的示例代碼直接返回了固定的目標(biāo)服務(wù)實(shí)例 ServiceInstance serviceInstance = new DefaultServiceInstance(serviceId, "example-host", 8080, false); return request.apply(serviceInstance); } @Override public <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) { return request.apply(serviceInstance); } @Override public URI reconstructURI(ServiceInstance instance, URI original) { return instance.getUri(); } @Override public ServiceInstance choose(String serviceId) { // 在這里實(shí)現(xiàn)你的負(fù)載均衡邏輯 // 可以使用負(fù)載均衡算法選擇目標(biāo)服務(wù)的實(shí)例 // 這里的示例代碼直接返回了固定的目標(biāo)服務(wù)實(shí)例 return new DefaultServiceInstance(serviceId, "example-host", 8080, false); } } }
在上面的示例中,MyLoadBalancerClient是一個自定義的LoadBalancerClient實(shí)現(xiàn),你可以在其中實(shí)現(xiàn)自己的負(fù)載均衡邏輯。示例代碼中直接返回了固定的目標(biāo)服務(wù)實(shí)例,你可以根據(jù)實(shí)際需求選擇合適的負(fù)載均衡算法。
- 運(yùn)行應(yīng)用程序并訪問Spring Cloud Gateway:啟動你的應(yīng)用程序,并使用Spring Cloud Gateway轉(zhuǎn)發(fā)請求。根據(jù)負(fù)載均衡配置,請求將被轉(zhuǎn)發(fā)到目標(biāo)服務(wù)的不同實(shí)例上。
這就是一個使用LoadBalancerClient實(shí)現(xiàn)負(fù)載均衡的示例。
你可以根據(jù)實(shí)際需求在自定義的LoadBalancerClient實(shí)現(xiàn)中選擇合適的負(fù)載均衡算法,并根據(jù)服務(wù)實(shí)例的健康狀態(tài)等信息進(jìn)行動態(tài)調(diào)整。
另外,你也可以使用DiscoveryClient來實(shí)現(xiàn)基于服務(wù)發(fā)現(xiàn)的負(fù)載均衡,它可以與服務(wù)注冊中心(如Eureka、Consul)集成,自動獲取可用的服務(wù)實(shí)例。
五、總結(jié)
本文簡述了Spring Cloud Zuul和Spring Cloud Gateway的簡單示例,其中還有很多功能還是得靠大家自己動手去實(shí)踐。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Spring Cloud服務(wù)入口Gateway的介紹和使用問題小結(jié)
- Spring Boot 3 整合 Spring Cloud Gateway實(shí)踐過程
- spring?cloud?gateway限流常見算法實(shí)現(xiàn)
- SpringCloud的網(wǎng)關(guān)Zuul和Gateway詳解
- 一文掌握spring cloud gateway(總結(jié)篇)
- SpringCloudGateway 網(wǎng)關(guān)登錄校驗實(shí)現(xiàn)思路
- Spring?Cloud?Gateway服務(wù)網(wǎng)關(guān)限流問題及解決
相關(guān)文章
Springboot文件上傳功能的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot文件上傳功能的實(shí)現(xiàn),文中通過代碼示例介紹的非常詳細(xì),具有一定的參考學(xué)習(xí)價值,需要的朋友們可以參考閱讀2023-04-04Mybatis中#{}和${}傳參的區(qū)別及#和$的區(qū)別小結(jié)
這篇文章主要介紹了Mybatis中#{}和${}傳參的區(qū)別及#和$的區(qū)別小結(jié) 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07Struts攔截器實(shí)現(xiàn)攔截未登陸用戶實(shí)例解析
這篇文章主要介紹了Struts攔截器實(shí)現(xiàn)攔截未登陸用戶實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02SpringBoot實(shí)現(xiàn)分布式驗證碼登錄方案小結(jié)
驗證碼登錄作為一種有效的防護(hù)手段,可以防止惡意gongji、暴力pojie等,本文主要介紹了SpringBoot實(shí)現(xiàn)分布式驗證碼登錄方案小結(jié),具有一定的參考價值,感興趣的可以了解一下2024-12-12springboot整合httpClient代碼實(shí)例
這篇文章主要介紹了springboot整合httpClient代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12Hibernate迫切連接和普通連接的區(qū)別實(shí)例詳解
這篇文章主要介紹了Hibernate迫切連接和普通連接的區(qū)別實(shí)例詳解,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12