当前位置:网站首页>[Alibaba middleware technology series] "Nacos technology" service registration and discovery related principle analysis

[Alibaba middleware technology series] "Nacos technology" service registration and discovery related principle analysis

2022-06-23 03:09:00 Haoyu Tianshang

Background introduction

The previous articles introduced Nacos Configure the capability mechanism of the central service , Next , Let's introduce Nacos Another very important feature is service registration and discovery , When it comes to service registration and discovery, I believe everyone should be familiar with it , In today's Micro service era , Service is very important , And in the Nacos Chinese service is called his first-class citizen .Nacos Support almost all major types of “ service ” Discovery 、 Configure and manage .

service (Service)

Service refers to one or a group of software functions ( For example, retrieval of specific information or execution of a group of operations ), Its purpose is that different clients can be reused for different purposes ( For example, through cross process network call ).Nacos Support mainstream service Ecology , Such as Kubernetes Service、gRPC|Dubbo RPC Service perhaps Spring Cloud RESTful Service.

nacos As shown in the architecture diagram of

(dubbo) RPC The infrastructure of microservices

In the picture 6 The meaning of the steps is explained as follows :

  1. The service container is responsible for starting , load , Run the service provider .
  2. The service provider at startup , Register your services with the registry .
  3. Service consumers at startup , Subscribe to the registry for the services you need .
  4. The registry returns a list of service provider addresses to the consumer , If there are changes , The registry pushes the change data to the consumer based on the long connection .
  5. Serving consumers , From the provider address list , Based on soft load balancing algorithm , Select a provider to invoke , If the call fails , Select another call .
  6. Service consumers and providers , Accumulates the number of calls and the call time in memory , Send statistics to the monitoring center every minute on a regular basis .

Service registry (Service Registry)

Above picture Registry It's the registry , Responsible for service registration and discovery ,Dubbo Before the service system Zookeeper Or your own Registry Realization , and Nacos It's another kind of Registry The implementation of the .

Service registry , It's a service , Examples and metadata databases (Dubbo3 The source data center has been 、 The configuration services are all extracted and independent ), The service instance is registered to the service registry at startup , And log off on shutdown .

Clients of services and routers query the service registry for available instances of services , The service registry may call the health check of the service instance API To verify that it can handle requests .

Service Metadata (Service Metadata)

Service metadata refers to including service endpoints (endpoints)、 Service Tag 、 Service version number 、 Service instance weight 、 Routing rules 、 Security policy and other data describing the service .

Introduction to logical architecture and its components

  • Service management : Implementation services CRUD, domain name CRUD, Service health check , Service weight management and other functions
  • Configuration Management : Implement configuration management CRUD, version management , Gray management , Monitoring management , Push trajectory , Aggregate data and other functions
  • Metadata management : Provide metadata CURD And marking ability
  • Plug-in mechanism : Realize the ability of three modules to be separated and combined , Implementation extension point SPI Mechanism
  • Event mechanism : Implement asynchronous event notification ,sdk Data change, asynchronous notification, etc
  • Log module : Manage log classification , The level of logging , Log portability ( Especially avoiding conflict ), Log format , Abnormal code + Help document
  • Callback mechanism :sdk Notification data , Call back the user through a unified mode . Interfaces and data structures need to be scalable
  • Addressing mode : solve ip, domain name ,nameserver、 Broadcast and other addressing modes , Need to be scalable
  • Push channel : solve server With storage 、server between 、server And sdk The problem of inter push performance
  • Capacity management : Manage every tenant , The capacity under the packet , Prevent the storage from being written explosion , Affect service availability
  • Traffic management : According to the tenant , Grouping and other dimensions on the request frequency , Number of long links , Message size , Request flow control to control
  • Caching mechanisms : Disaster recovery Directory , Local cache ,server Caching mechanisms . Disaster recovery directory needs tools
  • Boot mode : In stand-alone mode , Configuration mode , Service mode ,dns Pattern , perhaps all Pattern , Start different programs +UI
  • Agreement of conformity : Solve different data , In the case of different consistency requirements , Different consistency mechanisms
  • Storage module : Solve data persistence 、 Non persistent storage , Solve the problem of data fragmentation
  • Nameserver: solve namespace To clusterid The routing problem of , Solve the problem of user environment and nacos Physical environment mapping problem
  • CMDB: Solve metadata storage , With the three parties cmdb System docking problem , Solution application , people , Resource relations
  • Metrics: Exposure criteria metrics data , It is convenient to connect with the three party monitoring system
  • Trace: Exposure criteria trace, Convenience and SLA Get the system through , The journal is white and flat , Push trajectory, etc , And it can connect with the metering and billing system
  • Access management : It's equivalent to Alibaba cloud opening service , Assign identities 、 Capacity 、 Permission process
  • User management : Solve user management , Sign in ,sso Other questions
  • Rights management : Solve identification , Access control , Role management and so on
  • Audit system : Expand the interface to facilitate access to audit systems of different companies
  • Notification system : Core data changes , Or operation , Convenient passage SMS Get the system through , Inform the corresponding person of the data change
  • OpenAPI: Exposure criteria Rest style HTTP Interface , Simple and easy to use , Facilitate multilingual Integration
  • Console: Easy to use console , Do service management 、 Configuration management and other operations
  • SDK: Multilingual sdk
  • Agent:dns-f Similar patterns , Or with mesh And so on
  • CLI: Command line lightweight management of products , image git As easy to use

