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