Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build Nacos Cluster

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to build a Nacos cluster, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

A brief introduction to Nacos

Nacos (Naming and Configuration Service) is dedicated to helping you discover, configure, and manage micro services. Nacos provides an easy-to-use set of features to help you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management.

For more information, please see the Nacos official document [1]

II. Nacos installation

1. Nacos dependency

Nacos is developed based on java and runs on the java environment.

Go to the official website to download JDK [2] depending on 64 bit JDK 1.8 cycles.

2. Nacos installation

Download the compiled package, the latest stable version [3]

Unzip nacos-server-$version.zip or tar-xvf nacos-server-$version.tar.gz cd nacos/bin

III. Nacos deployment

1. Single instance deployment

Single instance deployment is not suitable for production environment, and a single point of failure is fatal.

Linux single instance non-clustered mode startup command

Startup.sh-m standalone

Linux single instance non-clustered mode shutdown command

Shutdown.sh

Access the nacos management page. The initialization user name and password are all nacos.

2. Cluster deployment

1. Cluster architecture

Highly available Nginx cluster

Nacos cluster (at least three instances)

High availability database cluster (replacing Nacos embedded databases)

2. Local virtual machine simulates cluster deployment

Local environment preparation

Use VMware workstation to virtualize several machines shown in the above table on the local PC machine, in which Nginx and MySQL are both single instances used for exercise only.

Building steps

Initialize the database tables necessary for nacos and configure

Locate the database script file provided in the Nacos installation directory

Create nacos_config libraries in the MySQL instance and import scripts

Modify the Nacos configuration file to point to the MySQL instance and replace its embedded database

# * switch from Nacos embedded database platform to MySQL * # spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://192.168.15.141:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC db.user=root db.password=123456

Description: all three nacos instances need to switch MySQL platforms, and all need to perform the above operations.

Nacos cluster configuration

Copy the cluster.conf file

Nacos cluster configuration, modify cluster.conf file

[root@localhost conf] # vim. / cluster.conf # it is ip # example 192.168.15.145 192.168.15.147 192.168.15.148

Description: all three nacos instances need to be configured in the above cluster. Now that the configuration of nacos is over, you can try to start three nacos instances in cluster mode.

Start three nacos instances in cluster mode

Try to access the nacos management page and test whether the three instances are working properly

Note: if the three instances are started normally in cluster mode, the login page above will be displayed by accessing the management pages of the three instances respectively. If you cannot access it, it is possible that the firewall does not open the port of the nacos service, and you can execute the following command.

[root@localhost bin] # firewall-cmd-add-port=8848/tcp-permanent success [root@localhost bin] # firewall-cmd-reload success [root@localhost bin] # firewall-cmd-list-all public (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: ssh dhcpv6-client ports: 27017/tcp 8848/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@localhost bin] #

Nginx configuration

Nginx installation reference, Nginx source code installation [4]

Modify Nginx configuration file nginx.conf

Worker_processes 1; events {worker_connections 1024;} http {include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # nacos cluster load balancer upstream nacos-cluster {server 192.168.15.145purl 8848; server 192.168.15.147purl 8848; server 192.168.15.148purl 8848 } server {listen 80; server_name 192.168.15.146; location / {# root html; # index index.html index.htm; proxy_pass http://nacos-cluster;} error_page 500502 503 504 / 50x.html Location = / 50x.html {root html;}

Start Nginx

/ usr/local/nginx/sbin/nginx-c / usr/local/nginx/conf/nginx.conf

Micro service configuration

Microservice parent pom configuration

4.0.0 com.atguigu.springcloud cloud2020 1.0-SNAPSHOT pom cloud-alibaba-nacos-config-client-3377 UTF-8 1.8 1.8 4.12 1.2.17 1.16.18 5.1.47 1.1.16 1.3.0 org.springframework.boot spring-boot 2.2.2.RELEASE pom import org.springframework. Cloud spring-cloud-dependencies Hoxton.SR1 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies 2.1.0.RELEASE pom Import mysql mysql-connector-java ${mysql.version} com.alibaba druid ${druid.version} Org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis.spring.boot.version} junit junit ${junit.version} Log4j log4j ${log4j.version} org.projectlombok lombok ${lombok.version} Org.springframework.boot spring-boot-maven-plugin true true

Micro-service pom dependency

Cloud2020 com.atguigu.springcloud 1.0-SNAPSHOT 4.0.0 cloud-alibaba-nacos-config-client-3377 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config com.alibaba.cloud spring-cloud-starter-alibaba- Nacos-discovery org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test Test org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true

Microservice bootstrap.yml configuration

Server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: # server-addr: my.nacos.com:8848 # nacos Cluster configuration (Nginx) server-addr: 192.168.15.146 Nginx 80 config: # server-addr: my.nacos.com:8848 # nacos Cluster configuration (Nginx) server- Addr: 192.168.15.146 addr 80 # specify configuration in yaml format file-extension: yaml # specify grouping group: DEV_GROUP # specify namespace ID namespace: my_nacos_namespace

Microservice startup class configuration

Package com.atguigu.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @ SpringBootApplication @ EnableDiscoveryClient public class NacosConfigClientMain3377 {public static void main (String [] args) {SpringApplication.run (NacosConfigClientMain3377.class, args);}}

Microservice Controller reads nacos configuration

Package com.atguigu.springcloud.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @ RestController @ Slf4j @ RefreshScope / / supports Nacos's dynamic refresh feature public class ConfigClientController {@ Value ("${config.info}") private String configInfo @ GetMapping ("/ config/info") public String getConfigInfo () {return configInfo;}}

Maintain a configuration on the nacos administration page

Start the microservice locally and access the

These are all the contents of the article "how to build Nacos clusters". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report