This is the third day of my participation in the First Challenge 2022

  • Micro services series: Nacos registry for Spring Cloud Alibaba
  • Micro service series: Nacos configuration Center for Spring Cloud Alibaba

The basics of Nacos have been covered in the previous two articles.

Nacos supports three deployment modes

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

Today we will start building a Nacos cluster. Without further ado, let’s begin today’s lesson.

What is a cluster

Clustering is to replicate applications into multiple identical applications to work together to improve working capacity. That is, multiple applications are scattered across different servers, each running the same code independently. It can distribute server pressure to solve the problem of high concurrency and prevent a single node failure. That is, the failure of one server does not affect the normal running of other servers.

Cluster Deployment Architecture

The schematic diagram of nacOS official document cluster structure is as follows:

Where SLB, we can use Nginx, this article is just for learning, so we use IP direct connection

# Spring
spring:
  application:
    # app name
    name: cloud-nacos-provider
  profiles:
    # Environment configuration
    active: dev
  cloud:
    nacos:
      discovery:
        # service registered address
        server-addr: 192.168. 0109.: 8848192168 0.109:18848192168 0.109:28848
Copy the code

The cluster structures,

In order to facilitate the use of my own computer to start the Nacos instances of different ports to simulate the Nacos cluster (PS: but in fact, it is not as convenient as multi-machine deployment).

1. Make two copies firstNacos

2, modify the two copies of the copyNacosport

In/conf/application. The properties file to modify server port for 18848 or 28848

Server. The port = 18848 or server. The port = 28848

3, modify,cluster.conffile

In the conf directory of the three Nacos directories, there is the cluster.conf.example file.

Modify the cluster.conf file

The IP here is the IP of my own computer and I find it problematic to use 127.0.0.1.

4. Use external configurationMySQLThe data source

Modify the conf/application. The properties file

Microservices: Nacos Configuration Center for Spring Cloud Alibaba is explained in detail in this article.

You may ask why use an external MySQL data source, but not an embedded database?

This really doesn’t work, because the data is stored in an embedded database, and each Nacos service in the cluster has its own built-in database, so the data storage is inconsistent.

To address this issue, Nacos has adopted a centralized storage approach to support clustered deployment and currently only supports MySQL storage.

When I simulate cluster environment this time, I also found a problem. The pseudo cluster in this single machine deployment leads to data store path conflict, the startup will fail, and the message “Starting… Viewing the startup log is indeed an error. This article gives a detailed explanation: solve the problem of single-machine multi-instance deployment of built-in data sources in cluster mode

It is best to use external storage for cluster deployment.

5. Start 3Nacosservice

startup.cmd

View the node list managed by the three console clusters

test

1. Direct IP connection mode

server:
  port: 9201

# Spring
spring:
  application:
    # app name
    name: cloud-nacos-provider
  profiles:
    # Environment configuration
    active: dev
  cloud:
    nacos:
      discovery:
        # service registered address
        server-addr: 192.168. 0109.: 8848192168 0.109:18848192168 0.109:28848
      config:
        Configure the center address
        server-addr: 192.168. 0109.: 8848192168 0.109:18848192168 0.109:28848
        Configuration file format
        file-extension: yml
        # Share configuration
        shared-configs:
        - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
Copy the code

Or articles before using the program, started to http://localhost:9201/config/get

No problem!

At this point, we manually shut down one of the Nacos services to simulate the failure of one of the services

Visit http://localhost:9201/config/get again, no problem!

2. Nginx forwarding

Start a server 192.168.10.100, install Nginx, and configure proxy forwarding rules.

#gzip on; Upstream {server 192.168.0.109:8848; Server 192.168.0.109:18848; Server 192.168.0.109:28848; } server { listen 80; server_name localhost; location / { proxy_pass http://nacos; }}Copy the code

bootstrap.yml

spring:
  application:
    # app name
    name: cloud-nacos-provider
  profiles:
    # Environment configuration
    active: dev
  cloud:
    nacos:
      discovery:
        # service registered address
        server-addr: 192.16810.100.: 80   # Nacos server address, clustered version of Nginx forward
      config:
        Configure the center address
        server-addr: 192.16810.100.: 80   # Nacos server address, clustered version of Nginx forward
        Configuration file format
        file-extension: yml
        # Share configuration
        shared-configs:
        - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
Copy the code

Visit http://192.168.10.100:80/nacos/index.html to enter Nacos console.

This concludes the article. At the same time, our Nacos series is over, and we’ll start the next one.

PS: Now that you’ve seen it, give it a thumbs up, Daniel!