微服务

138 阅读7分钟

什么是微服务

服务是一种开发软件的架构,由小型独立服务组成,服务之间通过API进行通信,服务由各个小型团队负责。

微服务架构使应用程序更易于扩展和更快地开发,从而加速创新并缩短新功能的上市时间。

优势

zhuanlan.zhihu.com/p/451736630

  • 单一职责
  • 独立开发、部署、运维、扩缩容
  • 混合技术栈和混合部署方式

挑战

  • 性能: 分布式系统是跨进程、跨网络的调用,受网络延迟和带宽的影响。

  • 可靠性: 高度依赖于网络状况,任何一次的远程调用都有可能失败,随着服务的增多还会出现更多的潜在故障点。因此,如何提高系统的可靠性、降低因网络引起的故障率,是系统构建的一大挑战。

  • 数据一致性: 需要在 C(一致性)A(可用性)P(分区容错性) 三者之间做出权衡。

  • 运维成本 :运维主要包括配置、部署、监控与告警和日志收集四大方面。微服务架构中,每个服务都需要独立地配置、部署、监控和收集日志,成本呈指数级增长。

  • 链路测试

微服务核心组件

服务注册与发现

API 网关服务

分布式配置中心

服务通信

服务治理

  • 服务监控、日志收集
  • 分布式服务追踪
  • 扩容缩容

服务注册与发现

传统的服务发现方法,如 nacos, consul, etcd 或 zookeeper

nacos

developer.aliyun.com/article/848…

能做注册中心也能做配置中心

nacos之前公司做java服务的时候用的

Nacos注册中的交互流程 围绕服务实例注册、实例健康检查、服务实例获取这三个核心来实现的。

以Java版本的Nacos客户端为例,服务注册基本流程:

  • 服务实例启动将自身注册到Nacos注册中心,随后维持与注册中心的心跳;

  • 心跳维持策略为每5秒向Nacos Server发送一次心跳,并携带实例信息(服务名、实例IP、端口等);

  • Nacos Server也会向Client主动发起健康检查,支持TCP/Http;

  • 15秒内无心跳且健康检查失败则认为实例不健康,如果30秒内健康检查失败则剔除实例;

  • 服务消费者通过注册中心获取实例,并发起调用;

其中服务发现支持两种场景:第一,服务消费者直接向注册中心发送获取某服务实例的请求,注册中心返回所有可用实例,但一般不推荐此种方式;第二、服务消费者向注册中心订阅某服务,并提交一个监听器,当注册中心中服务发生变化时,监听器会收到通知,消费者更新本地服务实例列表,以保证所有的服务均可用。

discovery

Client-side discovery

image.png

Server-side discovery

image.png

  • benefits

    • Compared to client-side discovery, the client code is simpler since it does not have to deal with discovery. Instead, a client simply makes a request to the router
  • drawbacks

    • The router need to be replicated for availability and capacity.
    • The router must support the necessary communication protocols (e.g HTTP, gRPC, Thrift, etc) unless it is TCP-based router
    • More network hops are required than when using Client Side Discovery

Examples

An AWS Elastic Load Balancer (ELB) is an example of a server-side discovery router. A client makes HTTP(s) requests (or opens TCP connections) to the ELB, which load balances the traffic amongst a set of EC2 instances. An ELB can load balance either external traffic from the Internet or, when deployed in a VPC, load balance internal traffic. An ELB also functions as a [Service Registry]. EC2 instances are registered with the ELB either explicitly via an API call or automatically as part of an auto-scaling group.

Some clustering solutions such as [Kubernetes] and [Marathon]run a proxy on each host that functions as a server-side discovery router. In order to access a service, a client connects to the local proxy using the port assigned to that service. The proxy then forwards the request to a service instance running somewhere in the cluster.

registration

Self registration

On startup the service instance registers itself (host and IP address) with the service registry and makes itself available for discovery. The client must typically periodically renew its registration so that the registry knows it is still alive. On shutdown, the service instance unregisters itself from the service registry.

