本文是介绍一下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 自动跳转到了我的掘金首页
源码下载:大老杨源码