美文网首页
微服务架构之spring cloud gateway

微服务架构之spring cloud gateway

作者: 架构师联盟 | 来源:发表于2020-05-13 09:19 被阅读0次

    Spring Cloud Gateway是spring cloud中起着非常重要的作用,是终端调用服务的入口,同时也是项目中每个服务对外暴露的统一口径,我们可以在网关中实现路径映射、权限验证、负载均衡、服务聚合等业务功能。

    (一) 版本说明

    a) Spring boot 2.0.6.RELEASE

    b) Spring cloud Finchley.SR2

    c) Java version 1.8

    d) spring-cloud-starter-gateway 2.0.2.RELEASE

    (二) 项目设置

    1. Pom文件
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
    <version>2.0.2.RELEASE</version>
    </dependency>
    

    2. application.yml配置文件

    spring:
    application:
    name: gateway
    cloud:
    gateway:
    enabled: true
    routes:
    #终端模块
    - id: clientservice
    uri: lb://CLIENTSERVICE
    predicates:
    - Path=/client/**
    filters:
    - StripPrefix=1
    #回调模块
    - id: callbackservice
    uri: lb://CALLBACKSERVICE
    predicates:
    - Path=/callback/**
    filters:
    - StripPrefix=1
    

    3. 主要参数说明

    a) spring.application.name 项目名称

    b) spring.cloud.gateway 所有gateway配置信息的根节点

    c) spring.cloud.gateway.enabled 是否启用

    d) spring.cloud.gateway.routes 路由映射,注意这里是数组

    e) spring.cloud.gateway.routes[0].id 标志号

    f) spring.cloud.gateway.routes[0].uri 路由映射目标路径

    g) spring.cloud.gateway.routes[0].predicates 匹配规则,也是暴露的映射路径

    h) spring.cloud.gateway.routes[0].StripPrefix 是否包含匹配的前缀,比如 /callback/,如果设置为1,则只有传递到目标路径,如果设置为0,,则callback也一并传递到目标路径,一般设置为1不传递自定义的暴露服务名称

    (三) 项目运行

    1. 运行项目,在注册中心即可看到gateway注册进来了,如下图所示

    clip_image002

    2. 也要把我们配置文件中配置的2个微服务已经在运行如下图所示

    clip_image004

    名称你可以改成你自己的服务名称,但记得要跟配置的映射一致。

    3. 在任何一个终端输入gateway的IP:PORT/映射的服务名称/API名称,就可以看到经过网关映射后的效果

    a) Client服务

    clip_image006

    b) Callback服务

    clip_image008

    clip_image010

    这样spring cloud gateway网关就介绍完了,如果在开发中遇到问题,也可以留言共同探讨共同进步。

    相关文章

      网友评论

          本文标题:微服务架构之spring cloud gateway

          本文链接:https://www.haomeiwen.com/subject/nupgnhtx.html