3rd party registration

A 3rd party registrar is responsible for registering and unregistering a service instance with the service registry. When the service instance starts up, the registrar registers the service instance with the service registry. When the service instance shuts downs, the registrar unregisters the service instance from the service registry.

Examples

  • [AWS Autoscaling Groups] automatically (un)registers EC2 instances with Elastic Load Balancer
  • Clustering frameworks such as [Kubernetes] (un)register service instances with the built-in/implicit registry

Health Check API

the service registry invokes a service instance’s health check API to verify that it is able to handle requests

API 网关服务

认证和授权, 路由 ,负载均衡, 重试、 降级, 熔断、日志、 监控、 链路追踪, 灰度发布,ABTesting 等功能。

配置中心

nacos能做注册中心也能做配置中心

服务通信

同步访问方式

rpc 、 rest (http)

异步访问方式

消息中间件:RabbitMQ、Kafka、RocketMQ之类

其他

  • 探活、负载均衡(轮询,加权轮询,ip_hash, least_conn)

  • 服务路由 所谓的路由规则,就是通过一定的规则如条件表达式或者正则表达式来限定服务节点的选择范围。

制定路由规则主要有两个原因。

  1. 业务存在灰度发布、多版本ABTesting的需求

  2. 多机房就近访问的需求(一般可以通过 IP 段规则来控制访问,在选择服务节点时,优先选择同一 IP 段的节点。这个也是算力靠近的优先原则。)

  • 服务容错 fail over, fail fast, fail back, fail safe

服务治理

服务监控

image.png 常见的开发监控报警技术有 ELK、InfluxData的TICK、Promethues 等。

日志收集

  • fluentd
  • grafana

服务追踪

服务追踪的原理主要包括下面两个关键点。

1、为了实现请求跟踪,当请求发送到分布式系统的入口端点时,服务跟踪框架为该请求创建一个唯一的跟踪标识 Trace ID,同时在分布式系统内部流转的时候,框架始终保持传递该唯一标识,直到返回给请求方为止。

通过 Trace ID 的记录,我们就能将所有请求过程的日志关联起来。

2、为了统计各处理单元的时间延迟,当请求到达各个服务组件时,或是处理逻辑到达某个状态时,也通过一个唯一标识 Span ID 来标记它的开始、具体过程以及结束。对于每个 Span 来说,它必须有开始和结束两个节点,

通过记录 Span开始 和结束 的时间戳,就能统计出该 Span 的时间延迟,除了时间戳记录之外,它还可以包含一些其他元数据,比如事件名称、请求信息等。

SkyWalking通过在应用程序中引入代理或探针来收集分布式系统的性能数据。这些代理可以以不同的形式存在,如Java Agent、Node.js Agent等,具体取决于应用程序的技术栈。

service-mesh 服务网格

服务网格从单个服务中移除控制服务间通信的逻辑,并将通信抽象到自己的基础设施层。它使用多个网络代理来路由和跟踪服务之间的通信。

代理充当组织网络和微服务之间的中间网关。所有进出该服务的流量都通过代理服务器路由。单个代理有时被称为 sidecar,因为它们是分开运行的,但在逻辑上位于每个服务旁边。这些代理一起构成了服务网格层

Istio

Istio 是一个开源服务网格项目,设计为主要与 Kubernetes 配合使用。Kubernetes 是一款开源容器编排平台,用于大规模部署和管理容器化应用程序。

Istio 的控制面板组件本身作为 Kubernetes 工作负载运行。它使用 Kubernetes 容器组(一组共享一个 IP 地址的紧密耦合的容器)作为 sidecar 代理设计的基础。

Istio 的第 7 层代理在与主服务相同的网络环境中作为另一个容器运行。从这个位置,它可以拦截、检查和操作所有通过容器组的网络流量。但是,主容器不需要任何改动,甚至不需要知道这种情况正在发生。