小白网-奉贤部落-奉贤免费信息发布平台

查看: 22|回复: 0
打印 上一主题 下一主题

gateway网关的作用(gateway搭建网关)

[复制链接]

2万

主题

2万

帖子

7万

积分

论坛元老

Rank: 8Rank: 8

积分
78182
跳转到指定楼层
楼主
发表于 2025-7-26 13:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这篇文章已经收录在我的Github选集里了。欢迎明星:https://github.com/yehongzhi/learningSummary!
了解服务网关的最好方法是从您为什么需要它开始。
根据主流微服务架构的特点,假设有A、B、c三个服务,如果这三个服务需要做一些请求过滤和权限检查,如何实现?
  • 每个服务都是自己实现的。
  • 编写一个公共服务,然后让A、B、C服务引入公共服务的Maven依赖。
  • 使用服务网关,所有客户端请求服务网关过滤请求并验证权限,然后将它们路由到A、B和C服务。第一种方式显然是逆天的,这里就不讨论了。第二种方法稍微聪明一些,但是如果公共服务的逻辑发生变化,那么所有依赖公共服务的服务都需要重新打包和部署才能生效。
    所以很显然,使用服务网关可以解决上述问题。其他服务不需要添加任何依赖,只需要在网关中配置一些参数,然后就可以路由转发给相应的后端服务。如果需要请求过滤和权限检查,它们都可以在网关层实现。如果需要更新权限检查的逻辑,只需要在网关层修改即可,其他后端服务不需要修改。
    接下来,我们来介绍一下服务网关的功能,包括:
  • 按指定路线发送
  • API监控
  • 权限控制
  • 限流所以服务网关很重要!那么,我们来学习一下目前主流的网关。
    开始使用GateWay的第一步是创建一个项目作为网关。这里使用的SpringBoot版本是2.0.1,它引入了依赖性:
    org . spring framework . cloudspring-cloud-starter-gatewayorg . spring framework . cloudspring-cloud-dependenciesfinch ley。SR1POMimport
    我们需要使用网关来转发请求,所以我们首先需要一个后端服务。在这里,我简单地创建了一个用户项目。然后启动用户项目,编写一个接口来获取所有用户信息:
    然后我们现在配置网关的application.yml来实现请求转发。
    服务器:端口:9201  spring: 应用:名称:API-网关云:网关:路由:-Uri:http://localhost:8080/user/getList #最终目的地的请求地址谓词:# Assert-Path =/user/getList # Route如果路径匹配
    也就是说,我请求http://localhost:9201/user/getList之后,端口9201就是一个网关服务,它会匹配/user/getList的路由,最后转发到目标地址http://localhost:8080/user/getList。
    这是一个简单的网关网关的使用。
    继续上面的介绍性例子,我们注意到有一个谓词配置,这有点令人困惑。中文翻译叫断言,有点类似于Java8流中的谓词函数。如果断言为真,则路由匹配。
    除此之外,gateway的另一个核心是Filter(过滤器),有全局和局部两种类型。那么整个网关流程是什么样的呢?请看下图:
    从图中可以看出,gateway的两个核心是谓词和过滤器。接下来,我们将重点介绍这两种的用法。
    路由谓词的使用Spring Cloud Gateway包含了很多内置的路由谓词工厂,你可以通过配置直接使用各种内置的预测器。
    路由后谓词请求在指定时间后匹配路由。
    spring:cloud:gateway:routes:-id:user _ getListuri:http://localhost:8080/user/getList谓词:-After = 2021-10-30t 01:00:00+08:00[亚洲/上海]
    在指定时间之前,路由谓词的请求将匹配此路由。
    spring:cloud:gateway:routes:-id:user _ getListuri:http://localhost:8080/user/getList谓词:-Before = 2021-10-30t 02:00:00+08:00[亚洲/上海]
    指定时间间隔内的路由谓词请求将匹配此路由。
    spring:cloud:gateway:routes:-id:user _ getListuri:http://localhost:8080/user/getList谓词:-Between = 2021-10-30t 01:00:00+08:00[亚洲/上海],2021-10-30T02:00:00+08:00
    具有指定cookie的Routepredict请求将匹配此路由。
    spring:cloud:gateway:routes:-id:user _ getListuri:http://localhost:8080/user/getList谓词:- cookie=username,yehongzhi
    使用POSTMAN发送一个在cookie中带有username=yehongzhi的请求。
    具有指定请求标头的标头预测请求将与此路由匹配。
    spring:cloud:gateway:routes:-Id:user _ getListuri:http://localhost:8080/user/getList谓词:- Header=X-Id,d+
    使用POSTMAN发送请求头中带有X-Id的请求。
    具有指定主机的Routepredict请求将匹配此路由。
    spring:cloud:gateway:routes:-id:user _ getListuri:http://localhost:8080/user/getList谓词:-Host = * * . yehongzhi . com
    使用POSTMAN发送一个请求,请求头中包含Host=www.yehongzhi.com。
    路径路由谓词发送对与路由匹配的指定路径的请求。
    spring:cloud:gateway:routes:-id:user _ getListuri:http://localhost:8080/user/getList谓词:- Path=/user/getList
    直接在浏览器中输入地址http://localhost:9201/user/getlist进行访问。
    方法路由谓词发送对指定方法的请求以匹配路由。
    spring:cloud:gateway:routes:-id:user _ getListuri:http://localhost:8080/user/getList谓词:- Method=POST
    通过邮递员邮寄请求。
    query使用指定的查询参数预测请求可以匹配此路由。
    spring:cloud:gateway:routes:-id:user _ Query _ byNameuri:http://localhost:8080/user/Query/byName谓词:- Query=name
    键入http://localhost:9201/user/query/by name?Name=tom地址,发送请求。
    权重路由谓词使用权重来路由相应的请求。以下配置表明80%的请求将被路由到localhost:8080,20%的请求将被路由到localhost:8081。
    spring:云:网关:路由:-id:user _ 1uri:http://localhost:8080谓词:- Weight=group1,8-id:user _ 2uri:http://localhost:8081谓词:- Weight=group1,2
    来自指定远程地址的RemoteAddr路由谓词请求可以匹配此路由。
    spring:cloud:gateway:routes:-id:user _ 1uri:http://localhost:8080/user/getList谓词:-remote addr = 192 . 168 . 1 . 4
    使用浏览器请求。
    使用组合spring:cloud:gateway:routes:-id:user _ 1uri:http://localhost:8080/user/getlist预测。-remote addr = 192 . 168 . 1 . 4-Method = POST-cookie = username,yehongzhi-Path =/user/getList
    使用POSTMAN发起请求,使用POST方法,uri为/user/getList,带cookie,RemoteAddr。
    自定义谓词如果需要自定义谓词,怎么玩?其实很简单。看源代码,照着做,需要继承AbstractRoutePredicateFactory类。
    例如,如果要求令牌值为abc,则将匹配路由。怎么写?请看代码:
    @ Componentpublic class TokenRoutePredicateFactory扩展AbstractRoutePredicateFactory {public static final String TOKEN _ KEY = " TOKEN value ";publitokenroutepricefactory(){/当前类的Config类通过反射创建Config并赋值,在apply中发回给super(tokenroutepricefactory . Config . class);}@ overridepublic list快捷方式字段order () {/"token value "与Configreturn arrays . aslist(token _ key)的接收字段一致;}@ overridepublic predict apply(config Config){/这里得到的Config对象是下面的自定义Config对象return new predict(){@ overridepublic boolean test(server webexchange exchange){MultiValueMap params = exchange . get request()。getQueryParams();//获取请求参数string value = params . Get first(" token ");//将请求参数与配置文件定义的令牌进行比较,如果相等,则返回truereturn config . gettokenvalue()!= null  amp ampconfig.getTokenValue()。等于(值);}};}/用于接收配置文件公共静态类config {私有字符串标记值定义的值;public String getTokenValue(){return token value;}public void setTokenValue(String token value){this . token value = token value;}}}
    这里需要注意的一点是,类名必须在RoutePredicateFactory的末尾,前一个用作配置名。例如,TokenRoutePredicateFactory的配置名是Token,这是一个约定的配置。
    然后将此配置添加到配置文件中:
    spring:Cloud:Gateway:Routes:-ID:user _ 1URI:http://localhost:8080/user/getlist预测:
    然后用POSTMAN发送请求,带有令牌参数,其值为abc。
    如果token的值不正确,它将报告404。
    为什么要整合注册中心?因为每个服务后面通常有多台机器,而且一般用服务名来配置,而不用服务的IP地址,需要负载均衡调用。
    我将使用Nacos作为这里的注册中心。
    介绍Maven依赖关系:
    org . spring framework . cloudspring-cloud-starter-Alibaba-nacos-discoveryorg . spring framework . cloudspring-cloud-dependenciesFinchley。SR1POMimportorg . spring framework . cloudspring-cloud-Alibaba-dependencies0 . 2 . 2 . releasePOMimport
    用注释启动该类以打开注册表。
    @ spring boot application@ EnableDiscoveryClientpublic class gateway application {public static void main(String[]args){spring application . run(gateway application . class,args);}}
    将配置添加到application.yml:
    spring:application:name:API-Gatewaycloud:nacos:discovery:server-addr:127 . 0 . 0 . 1:8848Service:$ { spring . application . name }Gateway:-ID:consumerURI:lb://consumer #使用LB协议,consumer是服务名
    创建一个消费者并向nacos注册,并提供一个接口:
    @ rest controllerpublic class consumer controller {@ Value(" $ { server . port } "private String port;@ request mapping(" consumer/get detail/{ ID } ")public string get detail(@ path variable(" ID ")string ID){return "端口号:"+port+",采集id:"+id+" }}
    启动消费者和网关项目,然后打开nacos控制台查看这两个服务。
    连续请求地址为http://localhost:9201/consumer/get detail/1,可以看出实现了负载均衡调用服务。
    可能有人会觉得每个服务都需要一条路线,很麻烦。有一个简单的配置可以解决这个问题:
    spring:gateway:discovery:locator:enabled:true
    然后启动该服务并重试。需要将服务名添加到请求地址中,仍然没有问题!
    文章最后重点介绍了网关的路由转发功能,并集成了注册中心。权限控制可以通过过滤器来实现。因为篇幅有点长,过滤器放在下一篇文章里。感谢您的阅读。
    如果你觉得有用,请喜欢。你们的赞是我创作的最大动力~
    我是一个努力让大家记住的程序员。下次见!!!
    能力有限,如有错误或不当之处,请批评指正,共同学习交流!
  • 回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|小白网-奉贤部落-奉贤免费信息发布平台  

    GMT+8, 2025-8-21 21:33 , Processed in 0.055891 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表