zoukankan      html  css  js  c++  java
  • 【SpringCloud】Gateway路由配置(十七)

      前一章【SpringCloud】Gateway网关入门(十六)介绍的Gateway的基本使用,本章介绍Gateway路由配置

      本章使用项目,还是上一章的项目

    Gateway路由配置

      Gateway的路由配置有2中方式,一种是通过YML配置文件来配置,一种是通过配置类来配置

    YML配置文件配置路由

      1、指定路径转发路由

      即根据指定的路径,进行转发,案例参考上一章

      配置如下:

     1 spring:
     2   application:
     3     name: cloud-gateway-gateway
     4   cloud:
     5     gateway:
     6       routes:
     7           # 路由的ID,没有固定规则,但要求唯一,建议配合服务名
     8         - id: payment_routh
     9           # 匹配后提供服务的路由地址
    10           uri: http://localhost:8001
    11           # 断言,路径相匹配的进行路由
    12           predicates:
    13             - Path=/payment/get/**

      2、通过服务名实现动态路由

      默认情况下Gatway会根据注册中心注册的服务列表, 以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能

      1)在前面项的基础上,新增一个支付服务模块,与已有支付模块相同,参考:【SpringCloud】服务提供者集群与服务发现Discovery(三)

      2)修改Gateway网关项目(springcloud-gateway-gateway9527)配置文件application.yml,修改内容如下:

     1 spring:
     2   application:
     3     name: cloud-gateway-gateway
     4   cloud:
     5     gateway:
     6       discovery:
     7         locator:
     8           # 开启从注册中心动态创建路由的功能,利用微服务名进行路由
     9           enabled: true
    10       routes:
    11           # 路由id
    12         - id: payment_routh
    13           # 匹配后提供服务路由地址
    14           uri: lb://cloud-payment-service
    15           # 断言
    16           predicates:
    17             - Path=/payment/get/**

      3)测试

        a、启动项目

        b、查看Eureka注册中心,地址:http://localhost:8761/

        

        c、访问地址:http://localhost:8002/payment/get/1,验证支付服务正常

        d、访问地址:http://localhost:9527/payment/get/1,验证动态网关已生效,并且是负载轮询的方式访问支付模块的服务

        

    通过配置类来配置路由

      本章演示通过配置类配置访问百度新闻网站(http://news.baidu.com

      1、访问百度新闻国内新闻模块,地址:http://news.baidu.com/guonei,确认新闻地址

      

      2、在以上Gateway网关项目(springcloud-gateway-gateway9527)中,新增配置类,内容如下:

     1 @Configuration
     2 public class GatewayConfig {
     3 
     4     @Bean
     5     public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
     6         // 路由构造器
     7         RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
     8         // 设置路径
     9         routes.route("baidu_news_guonei_routh", r -> {
    10             return r.path("/guonei").uri("http://news.baidu.com/guonei");
    11         });
    12         routes.route("baidu_news_guoji_routh", r -> {
    13             return r.path("/guoji").uri("http://news.baidu.com/guoji");
    14         });
    15 
    16         return routes.build();
    17     }
    18 }

      3、测试

        1)启动项目

        2)访问地址:http://localhost:9527/guonei,验证通过网关是否能访问国内新闻

        

        3)访问地址:http://localhost:9527/guoji,验证通过网关是否能访问国际新闻

      

  • 相关阅读:
    MQTT学习笔记——Yeelink MQTT维修 采用mqtt.js和paho-mqtt
    ubuntu 14.04设备OVS虚拟OpenFlow交换机配置汇总
    于iOS跳转到应用程序AppStore
    C++学习笔记33 转换操作符
    Win32 Windows规划 三
    [2014 Regional]牡丹江 H Hierarchical Notation 做题记录
    序言树,为了,序后,(递归)
    Visual Studio Code尝试体验
    2015第40周二Node学习
    2015第40周一Node学习
  • 原文地址:https://www.cnblogs.com/h--d/p/12735381.html
Copyright © 2011-2022 走看看