如何在项目中引入Spring Cloud Gateway

1.如何在项目中引入Spring Cloud Gateway

如果你是使用的Maven管理的项目,在项目中添加group为org.springframework.cloud和artifact id为spring-cloud-starter-gateway的starter。如下面的Maven依赖所示:

<!-- 配置Spring Boot -->
  <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.1.3.RELEASE</version>
  </parent>
<!-- 配置Spring Clound 依赖管理 -->
  <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version><!--注意这里面的版本,不同的版本,代码会有一些差别 --> <type>pom</type>                 <scope>import</scope> </dependency> </dependencies>   </dependencyManagement>   <dependencies> <dependency> <!--引入spring cloud gateway--> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> </dependencies>

 

如果你引入了spring cloud gateway的依赖,但是又不想使用spring cloud gateway生效,可以在application.yml配置中添加spring.cloud.gateway.enabled=false。

注意,Spring Cloud Gateway需要Spring Boot和Spring Webflux提供的Netty的运行时环境,因此,它不可以打包成war包,也不可以在传统的Servlet容器(比如tomcat)中运行。所以Spring Cloud Gateway项目中不能依赖<artifactId>spring-boot-starter-web</artifactId>,要不然会报错。

英语文档地址:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/multi/multi_gateway-starter.html

项目源码地址:https://gitee.com/wgslucky/SpringCloud   QQ交流群:677464431

posted @ 2019-08-22 17:58  王广帅  阅读(5315)  评论(0编辑  收藏  举报