<
>

SpringCloud超详细讲解微服务网关Gateway

2022-07-16 17:56:19 来源:易采站长站 作者:

目录
前言微服务网关GateWay介绍GateWay特性介绍Gateway 中的相关术语Gateway实战1、创建项目gateway2、创建启动类3、新增配置文件4、编程方式实现路由5、启动验证总结

前言

上一篇:微服务网关Zuul

上文中,我们介绍了微服务网关Zuul,Zuul>

所以本文需要再介绍一下Gateway。

微服务网关GateWay介绍

Spring Cloud Gateway 是 Spring 体系内的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。

Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全、监控/指标和限流。

GateWay特性介绍

    基于>动态路由Predicates 和 Filters 作用于特定路由集成 Hystrix 断路器集成 Spring Cloud DiscoveryClient易于编写的 Predicates 和 Filters限流路径重写

    Gateway>
      Route(路由):这是网关的基本构建块。它由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配。Predicate(断言):这是一个 Java 8 的 Predicate。输入类型是一个 ServerWebExchange。我们可以使用它来匹配来自 HTTP 请求的任何内容,例如 headers 或参数。Filter(过滤器):这是org.springframework.cloud.gateway.filter.GatewayFilter的实例,我们可以使用它修改请求和响应。

      Gateway实战

      上文中,我们启动了注册中心registry,dms服务,和app服务,以及zuul服务,本文我们将创建gateway服务以替换zuul:

      1、创建项目gateway

      创建子模块gateway>

      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-gateway</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
      

      2、创建启动类

      @EnableEurekaClient
      @SpringBootApplication
      public class GateWayApplication {
          public static void main(String[] args) {
              SpringApplication.run(GateWayApplication.class, args);
          }
      }
      

      启动类上增加@EnableEurekaClient以及@SpringBootApplication注解。

      3、新增配置文件

      新增配置文件application.yml

      server:
        port: 8004
      spring:
        application:
          name: gateway
        cloud:
          gateway:
            discovery:
              locator:
                enabled: true
                lower-case-service-id: true
            routes:
              - id: eureka-client-app-1
                uri: lb://app
                predicates:
                  - Path=/**
      eureka:
        client:
          service-url:
            defaultZone: http://localhost:8001/eureka/

      spring.cloud.gateway.routes路由参数配置说明:

        id:我们自定义的路由 ID。uri:需要转发的目标服务地址。predicates:路由条件。filters:过滤规则,本示例暂时没用。

        4、编程方式实现路由

        上面路由规则我们也可以使用编程方式来实现,在启动类中增加如下代码,是等效的:

        @Configuration
        public class GatewayConfig {
            @Bean
            public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
                RouteLocatorBuilder.Builder routes = builder.routes();
                routes.route("eureka-client-app-1",r -> r.path("/**")
                       .uri("lb://app"))
                        .build();
                return routes.build();
            }
        }
        

        5、启动验证

        访问Gateway服务的地址:http://localhost:8004/app/index,效果如下:

        总结

        本文介绍了如何使用>

        到此这篇关于SpringCloud超详细讲解微服务网关Gateway的文章就介绍到这了,更多相关SpringCloud Gateway内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!

暂时禁止评论

微信扫一扫

易采站长站微信账号