zoukankan      html  css  js  c++  java
  • 微服务架构之spring cloud gateway

    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网关就介绍完了,如果在开发中遇到问题,也可以留言共同探讨共同进步。

  • 相关阅读:
    Directx11教程(21) 修正程序最小化异常bug
    Directx教程(28) 简单的光照模型(7)
    Directx教程(22) 简单的光照模型(1)
    Directx教程(26) 简单的光照模型(5)
    Directx教程(25) 简单的光照模型(4)
    finit 1.5 发布,/sbin/init 的服务器监控器
    MySQL 8小时问题
    DataCleaner 3.0.1 发布,数据质量分析管理
    dedecms调用其它栏目列表
    ReportPlus:在iPad上创建和使用仪表盘及报表
  • 原文地址:https://www.cnblogs.com/lzh-boy/p/10381448.html
Copyright © 2011-2022 走看看