Spring Cloud AliBaba 開發教程:整合Spring Cloud Gateway

關注程序猿的故事,一塊兒用技術改變世界java

Gateway介紹
spring

Spring Cloud Gateway 是 Spring Cloud 的一個全新項目,該項目是基於Netty、Reactor以及WEbFlux構建,它旨在爲微服務架構提供一種簡單有效的統一的 API 路由管理方式。
Spring Cloud Gateway 做爲 Spring Cloud 生態系統中的網關,目標是替代 Netflix Zuul,其不只提供統一的路由方式,而且基於 Filter 鏈的方式提供了網關基本的功能,例如:安全、監控、埋點和限流等。
apache

優勢

  • 性能強勁,是Zuul的1.6倍編程

  • 功能強大,內置了不少實用的功能,例如轉發、監控、限流等bootstrap

  • 設計優雅,容易擴展瀏覽器

缺點

  • 依賴Netty與WebFlux,不是傳統的Servlet編程模型,有必定的學習成本安全

  • 不能在Servlet容器下工做,也不能構建成WAR包,即不能將其部署在Tomcat、Jetty等Servlet容器裏,只能打成jar包執行微信

  • 不支持Spring Boot 1.x,需2.0及更高的版本架構

Zuul與Gateway有哪些區別app

  • Zuul網關屬於netfix公司開源的產品屬於第一代微服務網關

  • Gateway屬於SpringCloud自研發的第二代微服務網關

  • 相比來講SpringCloudGateway性能比Zuul性能要好:

  • 注意:Zuul基於Servlet實現的,阻塞式的Api, 不支持長鏈接。

Gateway教程

第一步:

搭建測試項目alibaba-nacos-gateway,引入nacos與gateway依賴,pom.xml配置以下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.test</groupId> <artifactId>gateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gateway</name> <description>Demo project for Spring Boot</description>
<properties> <java.version>1.8</java.version> <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 整合spring-cloud-alibaba--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>

第二步:

修改項目啓動類,添加註解@EnableDiscoveryClient,可以讓註冊中心可以發現,掃描到改服務,代碼以下:

package com.test.gateway;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication@EnableDiscoveryClientpublic class GatewayApplication {
public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); }
}

第三步:

修改bootstrap.yml配置文件,配置nacos與gateway服務信息,配置以下:

server: port: 8905spring: application: name: alibaba-nacos-gateway cloud: nacos: discovery: server-addr: 127.0.0.1:8848 namespace: 0df79d5a-2432-4840-a254-e946a943a6c9 gateway: routes: - id: alibaba-nacos-server #使用alibaba-nacos-server提供服務 uri: lb://alibaba-nacos-server #目標服務在註冊中心的服務名 predicates: #主要的目的是能夠路由到以server開頭的全部接口方法 - Path=/server/** #匹配轉發路徑 filters: - StripPrefix=1 #從第幾級開始轉發

第四步:

以上就是gateway整合過程,接下來啓動alibaba-nacos-gateway與alibaba-nacos-server服務。

第五步:

啓動本地nacos服務,mac電腦啓動命令:sudo sh startup.sh -m standalone,啓動成功後打開瀏覽器訪問nacos服務:http://localhost:8848/nacos,查看服務列表,以下圖:

經過頁面能夠看到兩個服務都成功註冊。

第六步:

先直接訪問alibaba-nacos-server服務,訪問地址:http://localhost:8902/test?msg=123456,瀏覽器顯示以下圖:

如今能夠測試服務網關是否配置生效了,能夠經過配置的網關訪問alibaba-nacos-server服務提供的接口服務,訪問地址:http://localhost:8905/server/test?msg=123456,以下圖:

這就是Spring Cloud Gateway的做用之一轉發,轉發規律:${GATEWAY_URL}/{微服務名}/{請求路徑}。


本文分享自微信公衆號 - 程序猿的故事(zengxueqi-music)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索