查看原文
其他

服务网关 Spring Cloud Gateway 的应用

spring4all社区 SpringForAll社区 2020-10-17

如何启动 Spring Cloud Gateway

1、新建 Maven 工程,添加相关依赖 pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4.    <modelVersion>4.0.0</modelVersion>

  5.    <groupId>com.anoyi</groupId>

  6.    <artifactId>core-gateway</artifactId>

  7.    <version>0.0.1-SNAPSHOT</version>

  8.    <name>core-gateway</name>

  9.    <description>gateway for miroservice</description>

  10.    <properties>

  11.        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  12.        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  13.        <java.version>1.8</java.version>

  14.    </properties>

  15.    <dependencyManagement>

  16.        <dependencies>

  17.            <dependency>

  18.                <groupId>org.springframework.cloud</groupId>

  19.                <artifactId>spring-cloud-gateway</artifactId>

  20.                <version>2.0.0.RELEASE</version>

  21.                <type>pom</type>

  22.                <scope>import</scope>

  23.            </dependency>

  24.        </dependencies>

  25.    </dependencyManagement>

  26.    <dependencies>

  27.        <dependency>

  28.            <groupId>org.springframework.cloud</groupId>

  29.            <artifactId>spring-cloud-starter-gateway</artifactId>

  30.        </dependency>

  31.    </dependencies>

  32.    <build>

  33.        <plugins>

  34.            <plugin>

  35.                <groupId>org.springframework.boot</groupId>

  36.                <artifactId>spring-boot-maven-plugin</artifactId>

  37.            </plugin>

  38.        </plugins>

  39.    </build>

  40. </project>

2、添加启动类 Application.java

  1. import org.springframework.boot.SpringApplication;

  2. import org.springframework.boot.autoconfigure.SpringBootApplication;

  3. import org.springframework.context.annotation.Configuration;

  4. @Configuration

  5. @SpringBootApplication

  6. public class Application {

  7.    public static void main(String[] args) {

  8.        SpringApplication.run(Application.class, args);

  9.    }

  10. }

3、启动 Application(和 Spring Boot 项目一样)

访问 http://localhost:8080/ 报错 404,同时日志输出:

  1. 2018-06-27 09:18:48.981  WARN 44156 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler :

  2. Failed to handle request [GET http://localhost:8080/]: Response status 404

配置服务的路由:配置文件方式

假设本地启动了另外两个 Spring Boot 服务,分别是 服务A( http://localhost:8081)、服务B( http://localhost:8082 ),下面通过 Spring Cloud Gateway 来路由到这两个服务。

1、在 resources 路径下添加配置文件 application.yml

  1. spring:

  2.  cloud:

  3.    gateway:

  4.      routes:

  5.      - id: host_route

  6.        uri: http://localhost:8081

  7.        predicates:

  8.        - Path=/a/**

  9.        filters:

  10.        - StripPrefix=1

  11.      - id: host_route

  12.        uri: http://localhost:8082

  13.        predicates:

  14.        - Path=/b/**

  15.        filters:

  16.        - StripPrefix=1

  • id:固定,不同 id 对应不同的功能,可参考 官方文档

  • uri:目标服务地址

  • predicates:路由条件

  • filters:过滤规则

2、重启 Gateway 服务

3、测试

访问 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

配置服务的路由:编码方式

实现如上服务路由,还可以通过编码的方式实现。

1、删除配置文件 application.yml

2、修改 Application.java, 添加自定义路由配置

  1. import org.springframework.boot.SpringApplication;

  2. import org.springframework.boot.autoconfigure.SpringBootApplication;

  3. import org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;

  4. import org.springframework.cloud.gateway.route.RouteLocator;

  5. import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;

  6. import org.springframework.context.annotation.Bean;

  7. @SpringBootApplication

  8. public class Application {

  9.    @Bean

  10.    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {

  11.        StripPrefixGatewayFilterFactory.Config config = new StripPrefixGatewayFilterFactory.Config();

  12.        config.setParts(1);

  13.        return builder.routes()

  14.                .route("host_route", r -> r.path("/a/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:8081"))

  15.                .route("host_route", r -> r.path("/b/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:8082"))

  16.                .build();

  17.    }

  18.    public static void main(String[] args) {

  19.        SpringApplication.run(Application.class, args);

  20.    }

  21. }

其他功能

http://cloud.spring.io/spring-cloud-gateway/single/spring-cloud-gateway.html

官方提供了大量的路由规则,比如Time、Host、Header 等等,同时也提供了大量的过滤器,比如AddRequestHeader、AddRequestParameter、AddResponseHeader 等等。仅通过简单的配置即可实现功能强大的网关服务。


推荐:技术编程的成长道路上,哪些人对你帮助最大

上一篇:Java经典面试题:一个线程两次调用start()方法会出现什么情况?


最好的赞赏

就是你的关注

点击阅读原文获取更多


    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存