温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Cloud gateway网关服务是怎样的

发布时间:2021-10-08 15:33:16 来源:亿速云 阅读:153 作者:柒染 栏目:大数据

本篇文章为大家展示了Spring Cloud gateway网关服务是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

今天聊聊spring cloud gateway 作为spring cloud的亲儿子网关服务。很多的想法都是参照zuul,为了考虑zuul 迁移到gateway 提供了一个便利的条件。

gateway 他的核心功能也是和zuul 类似。但是他的实现方式与zuul 却有些不一样,他的核心是基于 Spring Boot 2.x, Spring WebFlux和Project Reactor 构建的。

  • Spring WebFlux 响应式Web框架。

    • Spring WebFlux是基于响应式流的,因此可以用来建立异步的、非阻塞的、事件驱动的服务。它采用Reactor作为首选的响应式流的实现库,不过也提供了对RxJava的支持。 由于响应式编程的特性,Spring WebFlux和Reactor底层需要支持异步的运行环境,比如Netty和Undertow;也可以运行在支持异步I/O的

    • Servlet 3.1的容器之上,比如Tomcat(8.0.23及以上)和Jetty(9.0.4及以上)。

  • spring-webflux上层支持两种开发模式:

    • 类似于Spring WebMVC的基于注解(@Controller、@RequestMapping)的开发模式;

    • Java 8 lambda 风格的函数式开发模式。

    • Spring WebFlux也支持响应式的Websocket服务端开发。

所以spring cloud gateway 不是基于阻塞的web 开发。他与传统的Servlet是存在冲突的。在创建功能的时候要排除掉传统的Servlet jar包引用

工作原理

客户端向Spring Cloud Gateway发出请求。如果网关处理程序映射确定请求与路由匹配,则将其发送到网关Web处理程序。该处理程序运行通过特定于请求的筛选器链发送请求。筛选器由虚线分隔的原因是,筛选器可以在发送代理请求之前或之后执行逻辑。执行所有“前置”过滤器逻辑,然后发出代理请求。发出代理请求后,将执行“发布”过滤器逻辑。

注意: 在没有端口的路由中定义的URI将分别将HTTP和HTTPS URI的默认端口分别设置为80和443

  • Predicate 断言:这是一个 Java 8 的 Predicate。输入类型是一个 ServerWebExchange。我们可以使用它来匹配来自 HTTP 请求的任何内容,例如 headers 或参数。

  • Route 路由转发 它由一个 serverID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配。

  • Filter 请求过滤 对web资源进行拦截,做一些处理后再交给处理器处理

修改之前工程的pom 文件总pom 里面我们有一个 spring-boot-starter-web 工程引用,删除掉。在服务里面单独依赖。上面已经讲述过,传统Servlet的jar包冲突问题。

在服务消费者和 服务提供者分别添加

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

我们创建工程 cloud-gateway ,修改pom

<dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <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>

创建 bootstrap.yml

server:
  port: 9000

spring:
  profiles:
    active: dev
  application:
    name: cloud-gateway-demo
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
    default-property-inclusion: non_null
  cloud:
    nacos:
      discovery:
        server-addr: 47.99.209.72:8848
      # ${prefix}-${spring.profile.active}.${file-extension}
      config:
        server-addr: 47.99.209.72:8848
        file-extension: yaml
    gateway:
      discovery:
        locator:
           # 是否与服务发现组件进行结合,通过serviceId转发到具体的服务实例。默认false,
          # 为true代表开启基于服务发现的路由规则。
          enabled: true
          # 配置之后访问时无需大写
          lower-case-service-id: true
      routes:
        - id: cloud-discovery-server
          uri: lb://cloud-discovery-server
          predicates:
            # 路径匹配,以 api 开头,直接配置是不生效的,看 filters 配置
            - Path=/server/**
          filters:
            # 前缀过滤,默认配置下,我们的请求路径是 http://localhost:9000/myshop-service-consumer-item/** 这时会路由到指定的服务
            # 此处配置去掉 1 个路径前缀,再配置上面的 Path=/api/**,就能按照 http://localhost:9000/api/** 的方式访问了
            - StripPrefix=1
        - id: cloud-discovery-client
          uri: lb://cloud-discovery-client
          predicates:
            # 路径匹配,以 api 开头,直接配置是不生效的,看 filters 配置
            - Path=/client/**
          filters:
            # 前缀过滤,默认配置下,我们的请求路径是 http://localhost:9000/myshop-service-consumer-item/** 这时会路由到指定的服务
            # 此处配置去掉 1 个路径前缀,再配置上面的 Path=/api/**,就能按照 http://localhost:9000/api/** 的方式访问了
            - StripPrefix=1

创建main 启动类

package com.xian.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


/**
 *
 * @Author: xlr
 * @Date: Created in 上午11:08 2019/11/4
 */
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServerApplication.class, args);
    }
}

启动服务 命令行curl http://localhost:9000/client/client/test

Spring Cloud gateway网关服务是怎样的

服务已经整合完毕。路由功能转发已经实现。配置文件的一些字段的说明也在注释上说明。 下一篇讲述一下 Spring Cloud Gateway 断言

上述内容就是Spring Cloud gateway网关服务是怎样的,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI