分布式系统使用网关到底是好还是坏?

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"灵魂拷问"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"分布式系统需要统一的网关吗?"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"网关会带来哪些优势?"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"引入网关会带来灾难吗?"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"分布式系统的设计大体上分为中心化和非中心化,像现在流行的微服务模式,本质上是把各种业务拆分为独立的进程来实现业务的扩展性。伴随着业务的不断被拆分,整体的系统性能也有所提高,但是系统又往往面临着中心化的需求,这个世界就是这么有趣,我们不但要非中心化的优势,还要保证中心化的优势。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"link","attrs":{"href":"#统一的需求","title":null}},{"type":"text","text":"统一的需求"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"说到非中心化需求,最简单最常见的就是鉴权,简单来说就是判断用户是否有执行某种动作的权限。以鉴权为例,当有N个微服务有同样的鉴权需求的时候,有以下几种解决方案:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每个服务自己实现一遍"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"写一个公共的服务接口,每个服务都调用这个接口"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"写到统一的流量入口处,只需要实现一次"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有人说第一种方案没人用,是吗?在服务数量不是太多的情况下,据我所知有很多公司初期都是每个服务都实现自己的鉴权,不要说这种做法一无是处,首先在各个团队技术栈不同的情况下,这是一个团队可以快速实现功能的一种解决方案,而且不依赖于任何其他服务,这意味着这个服务不会受到别的服务的影响,这体现的是非中心化最核心的价值之一。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至于第二种解决方案是典型的集中式处理的方案之一,他按照业务功能把鉴权独立成单独的服务,所有使用鉴权服务的系统都依赖于这个服务,所以这个统一的鉴权服务需要保证高可用,而且对性能比较敏感,稍有不慎,流量高峰就有可能对整个系统造成致命打击。但是,还是有不少公司在用这种架构模式,你说奇怪不奇怪,正所谓,存在即合理。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至于第三种使用网关模式,可以说是目前市面上文章吹捧最多的解决方案。当然得到广大程序员的吹捧自然有它的优势,但是请记住,它并非没有劣势。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/99/9913f07787c9eb3b9296bae7c40a14de.jpeg","alt":"image","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从上图清晰的可以看到,来自于客户端的所有流量都经过网关,所以理论上来讲,所有统一化的需求都可以在网关进行实现,这样每个后端的业务服务代码都不会有任何的侵入性。至于这些需求最常见的有:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"网关可以屏蔽后端服务的协议细节,比如有的服务可能采用的rpc方式,有的服务采用的restful方式,有的甚至可能采用的比较古老的web Service,在网关这一层我们可以统一通信协议,这样客户端调用起来会方便很多。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以统一整个系统的认证和授权功能,甚至可以统一不同的客户端采用的不同认证方式,比如Cookie认证,Session认证等。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以统一设置后端服务的保护策略,比如:不同的服务可以设置不同的白名单系统,不同的服务可以采用不同的限流策略等。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在网关这个统一的入口,更容易做日志的收集工作。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"虽然网关很香,但是一旦整个系统引入网关,就会随之带来一些问题:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"增加了网关,在请求的整个过程中就至少多了一层转发,性能会有稍微下降。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"网关如果出现问题,降造成整个系统不稳定,甚至down机,这就是网关的单点问题。所以一般网关都会有多个节点。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"link","attrs":{"href":"#如何引入网关","title":null}},{"type":"text","text":"如何引入网关"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在没有网关的时候,系统一般都会有一个无状态的web层,这一层的主要作用是根据业务组织后端服务结果来响应客户端的请求,例如:一个订单详情的请求,web层会同时请求订单服务来查询订单信息,请求用户服务来查询下单人信息,请求商户服务来查询商家信息,然后把这些信息组合成客户端想要的数据格式,然后返回客户端数据。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ca/caafdfff596794806a0cc08878133434.jpeg","alt":"image","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果做鉴权,限流等操作,同样也是做在这个web层,在引入了网关之后,这个web层到底还要不要呢?不同的公司有不同的做法,不过我还是建议保留web层,用它来做服务的聚合层,这样整体架构就变成了这样"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4a/4a828a62bd9b682bd80e6363f2ee82fb.jpeg","alt":"image","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"link","attrs":{"href":"#网关和负载均衡","title":null}},{"type":"text","text":"网关和负载均衡"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通过以上阐述可以看到,网关在部署架构上和负载均衡器很像,但是它们其实是两种东西。虽然它们都会承载统一的入口流量,但是负载均衡器只是单纯的对请求的转发,而网关除了对请求的转发之外,更重要的职责是统一的业务处理,就像鉴权,限流,降级等。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以从功能角度来说,网关几乎可以代替负载均衡器。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"link","attrs":{"href":"#写在最后","title":null}},{"type":"text","text":"写在最后"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你的系统是否需要网关不仅仅要看系统的需求,还要看公司的整体技术运维能力,毕竟一旦引入网关,它对技术上的要求比较高,而且对高可用和性能比较敏感。但是网关对于那些统一化的需求有着天然的优势,无论是鉴权,还是服务的治理,所以很多公司引入了网关。无论是采用自建还是开源来实现网关,我们都要考虑它的扩展性。现在比较流行的网关有:Kong,Zuul,Tyk 等,详细信息可以参见它们的官网。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"更多精彩文章"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&album_id=1342955119549267969&__biz=MzIwNTc3OTAxOA==#wechat_redirect","title":null},"content":[{"type":"text","text":"分布式大并发系列"}]}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&album_id=1342959003139227648&__biz=MzIwNTc3OTAxOA==#wechat_redirect","title":null},"content":[{"type":"text","text":"架构设计系列"}]}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&album_id=1342962375443529728&__biz=MzIwNTc3OTAxOA==#wechat_redirect","title":null},"content":[{"type":"text","text":"趣学算法和数据结构系列"}]}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&album_id=1342964237798391808&__biz=MzIwNTc3OTAxOA==#wechat_redirect","title":null},"content":[{"type":"text","text":"设计模式系列"}]}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f8/f8af5984765a267892bf1a1272272625.png","alt":"image","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章