Spring Cloud Gateway项目创建步骤详细解析是怎样的?

2026-06-10 11:131阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计477个文字,预计阅读时间需要2分钟。

Spring Cloud Gateway项目创建步骤详细解析是怎样的?

创建网关项目+添加网关后微服务的架构图+创建项目+POM文件+properties+java.version 1.8+spring-cloud.version Greenwich.SR3+/properties+dependencies+dependency+groupId org.springframework.cloud+version 2.1.3+/dependencies

创建网关项目

加入网关后微服务的架构图

创建项目


POM文件

<properties> <java.version>1.8</java.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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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> </dependencies> </dependencyManagement>

修改配置文件

将项目目录下的/src/main/resources/application.properties文件重命名为application.yml,properties配置格式和yml配置格式是等效的,而yml配置格式能更好的被配置中心使用,所以我们使用yml配置格式。

Spring Cloud Gateway项目创建步骤详细解析是怎样的?

测试网关项目

application.yml配置文件内容修改如下:

server: port: 9000 spring: cloud: gateway: routes: - id: first_route uri: github.com/sunweisheng predicates: - Path=/test

  • port:网关服务端口
  • routes:路由集合
  • id:路由的唯一标示
  • uri:路由目标地址
  • predicates:路由条件,如果为true则路由到uri

predicates(还有filters)的种类很多请参考Spring Cloud Gateway官网

启动项目测试

访问127.0.0.1:9000/test

源码

Github仓库: github.com/sunweisheng/spring-cloud-example

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计477个文字,预计阅读时间需要2分钟。

Spring Cloud Gateway项目创建步骤详细解析是怎样的?

创建网关项目+添加网关后微服务的架构图+创建项目+POM文件+properties+java.version 1.8+spring-cloud.version Greenwich.SR3+/properties+dependencies+dependency+groupId org.springframework.cloud+version 2.1.3+/dependencies

创建网关项目

加入网关后微服务的架构图

创建项目


POM文件

<properties> <java.version>1.8</java.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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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> </dependencies> </dependencyManagement>

修改配置文件

将项目目录下的/src/main/resources/application.properties文件重命名为application.yml,properties配置格式和yml配置格式是等效的,而yml配置格式能更好的被配置中心使用,所以我们使用yml配置格式。

Spring Cloud Gateway项目创建步骤详细解析是怎样的?

测试网关项目

application.yml配置文件内容修改如下:

server: port: 9000 spring: cloud: gateway: routes: - id: first_route uri: github.com/sunweisheng predicates: - Path=/test

  • port:网关服务端口
  • routes:路由集合
  • id:路由的唯一标示
  • uri:路由目标地址
  • predicates:路由条件,如果为true则路由到uri

predicates(还有filters)的种类很多请参考Spring Cloud Gateway官网

启动项目测试

访问127.0.0.1:9000/test

源码

Github仓库: github.com/sunweisheng/spring-cloud-example

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。