Nacos Service registration and discovery

service provider (Service Provider)

Refers to the application side that provides reusable and callable services .

Simulate service registration

The most important thing of service registration is where to register the service , At the registry server , There must be a container for managing services , He keeps instances of all services . You don't need to know the specific implementation details of the container , Just know that there is such a concept .

  • Register two instances of the same service to Nacos in :
  • Dual registration mode injection into
  • Keep the service online
demo The code is as follows :

adopt NamingService Interface registerInstance Method to register the service , This method has many overloaded methods , Here we choose a simple one to call . After registration , By calling getAllInstances Method , Get all available instances now , Then let the main thread wait , Print as follows :

You can find naming The client successfully obtained two instances .

Service consumer (Service Consumer)

After the service is registered in the registry , Consumers of services can carry out the process of service discovery , Consumers can send a request to get a service instance directly to the registry , In this case, the registry will return all available service instances to the consumer , But this is generally not recommended . Another way is for consumers of services to subscribe to a service in the registry , And submit a listener , When the service in the registry changes , The monitor will be notified , At this time, the consumer updates the local service instance list , To ensure that all services are available .

Nacos Consumer service mechanism

It refers to the application party that will initiate a service call . After the service is registered , Service consumers can subscribe to the registry for the services they need , The registry will list all instances of the service “ push ” To the consumer , In fact, the client actively polls to obtain the service , The principle is the same as that for the client to obtain the configuration items of the configuration center .

Now I create a service consumer , Then subscribe to a service in the registry , After receiving the service list returned by the registry , perform 5 Time select Operation of service instance , It is equivalent to a simulated service request , The specific code is shown in the figure below :

Among them printInstances The main method is to print out the instances of all services , take ServiceConsumer After the class is started , Print out the following logs :

Nacos Mechanism load balancing

There are many ways to implement load balancing , Including polling method , Random method , Request for ip do hash Post mold taking, etc , From the perspective of load, it can be divided into : Server load balancing and client load balancing .Nacos After the client of gets the complete instance list of the service , Load balancing algorithm will be performed on the client to obtain an available instance , The pattern uses random access .

Nacos The complete process of service registration and subscription

  • Nacos There are two parts for the client to register the service , One is to register the service information to the server , The other is to send heartbeat packets to the server , Both operations are performed through NamingProxy Data interaction with the server .
  • Nacos There are also two parts when the client subscribes to the service , One is the regular task of constantly querying available service instances from the server , The other is to constantly take out services from the changed service queue and notify EventListener Holder's scheduled task , Update service subscription list .

Reference material

https://nacos.io/zh-cn/docs/feature-list.html

原网站

版权声明
本文为[Haoyu Tianshang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201221716141433.html