Make writing a habit together! This is the 8th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.

A catalog of articles in the microservices series

  • Deep into microservices -SpringBoot automatic assembly principle
  • Deep microservices -SpringCloud invokes the component Feign
  • Deep microservices – the foundation for service registration and discovery of SpringCloud Eureka
  • Deep microservices – service registration and discovery of SpringCloud Eureka’s high availability and core principles
  • In-depth Nacos foundation and Nacos Server construction of micro services
  • In-depth micro services -Nacos core concept and service discovery practice

preface

This series takes you deep into the basic usage and underlying principles of the various frameworks of the Microservices Spring architecture. The last article introduced the basic concepts of Nacos and the construction of Nacos Server. This section will take you to learn the core concepts of Nacos and the function of the client to realize service discovery


Nacos service discovery core concepts

Nacos architecture diagram

A Service is a software function or set of software functions (such as the retrieval of specific information or the execution of a set of operations) that can be reused by different clients for different purposes (such as through cross-process network calls)

A Service Registry is a database of services, instances, and metadata. The underlying layer is Map(Namespace, Map(Group ::serviceName, Service)). Service instances are registered with the Service registry when they are started and deregistered when they are shut down. Clients of services and routers query the service registry to find available instances of services. The service registry may invoke the health check API of the service instance to verify that it can handle the request.

Service Metadata Service Metadata refers to data that describes services, including endpoints, Service labels, Service versions, Service instance weights, routing rules, and security policies

Service providers are applications that provide reusable and callable services

A Service Consumer is an application that initiates a call to a Service

Naming Service Providing mapping management Service between the name of all objects and entities in a distributed system and the associated metadata. For example, ServiceName -> Endpoints Info, Distributed Lock Name -> Lock Owner/Status Info, DNS Domain Name -> IP List, Service discovery and DNS are two scenarios of name services


Nacos data model

Nacos data model keys consist of three parts: Namespace defaults to an empty string, public Namespace (public), and group defaults to DEFAULT_GROUP

See the com. Alibaba. Cloud. Nacos. NacosConfigProperties

/** * namespace, separation configuration of different environments. */
	private String namespace;
/** * nacos config group, group is config data meta info. */
	private String group = "DEFAULT_GROUP";
Copy the code
spring:
  application:
    name: AppName
  cloud:
    nacos:
      config:
        server-addr: nacos:8848
        namespace: namespaceId # namespace id
        group: GroupName Default group name is DEFAULT_GROUP
      discovery:
        server-addr: nacos:8848
Copy the code

Nacos namespace interface, support to add, delete, edit and other operations

Nacos data model

Nacos service domain model

Nacos supports data storage and management of services in all scenarios. The service domain model of Nacos is a three-tier service-cluster-instance model.

Nacos data logical isolation model

A user account can create multiple namespaces, each corresponding to a client instance. The corresponding registry Namespace physical cluster can be routed according to the rules in the actual project of production can be environment according to the Namespace isolation (master/develop/test/hotfix), the application can be according to the different packet switching configuration, meet the demand of the actual scene service isolation

Nacos service discovery principle

The core concepts of Nacos service discovery:

  • The service registry: When Nacos Client is started, it will send REST request (1.0 default HTTP,2.0 GRPC) to register service with Nacos Server and provide metadata information, including alias, host, port and other information of the service. NacosServer stores instance information in its own registry when it receives a Client registration requestMap)
  • Service heartbeat: After Nacos Client is started, it will start a scheduled task to send heartbeat notifications to Nacos Server at regular intervals (by default, heartbeat notifications are sent every 5 seconds) to prevent its own services from being excluded
  • Service discovery: When making service invocation, Nacos Client will send REST request to Nacos Server to pull the service list and cache it locally. In order to prevent the cache from being inconsistent with the actual service data, The Nacos Client starts a scheduled task to pull the latest registry information updates from the Nacos Server to the local cache
  • Service health check: The Nacos Server starts a scheduled task to check the health status of the Nacos Client. If the Server does not receive a heartbeat event from the Client within 15 seconds, the health property of the instance is set to false. If the instance does not receive a heartbeat event within 30 seconds, The instance is removed from the Nacos Server registry

The health property of the instance is Nacos Client health status, which defaults to true

The above mechanism will be explained in detail in the subsequent source code parsing

Nacos services discover the actual combat

1. Introduce the Maven

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Copy the code

2. Start class annotated @enableDiscoveryClient

@SpringBootApplication
@EnableFeignClients({ "com.jany"}) @EnableDiscoveryClient public class NacosClientApplication { public static void main(String[] args) { SpringApplication.run(NacosClientApplication.class, args); }}Copy the code

3. Configure the bootstrap.yml file

Change the nacOS as required to nacOS Server address Username is nacos by default, and the password is nacos by default

spring:
  application:
    name: nacos-client-demo
  cloud:
    nacos:
      discovery:
        server-addr: nacos:8848
        username: nacos
        password: nacos
Copy the code

4. Registered services are visible on the Nacos console after startup

conclusion

This article mainly introduces the core concept of Nacos and the practice of service discovery. I hope you can support it and I will explain Nacos in more depth in the future.