Spring Cloud Alibaba: building a Nacos cluster

Spring Cloud Alibaba: building a Nacos cluster

Bloggers are going to build a cluster composed of three Nacos nodes. First, they need to create three virtual machines. The system is CentOS7. Each virtual machine has the same configuration, 2G memory and 8G disk. How to create a virtual machine please refer to this blog:

You also need to use XShell to connect the virtual machine. After all, it is troublesome to operate the virtual machine on the VirtualBox. First use VirtualBox to enter the virtual machine, query the IP address of the virtual machine (use ip addr or ifconfig command), and then use XShell to connect it.

Static IP address

In order to avoid the trouble caused by dynamic allocation of virtual machine IP address, set the IP address of each virtual machine as a static IP address, so that when you use XShell to connect the virtual machine in the future, you don't need to use VirtualBox to enter the virtual machine every time to query the newly allocated IP address.

cd /etc/sysconfig/network-scripts/


The configuration file corresponds to the network card name of the virtual machine, as shown in the following figure:

Install vim in the virtual machine to edit these configuration files:

yum install -y vim

Modify the configuration file and set the static IP address:

vim ifcfg-enp0s3

BOOTPROTO="static"     # Static IP address, which is dynamically assigned by default
ONBOOT="yes"           # Power on enable
IPADDR=192.168.1.195   # Set static IP address
NETMASK=255.255.255.0  # Subnet mask
GATEWAY=192.168.1.1    # gateway
DNS1=192.168.1.1       # DNS server

The IP address is set to the IP address queried by the virtual machine using VirtualBox. The subnet mask is generally 24 bits (255.255.255.0, the same as the local computer). For simplicity, set GATEWAY and DNS to the GATEWAY of the local computer (Windows10):

ipconfig


After setting the static IP addresses of the three virtual machines, you can shut down the virtual machines on the VirtualBox, start these virtual machines in the no interface startup mode, and then connect with XShell.

Build Nacos cluster

Bloggers will use shell scripts to build Nacos clusters (the script flow is described with echo command, and slight modifications can also be used to create stand-alone Nacos services):

#!/bin/bash

# author: itkaven
# input: nacos_version ip1 ip2 ip3 port  

jdk="jdk1.8.0_202"

if [ $# -ne "5" ]; then
    echo "Please enter in sequence nacos Version number of the three virtual machines IP Address and startup port (specify one)!"
else
    if [ -e "/usr/local/nacos" ]; then
        echo "nacos File already exists!"
    else
        echo "nacos File does not exist, waiting for download!"
        yum install -y wget
        echo "Ready from Github download nacos Compressed package(The download may fail due to network reasons. Just re execute the script)!"
        wget -P "/usr/local" --no-check-certificate "https://github.com/alibaba/nacos/releases/download/$1/nacos-server-$1.tar.gz"
        echo "nacos Compressed package download completed!"
        echo "Start decompression nacos Compressed package!"
        tar -zxvf "/usr/local/nacos-server-$1.tar.gz" -C "/usr/local"
        echo "delete nacos Compressed package"
        rm -f "/usr/local/nacos-server-$1.tar.gz"
    fi
    cd "/usr/local/nacos/conf"
    echo "modify nacos of application.properties(Configure according to the port entered by the user nacos (run on specified port)!"
    sed -i "s#server.port=.*#server.port=$5#g" application.properties
    echo "add to nacos Cluster configuration (based on the number of three virtual machines entered by the user) IP Address and running port)!"
    echo -e "$2:$5\n$3:$5\n$4:$5" > ./cluster.conf
    if [ ! -e "/usr/local/$jdk" ]; then
        echo "not installed JDK´╝îPrepare for installation JDK($jdk)!"
        if [ ! -e "/usr/local/jdk-8u202-linux-x64.tar.gz" ]; then
            echo "Please upload JDK Compressed package!"
        else
            echo "JDK The compressed package has been uploaded!"
            echo "Start decompression!"
            tar -zxvf "/usr/local/jdk-8u202-linux-x64.tar.gz" -C "/usr/local"
            echo "Delete the compressed package!"
            rm -f "/usr/local/jdk-8u202-linux-x64.tar.gz"
            echo "to configure JDK Environment variable!"
            echo "export JAVA_HOME=/usr/local/jdk1.8.0_202" >> /etc/profile
            source /etc/profile
            echo "export CLASSPATH=.:$JAVA_HOME/lib/" >> /etc/profile
            echo "export PATH=$PATH:$JAVA_HOME/bin" >> /etc/profile
            source /etc/profile
        fi
    else
        echo "Already installed JDK($jdk)!"
    fi
    echo "For convenience, turn off the firewall and let nacos The cluster can communicate, nacos Multiple ports will be used for communication!"
    systemctl stop firewalld
    echo "Start in cluster mode nacos(Use built-in data sources)!"
    cd "/usr/local/nacos/bin"
    ./startup.sh -p embedded
fi

Intent of the script:

  • To install JDK, you need to put the JDK compressed package under the / usr/local / path of the virtual machine in advance. Bloggers don't like to install JDK using yum, but downloading the JDK compressed package using wget command will get a defective package, because Oracle has official verification; Therefore, bloggers adopt a compromise approach. They upload JDK compressed packages themselves, and decompress and environment variables through scripts.
  • Download Nacos (from Github), decompress it, modify the server.port in the default configuration file application.properties to the specified port, and write the cluster configuration into the configuration file cluster.conf (according to the specified IP addresses and startup ports of the three virtual machines).
  • Turn off the firewall (to facilitate the communication between Nacos clusters), start Nacos in a cluster mode, and use the built-in data source. In fact, bloggers are lazy and don't want to build a Mysql Cluster, and then supplement it.

Put the downloaded JDK compressed package under the / usr/local / path of each virtual machine (if the JDK version is different from the blogger, the script needs to be modified):

For the stand-alone use of Nacos, please refer to this blog:

In fact, the building methods of clusters and stand-alone are similar, except that to build a Nacos cluster, you need to modify the cluster configuration file (cluster.conf) and run Nacos in cluster mode (the default mode is cluster operation, and - m standalone needs to be added for stand-alone operation). Therefore, the difference between cluster and stand-alone construction methods lies in these two lines:

echo -e "$2:$5\n$3:$5\n$4:$5" > ./cluster.conf
./startup.sh -p embedded

First copy the shell script to three virtual machines.

vim nacos_server.sh

Save and exit:

Modify Nacos_ Permission of server.sh script (otherwise, you have no permission to run):

chmod 700 nacos_server.sh

Run nacos_server.sh script (re run when an error is reported, which is generally a network problem. After all, the blogger basically stepped on the pit):

./nacos_server.sh 1.4.2 192.168.1.195 192.168.1.196 192.168.1.197 9000

1.4.2 is the version number of Nacos. The three IP addresses are the IP addresses of the three virtual machines, and Nacos is run on the 9000 port of the virtual machine (the parameter sequence is fixed).

192.168.1.195
192.168.1.196
192.168.1.197 

Wait for Nacos_ The execution of the server.sh script is complete (the blogger has executed it several times, so it will be brief here).

You can view the log files of Nacos:

vim /usr/local/nacos/logs/start.out

Obviously, the Nacos cluster started successfully.



At this time, if a configuration file is created on a Nacos node, other Nacos nodes will also be synchronized to create the configuration file.

This profile also appears when you refresh other Nacos nodes.


Of course, you can delete the profile on any Nacos node.

Refresh the other Nacos nodes, and the profile will also be deleted.

Service registration

Create the alibaba maven project as the parent module, and then create the nacos child module.
AlibabaBlog module

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.kaven</groupId>
    <artifactId>alibaba</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <description>Spring Cloud Alibaba</description>
    <modules>
        <module>nacos</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-cloud-version>Hoxton.SR9</spring-cloud-version>
        <spring-cloud-alibaba-version>2.2.6.RELEASE</spring-cloud-alibaba-version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

nacos module

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>alibaba</artifactId>
        <groupId>com.kaven</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

application.yml:

server:
  port: 8080

spring:
  application:
    name: nacos
  cloud:
    nacos:
      discovery:
        server-addr: "192.168.1.195:9000,192.168.1.196:9000,192.168.1.197:9000"

NacosApplication startup class:

package com.kaven.alibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosApplication.class);
    }
}

