本文是介紹一下SpringCloud Gateway簡單路由轉發使用。
SpringCloud Gateway簡介
SpringCloud是基於Spring Framework 5,Project Reactor和Spring Boot 2.0構建,目標是用於替代zuul。
官方文檔
官方文檔地址:https://cloud.spring.io/spring-cloud-gateway/
在官方文檔上是這樣介紹Spring Cloud Gateway的:
該項目提供了一個用於在Spring MVC之上構建API網關的庫。Spring Cloud Gateway旨在提供一種簡單而有效的方式來路由到API,並為他們提供橫切關注點。)
入門案例
接下來,介紹一下簡單使用SpringCloud Gateway路由功能(本文使用SpringBoot2.0.0.RELEASE和SpringCloud Finchley.RC1版本)。
創建項目
新建一個項目,項目種加入SpringCloud Gateway依賴,完整pom如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalaoyang</groupId>
<artifactId>springcloud_gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud_gateway</name>
<description>springcloud_gateway</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RC1</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-devtools</artifactId>
<scope>runtime</scope>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
SpringBoot主程序
在類中配置路由
主程序中加入了一種配置路由的方法,利用@Bean的方式自定義RouteLocator。
package com.dalaoyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringcloudGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudGatewayApplication.class, args);
}
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/jianshu")
.uri("http://www.jianshu.com/u/128b6effde53")
).build();
}
}
在配置文件中配置:
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
gateway:
routes:
- id: dalaoyang
uri: http://www.dalaoyang.cn/
predicates:
- Path=/dalaoyang/**
- id: juejin
uri: https://juejin.im/user/5aa50b96f265da23866f836e
predicates:
- Path=/juejin/**
運行測試
到這里其實就配置完成了
訪問http://localhost:8888/dalaoyang 自動跳轉到了我的博客首頁
訪問http://localhost:8888/jianshu 自動跳轉到了我的簡書首頁
訪問http://localhost:8888/juejin 自動跳轉到了我的掘金首頁
源碼下載:大老楊源碼