spring cloud gateway 入门最好的一个例子
从0开始学习spring cloud gateway
1.首先创建一个maven工程
点击next
输入对应的值,然后next
选择左边的spring cloud Routing,选择gateway,右边自动出现gateway,点击next,
发现该工程的名字以及工程所在的位置,点击finish,到此工程创建成功。
看一下工程的目录:
查看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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zhd.springcloud</groupId>
<artifactId>gatewaytset04</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gatewaytset04</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</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-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
2.修改application.properties名字,改为application.yml,当然了,你也可以不用修改,
修改后:
3.开始编写yml配置文件:
server:
port: 8091
4.编写代码
package com.zhd.springcloud.gatewaytset04;
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;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class Gatewaytset04Application {
public static void main(String[] args) {
SpringApplication.run(Gatewaytset04Application.class, args);
}
//在spring cloud gateway中使用RouteLocator的Bean进行路由转发,等价于在yml配置文件中的配置
@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder){
String str = "https://www.cnblogs.com/fulong133";
return builder.routes()
//id 表示被转发到uri地址的id名,
.route("id",p -> p
//predicates,当访问的连接满足http://localhost:8091/fulong133 时即转发到str
.path("/fulong133")
.uri("https://www.cnblogs.com"))
.build();
}
}
5启动工程
6然后访问网址:http://localhost:8091/fulong133
就回到了我的博客
==============================分割线===================================
7.用代码编写之外,还可以用yml配置文件实现
把代码注释掉
//@RestController
@SpringBootApplication
public class Gatewaytset04Application {
public static void main(String[] args) {
SpringApplication.run(Gatewaytset04Application.class, args);
}
//在spring cloud gateway中使用RouteLocator的Bean进行路由转发,等价于在yml配置文件中的配置
// @Bean
// public RouteLocator myRoutes(RouteLocatorBuilder builder){
// String str = "https://www.cnblogs.com/fulong133";
// return builder.routes()
// //id 表示被转发到uri地址的id名,
// .route("id",p -> p
// //predicates,当访问的连接满足http://localhost:8091/fulong133 时即转发到str
// .path("/fulong133")
// .uri("https://www.cnblogs.com"))
// .build();
// }
}
然后修改yml文件
# 端口号
server:
port: 8091
# 配置
spring:
application:
# name:后面要有空格
name: spring-cloud-gateway
cloud:
gateway:
routes:
- id: id
uri: https://www.cnblogs.com
# 断言
predicates:
# 匹配路径转发
- Path=/fulong133
访问网址:http://localhost:8091/fulong133,然后转发到我的博客,入门完成了,大家可以根据官方文档上的其他predicates进行练习,当然还有filter,限流等等。推荐一个网址:方志朋 : https://blog.csdn.net/forezp/article/details/85057268 大家从github上下载代码或者自己创建工程,然后根据文中信息一步一步来,挺简单的。看完了点个赞呗~~