In-depth Microservices-Nacos Core Concepts and Service Discovery Practice

Get into the habit of writing together! This is the 8th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Microservices Series Article Directory


foreword

This series takes you deep into the basic use and underlying principles of each framework of the microservice Spring system. The previous 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 DiagramNacos Architecture.png

Service A service refers to a software function or set of functions (such as the retrieval of specific information or the execution of a set of operations) that is intended to be reused by different clients for different purposes (such as through network calls across processes)

Service Registry The Service Registry is a database of services, their instances and metadata. Under the hood Map(namespace, Map(group::serviceName, Service)), service instances are registered with the service registry at startup and deregistered at shutdown. Clients of services and routers query the service registry to find available instances of the service. The service registry may call the service instance's health check API to verify that it can handle requests.

Service Metadata Service Metadata refers to data describing services including service endpoints, service labels, service version numbers, service instance weights, routing rules, security policies, etc.

Service Provider refers to the application side that provides reusable and callable services

服务消费方 (Service Consumer) 是指会发起对某个服务调用的应用方

名字服务 (Naming Service) 提供分布式系统中所有对象(Object)、实体(Entity)的“名字”到关联的元数据之间的映射管理服务,例如 ServiceName -> Endpoints Info, Distributed Lock Name -> Lock Owner/Status Info, DNS Domain Name -> IP List, 服务发现和 DNS 就是名字服务的2大场景


Nacos 数据模型

Nacos 数据模型 Key 有三部分组成,Namespace默认是空串,公共命名空间(public),分组默认是 DEFAULT_GROUP

详见 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";
复制代码
spring:
  application:
    name: AppName
  cloud:
    nacos:
      config:
        server-addr: nacos:8848
        namespace: namespaceId # 命名空间id
        group: GroupName # 	分组名称 默认为DEFAULT_GROUP
      discovery:
        server-addr: nacos:8848
复制代码

Nacos 命名空间界面,支持新增,删除,编辑等操作

Nacos namespace.png

Nacos 数据模型 Nacos data model.jpeg

Nacos 服务领域模型

Nacos支持服务在所有场景下的数据存储和管理,Nacos 的服务领域模型为 服务-集群-实例的三层模型。

Nacos service domain model.jpeg

Nacos 数据逻辑隔离模型

⼀个用户账号可以新建多个命名空间,每个命名空间对应⼀个客户端实例,这个命名空间对应的注册中心物理集群是可以根据规则进行路由的 实际项目生产中可根据Namespace进行环境隔离(master/develop/test/hotfix),应用可以根据不同的分组进行切换配置,满足实际场景的服务隔离需求

Nacos data logic isolation model.png

Nacos 服务发现原理

Nacos service registration discovery principle.png Nacos服务发现核心概念:

  • 服务注册:Nacos Client 启动的时候会发送REST 请求(1.0默认为http,2.0为grpc)向Nacos Server注册服务并提供元数据信息,包含服务的别名、host、端口等信息。NacosServer接收到Client注册请求会讲实例信息存储到自身的注册表(底层为Map)中
  • 服务心跳:Nacos Client启动之后会启动一个定时任务每隔一段时间进行心跳通知Nacos Server(默认5s发送一次心跳),防止自身服务被剔除
  • 服务发现:Nacos Client在进行服务调用时会向Nacos Server发起REST请求拉取服务列表,并缓存到本地,为防止缓存与实际服务数据不一致,Nacos Client会启动一个定时任务从Nacos Server拉取最新的注册表信息更新到本地缓存
  • 服务健康检查:Nacos Server会启动一个定时任务检测Nacos Client的健康状态,若15s内没有收到客户端的心跳事件,会将实例的health属性设置为false,如果实例超过30s没有收到心跳事件,会从Nacos Server注册表中剔除该实例

实例的health属性为Nacos Client 健康状态,默认为true

上述的机制会在后续的源码解析详细讲解

Nacos 服务发现实战

1.引入Maven

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
复制代码

2.启动类加上注解@EnableDiscoveryClient

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

}

复制代码

3.配置文件bootstrap.yml

根据实际情况更改 nacos 为Nacos Server地址 username默认为nacos,密码默认为nacos

spring:
  application:
    name: nacos-client-demo
  cloud:
    nacos:
      discovery:
        server-addr: nacos:8848
        username: nacos
        password: nacos
复制代码

4.启动之后可在Nacos控制台看到注册上来的服务

Nacos registration to discover actual combat.png

总结

This article mainly introduces the core concepts of Nacos and the actual combat of service discovery. I hope for more support, and I will explain the content of Nacos in more depth in the future.

Guess you like

Origin juejin.im/post/7085549819183235086