Nacos of Spring Cloud Alibaba

Introduction to Nacos

  • Nacos is a component of Spring Cloud Alibaba
  • Full name: Dynamic Naming and Configuration Service
  • In a simple sentence: Nacos=Eureka+Config+Bus

Nacos installation

  • After downloading, directly run startup.exe under the bin directory CMD, and then access http://ip:8848/nacos Login, default account password: Nacos
  • Standalone mode: startup cmd -m standalone

Nacos service registration

pom

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

Start class plus @ EnableDiscoveryClient

configuration file

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Configure Nacos address

management:
  endpoints:
    web:
      exposure:
        include: '*' #Exposure for monitoring

Nacos service configuration

pom

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

configuration file

  • Note that the file is called bootstrap YML (in fact, there can be two configuration files, one bootstrap for loading high priority configuration and one application for loading local configuration)
spring:
  profiles:
    active:
      dev
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos service registry address
      config:
        server-addr: localhost:8848 #Nacos as configuration center address
        file-extension: yaml #Specifies the configuration of yaml format
        group: DEV_GROUP #Profile group name
        namespace: 7d8f0f5a-6a53-4785-9686-dd460158e5d4

append notes to

  • Add @ RefreshScope to the class that reads the configuration file
@RestController
@RefreshScope //Support the dynamic refresh function of Nacos.
public class ConfigClientController
{
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo() {
        return configInfo;
    }
}

Create profile in configuration center

  • Each configuration file has a DataId in the form of ${prefix} - ${spring. Profiles. Active}$ {file-extension}
    • prefix defaults to spring application. Value of name
    • File extension is spring cloud. nacos. config. Value of file extension
    • For example: nacos-config-client-dev.yaml

As shown in the figure:

Relationship between Namespace, Group and DataId

  • Namespace is generally used to distinguish deployment environments (development, test and production), and Group is used to divide different microservices into the same Group
  • Of course, you can also distinguish deployment environments according to your own rules, such as DataId or Group (for example, you can create Nacos config client Dev and Nacos config client prod in the same Group on Nacos, and switch the corresponding configuration in the service to read them in real time. Or you can create different groups and modify the value of spring.cloud.nacos.config.group), Because there are few and simple services, there is no need to configure any Namespace
  • There are also such: Namespace corresponds to enterprise, Group corresponds to project, and DataID corresponds to environment
  • By default, Namespace=public, Group=DEFAULT_GROUP´╝îcluster=DEFAULT

Nacos supports switching between AP and CP modes

  • Generally speaking, if there is no need to store service level information, and the service instance is registered through Nacos client and can maintain heartbeat reporting, you can choose AP mode. The current mainstream services, such as spin cloud and Dubbo services, are applicable to the AP mode. The AP mode weakens the consistency for the possibility of service. Therefore, only temporary instances can be registered in the AP mode.
  • If you need to edit or store configuration information at the service level, CP is required, and K8S service and DNS service are applicable to CP mode. In CP mode, persistent instance registration is supported. At this time, the Raft protocol is used as the cluster operation mode. In this mode, the service must be registered before registering the instance. If the service does not exist, an error will be returned.

Nacos cluster

  • The cluster architecture of the official website is as follows. VIP refers to virtual IP, i.e. nginx (cluster)

Nacos data storage

  • After restarting Nacos, the previously configured information still exists, because Nacos uses embedded database (derby) to store data by default.
  • If you start multiple Nacos nodes in the default configuration, there is a consistency problem in the data storage.
  • In order to solve this problem, Nacos adopts centralized storage to support cluster deployment. At present, it only supports MySQL storage

Nacos cluster for Linux + MySQL production environment configuration

  • Note: at least 3 nacos can form a cluster
  • Nacos, MySQL and nginx need to be installed on Linux
  • The details are very simple, reference resources However, this article is a nacos cluster built on a single machine, which belongs to a pseudo cluster

Keywords: Java Back-end Spring Cloud

Added by juma929 on Wed, 16 Feb 2022 05:33:39 +0200