iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringCloud Gateway的基本入门和注意点详解
  • 687
分享到

SpringCloud Gateway的基本入门和注意点详解

2024-04-02 19:04:59 687人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录1.gateway和zuul2.使用gateway的路由功能1. 搭载SpringCloud gateway2.简单使用gateway1.application配置1.gatew

1.gateway和zuul

spring cloud Finchley版本的gateway比zuul 1.x系列的性能和功能整体要好,且使用 Gateway 做跨域相比应用本身或是 Nginx 的好处是规则可以配置的更加灵活.

这两者相同的地方就是都是作为网关,处理前段的请求,可以进行路由到对应的服务或者url,也可以针对权限做过滤处理,也可以对其他服务响应的结果做处理

截至目前springCloud gateway最新版本是2.1.0 RC3,可见官方网站SprinGCloud gateway,每个版本增加的功能都比较多,改动的地方也比较多,前几个版本有比较坑的地方,建议使用最新版本

2.使用gateway的路由功能

1. 搭载springcloud gateway

准备一个spring cloud工程,包括eureka-server注册中心,service-client服务提供者,端口8090

service-client提供一个接口:


@RestController
@Slf4j
public class ProducerController {
  @RequestMapping("/hi")
  public String hi(@RequestParam String name) {
    log.info("[client服务] [hi方法]收到请求");
    return "hi " + name + ",i am from service-client";
  }
}

再建一个spring cloud工程,service-gateway网关,端口8088

pom的依赖:


 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/>
 </parent>
 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <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>
</dependencies>

application启动类:


package com.zgd.springcloud.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

这样基本的框架就搭好了,先启动eureka-server注册中心,再启动service-client和service-gateway

直接调用 localhost:8090/hi?name=zgd,可以收到


hi zgd,i am from service-client

2.简单使用gateway

GateWay大体可以分为路由工厂Route Predicate Factories,网关过滤器工厂GatewayFilter Factories,全局过滤器工厂Global Filters处理请求。

对于路由转发,Spring Cloud gateway内置了很多校验条件谓语(predicate)来实现路由功能。

比如

  • 根据时间来路由: After Route Predicate Factory某个时间点之后请求路由,Before Route Predicate Factory某个时间点之前请求路由,Between Route Predicate Factory两者时间之间
  • 通过请求路径来路由: Path Route Predicate Factory
  • 根据请求头来路由
  • 根据cookie来路由
  • 根据域名来路由

有两种方式配置,一种是配置文件application的方式,一种是代码配置

1.application配置

a. 路由到其他地址


spring:
  cloud:
    gateway:
      #可以根据请求参数,cookie,host,请求时间,请求头等进行校验判断路由, 下面根据先后顺序转发
      routes:
        - id: host_route
          uri: Http://httpbin.org:80/get
          predicates:
            - Path=/zzzgd*;q=0.8", 
    "Accept-Encoding": "gzip, deflate, br", 
    "Accept-Language": "zh-CN,zh;q=0.9", 
    "Connection": "close", 
    "Cookie": "SL_G_WPT_TO=zh; SL_GWPT_Show_Hide_tmp=undefined; SL_wptGlobTipTmp=undefined", 
    "Forwarded": "proto=http;host=\"localhost:8088\";for=\"0:0:0:0:0:0:0:1:55782\"", 
    "Host": "httpbin.org", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (windows NT 10.0; WOW64) AppleWEBKit/537.36 (Khtml, like Gecko) Chrome/70.0.3538.110 Safari/537.36", 
    "X-Forwarded-Host": "localhost:8088"
  }, 
  "origin": "0:0:0:0:0:0:0:1, 119.147.213.42", 
  "url": "http://localhost:8088/get?name=zgd"
}

上面的是根据地址来路由,还有下面多种路由配置:

根据域名来转发路由:


routes:
 - id: host_route
   uri: http://httpbin.org:80/get
   predicates:
     - Host=**.csdn.** # 请求域名携带csdn的,则转发
 - id: query_route
   uri: http://httpbin.org:80/get
   predicates:
     - Query=username, zzz* # 请求参数含有username,且值满足zzz开头的,则转发(对值的匹配可以省略)
 - id: header_route
   uri: http://httpbin.org:80/get
   predicates:
     - Header=request, \d+ # 如果请求头含有request,且为数字,则转发
 - id: cookie_route
      uri: http://httpbin.org:80/get
      predicates:
        - Cookie=name, zzzgd # 如果携带cookie,参数名为name,值为zzzgd,则转发
    - id: path_route
      uri: http://httpbin.org:80/get
      predicates:
        - Path=/zzzgd
@Configuration
public class GateWayConfig {
  @Bean
  public RouteLocator routeLocator(RouteLocatorBuilder builder) {
    return builder.routes()
            .route(r -> r.path("/fluent/**").and().query("name")
                    .uri("http://httpbin.org:80/get"))
            .build();
  }
}

启动,访问

http://localhost:8088/fluent/1/?name=bb

成功!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: SpringCloud Gateway的基本入门和注意点详解

本文链接: https://www.lsjlt.com/news/154328.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作