继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

微服务下使用网关 Spring Cloud Gateway

九州编程
关注TA
已关注
手记 475
粉丝 42
获赞 202

webp

Spring Cloud Gateway 工作原理

webp

客户端向 Spring Cloud Gateway 发出请求,如果请求与网关程序定义的路由匹配,则将其发送到网关 Web 处理程序,此处理程序运行特定的请求过滤器链。

过滤器之间用虚线分开的原因是过滤器可能会在发送代理请求之前或之后执行逻辑。所有 "pre" 过滤器逻辑先执行,然后执行代理请求,代理请求完成后,执行 "post" 过滤器逻辑。

如何启动 Spring Cloud Gateway

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

<?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.anoyi</groupId>
    <artifactId>core-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>core-gateway</name>
    <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>

2、添加启动类 Application.java

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Configuration;@Configuration@SpringBootApplicationpublic class Application {    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

3、启动 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 来路由到这两个服务。

1、在 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:过滤规则

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, 添加自定义路由配置

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;@SpringBootApplicationpublic 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 等等。仅通过简单的配置即可实现功能强大的网关服务。



作者:Anoyi
链接:https://www.jianshu.com/p/1c942a8abe18


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP