Make writing a habit together! This is the 7th 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

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 high availability and core principles of SpringCloud Eureka. This section will take you to learn the basic concepts of Nacos and the construction of Nacos Server


What is Nacos?

Nacos can discover, configure and manage microservices in the microservice ecosystem, and realize dynamic service discovery, service configuration, service metadata and traffic management

Key features of Nacos

  • Service discovery and service health monitoring
    • Nacos supports DNs-based and RPC-based service discovery
    • Nacos provides real-time health checks on services to prevent requests from being sent to unhealthy hosts or service instances
  • Dynamically configured service
    • Nacos manages application configuration and service configuration for all environments in a centralized, external, and dynamic manner
    • Nacos provides configuration management features including configuration version tracking, Canary publishing, one-click rollback configuration, and client configuration update status updates
  • Dynamic DNS Service
    • The dynamic DNS service supports weighted routing
  • Services and their metadata management
    • Nacos supports all services and metadata in the data center, including management service descriptions, life cycles, static dependency analysis of services, health of services, traffic management of services, routing and security policies, SLA of services, and most importantly metrics statistics

Nacos ecological

Currently, Nacos supports a variety of ecosystems, such as SpringCloud, Dubbo, Cloud Native and so on

Nacos Server set up

Version 1.4.2 is available for download

The directory after downloading

Nacos supports three deployment modes

  • Single-machine mode – Used for testing and single-machine trial
  • Cluster mode – Used in production environments to ensure high availability
  • Multi-cluster mode – Used in multi-DATA center scenarios

Single-machine mode startup

  • Linux/Unix

bin/startup.sh -m standalone

  • Windows

cmd startup.cmd -m standalone

Console OutputAccess management terminalXx.xx.xx. xx:8848, the account/password is nacos/na…

Port 8848 is the default port number. You can modify the server.port property in the application

Nacos management interface

Single-machine mode supports mysql

The default Nacos uses an embedded database for data storage, and mysql data source capability is supported after version 0.7

  1. The database version must be 5.6.5+
  2. Initialize the mysql database with the database initialization file nacos-mysql.sql
  3. Modify the conf/application. The properties files, increase support for mysql data source configuration (currently only supports mysql), add a mysql data source url, user name and password
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
 spring.datasource.platform=mysql

### Count of DB:
 db.num=1

### Connect URL of DB:
 Db. Url. 0 = JDBC: mysql: / / 127.0.0.1:3306 / nacos? characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTime zone=UTC
 db.user.0=nacos
 db.password.0=nacos
Copy the code

Then start NACOS in single-machine mode. All data written by NACOS to embedded database is written into mysql NACOS table as follows

K8s builds single NACOS

---
apiVersion: v1
kind: Service
metadata:
  name: nacos
  namespace: #k8s resource space
spec:
  externalIPs:
    - xx.xx.xx.xx
  selector:
    app: nacos
  ports:
    - name: svc-port
      port: 8848
      targetPort: 8848

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nacos
  namespace: #k8s resource space
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nacos
  template:
    metadata:
      labels:
        app: nacos
    spec:
      volumes:
        - name: localtime
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
        - name: timezone
          hostPath:
            path: /etc/timezone
      imagePullSecrets:
        - name: harborsecret
      containers:
        - name: nacos
          image: Nacos - server: 1.4.2
          imagePullPolicy: Always
          resources:
            limits:
              cpu: "2"
              memory: 2G
            requests:
              cpu: 500m
              memory: 500Mi
          env:
            # Run profile
            - name: MODE
              value: standalone ## Cluster mode
            - name: SPRING_DATASOURCE_PLATFORM
              value: mysql # # the data source
            - name: MYSQL_SERVICE_HOST
              value: '127.0.0.1' # # MySQL address
            - name: MYSQL_SERVICE_DB_NAME
              value: nacos  	MySQL Nacos library name
            - name: MYSQL_SERVICE_USER
              value: root		MySQL Nacos account
            - name: MYSQL_SERVICE_PASSWORD
              value: 'root'		MySQL Nacos password
            - name: NACOS_AUTH_ENABLE
              value: 'true'		## Enable authentication
            - name: NACOS_AUTH_CACHE_ENABLE
              value: 'true'		Authentication cache is enabled
            - name: NACOS_AUTH_IDENTITY_KEY  # configure the key for custom identification
              value: 'xxx_key'	Authentication cache is enabled
            - name: NACOS_AUTH_IDENTITY_VALUE 
              value: 'xxx_value'  ## Configure the value of the custom identity
          volumeMounts:
            - name: localtime
              mountPath: /etc/localtime
            - name: timezone
              mountPath: /etc/timezone
Copy the code