如何啓動 Spring Cloud Gateway
一、新建 Maven 工程,添加相關依賴 pom.xmlhtml
-
<?xml version="1.0" encoding="UTF-8"?>
java -
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
spring -
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
apache -
<modelVersion>4.0.0</modelVersion>
app -
-
<groupId>com.anoyi</groupId>
maven -
<artifactId>core-gateway</artifactId>
spring-boot -
<version>0.0.1-SNAPSHOT</version>
測試 -
-
<name>core-gateway</name>
ui -
<description>gateway for miroservice</description>
編碼 -
-
<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-gateway</artifactId>
-
<version>2.0.0.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>
-
-
</dependencies>
-
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
</plugin>
-
</plugins>
-
</build>
-
-
</project>
二、添加啓動類 Application.java
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.context.annotation.Configuration;
-
-
@Configuration
-
@SpringBootApplication
-
public class Application {
-
-
public static void main(String[] args) {
-
SpringApplication.run(Application.class, args);
-
}
-
-
}
三、啓動 Application(和 Spring Boot 項目同樣)
訪問 http://localhost:8080/
報錯 404,同時日誌輸出:
-
2018-06-27 09:18:48.981 WARN 44156 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler :
-
Failed to handle request [GET http://localhost:8080/]: Response status 404
配置服務的路由:配置文件方式
假設本地啓動了另外兩個 Spring Boot 服務,分別是 服務A( http://localhost:8081)、服務B( http://localhost:8082 ),下面經過 Spring Cloud Gateway 來路由到這兩個服務。
一、在 resources 路徑下添加配置文件 application.yml
-
spring:
-
cloud:
-
gateway:
-
routes:
-
- id: host_route
-
uri: http://localhost:8081
-
predicates:
-
- Path=/a/**
-
filters:
-
- StripPrefix=1
-
- id: host_route
-
uri: http://localhost:8082
-
predicates:
-
- Path=/b/**
-
filters:
-
- StripPrefix=1
-
id:固定,不一樣 id 對應不一樣的功能,可參考 官方文檔
-
uri:目標服務地址
-
predicates:路由條件
-
filters:過濾規則
二、重啓 Gateway 服務
三、測試
訪問 http://localhost:8080/a/
路由到 服務A http://localhost:8081/
訪問 http://localhost:8080/b/
路由到 服務B http://localhost:8082/
其餘地址,例如 http://localhost:8080/a/user/all
路由到 服務A http://localhost:8081/user/all
配置服務的路由:編碼方式
實現如上服務路由,還能夠經過編碼的方式實現。
一、刪除配置文件 application.yml
二、修改 Application.java, 添加自定義路由配置
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;
-
import org.springframework.cloud.gateway.route.RouteLocator;
-
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
-
import org.springframework.context.annotation.Bean;
-
-
@SpringBootApplication
-
public class Application {
-
-
@Bean
-
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
-
StripPrefixGatewayFilterFactory.Config config = new StripPrefixGatewayFilterFactory.Config();
-
config.setParts(1);
-
return builder.routes()
-
.route("host_route", r -> r.path("/a/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:8081"))
-
.route("host_route", r -> r.path("/b/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:8082"))
-
.build();
-
}
-
-
public static void main(String[] args) {
-
SpringApplication.run(Application.class, args);
-
}
-
-
}
其餘功能
http://cloud.spring.io/spring-cloud-gateway/single/spring-cloud-gateway.html
官方提供了大量的路由規則,好比Time、Host、Header 等等,同時也提供了大量的過濾器,好比AddRequestHeader、AddRequestParameter、AddResponseHeader 等等。僅經過簡單的配置便可實現功能強大的網關服務。