@The EnableDiscoveryClient annotation needs to be added.

Start the nacos service:

The Nacos service is successfully registered on the Nacos cluster.



However, there is a disadvantage in deploying Nacos cluster in this way. If a Nacos node goes down, you need to modify the application configuration file before you can continue to use it.

Nginx agent

For convenience, bloggers deploy nginx on the existing virtual machine. Applications can indirectly access the Nacos cluster by accessing nginx. Nginx will forward requests to the Nacos cluster in the intranet (the intranet is more secure) and perform load balancing.

The blogger has introduced the installation and operation of Nginx in his previous blog:

The blogger deploys Nginx on the virtual machine 192.168.1.196. The deployment of Nginx should not be on the same server as the Nacos cluster. The blogger here is just for convenience.

Follow the steps in the above blog to install and run Nginx.

Nginx deployment succeeded.

To modify a profile:

[root@localhost sbin]# cd /usr/local/nginx/conf
[root@localhost conf]# ll
total 68
-rw-r--r--. 1 root root 1077 Nov 11 01:30 fastcgi.conf
-rw-r--r--. 1 root root 1077 Nov 11 01:30 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 Nov 11 01:30 fastcgi_params
-rw-r--r--. 1 root root 1007 Nov 11 01:30 fastcgi_params.default
-rw-r--r--. 1 root root 2837 Nov 11 01:30 koi-utf
-rw-r--r--. 1 root root 2223 Nov 11 01:30 koi-win
-rw-r--r--. 1 root root 5231 Nov 11 01:30 mime.types
-rw-r--r--. 1 root root 5231 Nov 11 01:30 mime.types.default
-rw-r--r--. 1 root root 2656 Nov 11 01:30 nginx.conf
-rw-r--r--. 1 root root 2656 Nov 11 01:30 nginx.conf.default
-rw-r--r--. 1 root root  636 Nov 11 01:30 scgi_params
-rw-r--r--. 1 root root  636 Nov 11 01:30 scgi_params.default
-rw-r--r--. 1 root root  664 Nov 11 01:30 uwsgi_params
-rw-r--r--. 1 root root  664 Nov 11 01:30 uwsgi_params.default
-rw-r--r--. 1 root root 3610 Nov 11 01:30 win-utf
[root@localhost conf]# vim nginx.conf

reload nginx:

[root@localhost conf]# cd ..
[root@localhost nginx]# cd sbin
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sbin]# ./nginx -s reload

Obviously, the Nginx agent succeeded.

Modify the application.yml of the nacos service:

server:
  port: 8080

spring:
  application:
    name: nacos
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.1.196:80

Restart the nacos service:

The Nacos service was successfully registered to the Nacos cluster.




Therefore, after successfully using Nginx to proxy the Nacos cluster, the Nacos cluster can not be exposed to the public network, so that the Nacos cluster is more secure.

If the blogger has something wrong or you have different opinions, you are welcome to comment and supplement.

Tags: Nginx Spring Cloud Cloud Native

Posted on Thu, 11 Nov 2021 04:50:38 -0500 by serenade2