BlessingCR’s Blog
BlessingCR’s Blog

Nacos

Nacos
nacos 是一个注册中心和配置中心一体化的中间件
支持 CP ,AP 模式   Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可得兼
  • 1、服务发现和服务健康监测。
Nacos支持基于DNS和RPC的服务发现,即服务消费者可以使用DNS或者HTTP的方式来查找和发现服务。
Nacos提供对服务的实时的健康检查,阻止向不健康的主机或者服务实例发送请求。Nacos支持传输层(Ping/TCP)、应用层(HTTP、Mysql)的健康检查。
  • 2、动态配置服务。
动态配置服务可以以中心化、外部化和动态化的方式管理所有环境的应用配置和服务配置。
  • 3、动态DNS服务。
支持权重路由,让开发者更容易的实现中间层的负载均衡、更灵活的路由策略、流量控制以及DNS解析服务。
  • 4、服务和元数据管理。
Nacos允许开发者从微服务平台建设的视角来管理数据中心的所有服务和元数据。如:服务的生命周期、静态依赖分析、服务的健康状态、服务的流量管理、路由和安全策略等。
NamingService
Nacos为服务注册与发现提供了一个SDK类 NamingService,通过该类,可以实现服务的注册与发现、订阅服务的动态变化、获取服务实例、获取注册中心的健康状态等等。
其中,NamingService中的接口,可以分为以下几类:
namingService.registerInstance():注册实例
namingService.deregisterInstance():取消注册
namingService.getAllInstances(String serviceName):获取服务的所有实例
namingService.getServicesOfServer(int pageNo, int pageSize):分页查询,从服务器获取所有服务名称
namingService.getSubscribeServices():获取当前客户端所有订阅的服务
namingService.selectInstances():根据条件获取服务实例
namingService.selectOneHealthyInstance():根据条件选择一个健康的实例
namingService.subscribe():订阅服务以接收实例更改事件
namingService.unsubscribe():取消订阅
namingService.getServerStatus():获取服务器健康状态
https://blessingcr.com/wp-content/uploads/2023/03/ReXC8oNyyXUMGIPCVP0FloCp-gvJpk0-GwszNfBfeEI.png
1.注册流程
服务提供者向nacos server发起服务注册前,先向nacos server建立起心跳检测机制,nacos server那边也有一个心跳检测,服务提供者不停的向nacos server发起心跳检测,告知自己的健康状态,nacos server发现该服务心跳检测时间超时会发布超时事件来告知服务消费者。
https://blessingcr.com/wp-content/uploads/2023/03/gguym9cNyZoOrIQOjn_hZcNPrxhi4YXJ99vPYvF0DRg.png
2.服务发现
服务发现由NacosWatch完成,它实现了Spring的Lifecycle接口,容器启动和销毁时会调用对应的start()和stop()方法。
大致流程:nacos client这边在spring容器启动后执行一个服务订阅操作的延时任务,这个任务执行时先拉取nacos server那边最新的服务列表,然后与本地缓存的服务列表进行比较,取消订阅下线的服务,然后每隔30秒向nacos server发起订阅操作,订阅所有服务。
服务消费者如何实时感知服务提供者的状态信息呢?
https://blessingcr.com/wp-content/uploads/2023/03/sCouvzmjUffU68nm_UBVqVSf8aS0x6jIB_fhHnTiD20.png
  • 1、服务消费者订阅后会执行一个轮询任务(每10s执行一次)用来拉取最新的服务提供者信息并实时更新,实现在HostReactor中的UpdateTask完成,下面来看代码
  • 2、上面服务注册时说过,服务提供者注册时nacos服务端也有一个相应的心跳检测,当心跳检测超时也就是未及时收到服务提供者的心跳包,nacos server判定该服务状态异常,随后通过UDP推送服务信息用来告知对应服务消费者,服务消费者通过PushReceiver来处理udp协议,HostReactor.processServiceJson(String json)来更新本地服务列表。

回复 Gricelda Baison 取消回复

textsms
account_circle
email

  • This is the fitting weblog for anyone who needs to find out about this topic. You notice so much its nearly onerous to argue with you (not that I actually would need…HaHa). You positively put a new spin on a topic thats been written about for years. Great stuff, simply nice!

    1 年前 回复
  • You actually make it seem so easy with your presentation however I in finding this matter to be actually something which I feel I’d never understand. It seems too complicated and extremely vast for me. I am having a look forward on your subsequent put up, I’ll attempt to get the cling of it!

    11 月前 回复

BlessingCR’s Blog

Nacos
Nacos nacos 是一个注册中心和配置中心一体化的中间件 支持 CP ,AP 模式   Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可得兼 …
扫描二维码继续阅读
2023-03-06