Spring Cloud Gateway替代zuul作為API網(wǎng)關(guān)的方法
本文簡要介紹如何使用Spring Cloud Gateway 作為API 網(wǎng)關(guān)(不是使用zuul作為網(wǎng)關(guān)),關(guān)于Spring Cloud Gateway和zuul的性能比較本文不再贅述,基本可以肯定Spring Cloud Finchley版本的gateway比zuul 1.x系列的性能和功能整體要好。
特別提醒:Spring Cloud Finchley版本中,即使你引入了spring-cloud-starter-netflix-zuul,也不是2.0版本的zuul
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
我們的介紹分為3個(gè)部分, 第一,pom文件,第二項(xiàng)目的基本架構(gòu),第三源碼和截圖。
第一,pom文件
因?yàn)槭褂肊ureka作為服務(wù)注冊(cè)和發(fā)現(xiàn),因此在pom中引入了eureka,各位可根據(jù)自己的實(shí)際情況修改。
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.yq</groupId> <artifactId>GatewayDemo</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <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-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-hystrix</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.33</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.5</version> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第二,項(xiàng)目結(jié)構(gòu)
總共有3個(gè)項(xiàng)目,
第一個(gè)項(xiàng)目是eureka 注冊(cè)中心,非常簡單,基本就一個(gè)Application類。 端口7700
第二個(gè)項(xiàng)目是User service,也非常簡單提供兩個(gè)rest api,為了簡略不連接數(shù)據(jù)庫,直接在內(nèi)存中生成一組數(shù)據(jù)。端口6601
第三個(gè)項(xiàng)目就是我們的網(wǎng)關(guān)。端口6604
目前項(xiàng)目中集成websocket服務(wù)配置,本文暫不介紹可直接忽略。
第三,項(xiàng)目代碼和運(yùn)行截圖
網(wǎng)關(guān)的主代碼
package com.yq; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; @SpringCloudApplication public class APIGatewayApplication { private static final Logger logger = LoggerFactory.getLogger(APIGatewayApplication.class); @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route(r -> r.path("/baidu") .uri("http://baidu.com:80/") ) .route("websocket_route", r -> r.path("/apitopic1/**") .uri("ws://127.0.0.1:6605")) .route(r -> r.path("/userapi3/**") .filters(f -> f.addResponseHeader("X-AnotherHeader", "testapi3")) .uri("lb://user-service/") ) .build(); } public static void main(String[] args) { SpringApplication.run(APIGatewayApplication.class, args); logger.info(" Start APIGatewayApplication Done"); } }
網(wǎng)關(guān)的配置文件application.yml
server: port: 6604 #服務(wù)名 spring: application: name: gateway-service cloud: gateway: filter: remove-non-proxy-headers: headers: - dummy routes: - id: apiuser # 重點(diǎn)!/info必須使用http進(jìn)行轉(zhuǎn)發(fā),lb代表從注冊(cè)中心獲取服務(wù) uri: lb://user-service predicates: # 重點(diǎn)!轉(zhuǎn)發(fā)該路徑!,/userapi/**, - Path=/userapi/** # http://localhost:6601/userapi/user/users/2, 必須加上StripPrefix=1,否則訪問服務(wù)時(shí)會(huì)帶上userapi #而不是我們期望的去掉userapi,只保留**部分 filters: - StripPrefix=1 - id: api2user uri: lb://user-service predicates: - Path=/userapi2/** filters: - StripPrefix=1 eureka: client: serviceUrl: defaultZone: http://localhost:7700/eureka/
我們簡要分析分析一下配置文件
- id: apiuser
# 重點(diǎn)!/info必須使用http進(jìn)行轉(zhuǎn)發(fā),lb代表從注冊(cè)中心獲取服務(wù)
uri: lb://user-service
predicates:
# 重點(diǎn)!轉(zhuǎn)發(fā)該路徑!,/userapi/,
- Path=/userapi/
# http://localhost:6601/userapi/user/users/2, 必須加上StripPrefix=1,否則訪問服務(wù)時(shí)會(huì)帶上userapi
#而不是我們期望的去掉userapi,只保留**部分
filters:
- StripPrefix=1
配置了一個(gè)路由apiuser, 當(dāng)路徑( - Path=/userapi/**),就轉(zhuǎn)發(fā)到服務(wù)(lb://user-service),同時(shí)把路徑中的userapi這部分去掉(- StripPrefix=1)。
運(yùn)行效果圖
直接訪問User service
http://localhost:6601/user/users/2
通過網(wǎng)關(guān)訪問user service
http://localhost:6604/userapi/user/users/2
參考文檔:
1, http://cloud.spring.io/spring-cloud-static/Finchley/single/spring-cloud.html#_spring_cloud_gateway
2, https://github.com/spring-cloud-samples/spring-cloud-gateway-sample
到此這篇關(guān)于Spring Cloud Gateway替代zuul作為API網(wǎng)關(guān)的文章就介紹到這了,更多相關(guān)Spring Cloud Gateway替代zuul作為API網(wǎng)關(guān)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
設(shè)置Myeclipse中的代碼格式化、注釋模板及保存時(shí)自動(dòng)格式化
這篇文章主要介紹了設(shè)置Myeclipse中的代碼格式化、注釋模板及保存時(shí)自動(dòng)格式化方法,需要的朋友可以參考下2014-10-10SpringBoot配置MyBatis-Plus實(shí)現(xiàn)增刪查改
本文主要介紹了SpringBoot配置MyBatis-Plus實(shí)現(xiàn)增刪查改,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08教你通過B+Tree平衡多叉樹理解InnoDB引擎的聚集和非聚集索引
大家都知道B+Tree是從二叉樹演化而來,在這之前我們來先了解二叉樹、平衡二叉樹、平衡多叉樹,這篇文章主要介紹了通過B+Tree平衡多叉樹理解InnoDB引擎的聚集和非聚集索引,需要的朋友可以參考下2022-01-01SpringCloud+SpringBoot項(xiàng)目搭建結(jié)構(gòu)層次的實(shí)例
這篇文章詳細(xì)介紹了SpringCloud項(xiàng)目的架構(gòu)層次及其搭建經(jīng)驗(yàn),包括Controller層、Service層、Repository層、Entity層、DTO層、Exception層等,通過文字和圖片的形式,幫助讀者理解如何組織和實(shí)現(xiàn)一個(gè)SpringBoot項(xiàng)目的不同層次2025-01-01Springboot Redis?哨兵模式的實(shí)現(xiàn)示例
本文主要介紹了Springboot Redis?哨兵模式的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01使用Java計(jì)算集合中的組內(nèi)平均值的代碼實(shí)現(xiàn)
在Java開發(fā)中,集合(Collection)是一個(gè)重要的數(shù)據(jù)結(jié)構(gòu),廣泛應(yīng)用于各種場(chǎng)景,計(jì)算集合中的組內(nèi)平均值是一個(gè)常見的操作,本文將深入探討如何使用Java來計(jì)算集合中的組內(nèi)平均值,涵蓋基本概念、具體實(shí)現(xiàn)、優(yōu)化策略和實(shí)用示例,需要的朋友可以參考下2024-06-06Java concurrency之共享鎖和ReentrantReadWriteLock_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
本篇文章主要介紹了Java concurrency之共享鎖和ReentrantReadWriteLock,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-06-06