当前位置:网站首页>Nacos cluster construction and MySQL persistence configuration

Nacos cluster construction and MySQL persistence configuration

2022-04-23 18:38:00 Hua Weiyun

@toc

This article is very important , Because the production environment is used for high availability nacos colony , For single node service registration, you can follow the official website configuration , But clustering is a must .

1、Nacos Cluster deployment architecture

   Most production environments use cluster mode to ensure high availability .

  nacos The cluster architecture is as follows :

  nacos Three deployment modes are supported :

  • Click mode : For testing and click trial .
  • Cluster pattern : For the production environment , Ensure high availability .
  • Multi cluster mode : For multi data center scenarios .

   Therefore, when open source, it is recommended that users put all service lists into one vip below , Then hang it under a domain name
  http://ip1:port/openAPI Direct connection ip Pattern , The hook of the machine needs to be modified ip Can be used .
  http://SLB:port/openAPI mount SLB Pattern ( Intranet SLB, Do not expose to the public network , To avoid safety risks ), Direct connection SLB that will do , Hang down server real ip, Poor readability .
  http://nacos.com:port/openAPI domain name + SLB Pattern ( Intranet SLB, Do not expose to the public network , To avoid safety risks ), Good readability , And change ip convenient , Recommended mode

   Make complaints about it here. , Alibaba's documents are not very complete , Maybe there are too many big guys in Ali , Think we can understand the picture above , If you see the above architecture diagram, you can immediately translate it into the following , That means you really understand .

   The above architecture diagram does not specify SLB What is it? , In fact, the above architecture diagram is translated into the following one. Does it look familiar .

image-20220415183010933

2、Nacos Persistent configuration explanation

   because Nacos The default is embedded database derby, This is not convenient for us to use tools to detect in real time , So here we use mysql Persistence nacos Configuration of .

  derby Switch to mysql Steps for :

  • Go first conf Find... Under the folder nacos-mysql.sql This script , Then execute the script in your own database , The effect after execution is as follows :

    image-20220415183420888

  • Go ahead and revise conf Under the folder application.properties file , Add database configuration file :

   Examples are as follows :

image-20220415183511579

3、Linux edition Nacos+MySQL Production environment configuration

3.1 Cluster planning

   Prepare three nacos node

  • nacos01 node :192.168.159.33:3333
  • nacos02 node :192.168.159.33:4444
  • nacos03 node :192.168.159.33:5555

   Prepare one nginx

   Prepare one mysql:192.168.159.33:3306

   namely 1 individual nginx+3 individual nacos node +1 individual mysql Low configuration cluster .
 Insert picture description here

   And here for simplicity ,nginx and mysql No cluster is used , The effect is the same .

   Yes, of course , If you think it is unreasonable for all three nodes to be built in the same virtual machine , Then you can also start three virtual machines , Then revise ip that will do .

3.2 nacos Of linux Version installed

   Download address :https://github.com/alibaba/nacos/releases/tag/1.4.1

   What I use here is 1.4.1 edition ,

image-20220415184001450

   After downloading, you can decompress it directly .

   Directory as follows :

image-20220415184107358

3.3 linux On the server mysql Database configuration

   take conf Under the folder nacos-mysql.sql Script in mysql In the implementation of .

image-20220415184222261

   View the results after execution

image-20220415184302813

3.4 Copy three copies nacos file

image-20220415184417297

   Here we use nacos01、nacos02、nacos03, Just for testing , Please configure on different nodes in the production environment .

3.5 modify application.properties

   modify /conf Under the folder application.properties file , increase mysql Persistent configuration .

  nacos01、nacos02、nacos03 The following modifications need to be made in :

spring.datasource.platform=mysqldb.num=1db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?useUnicode=true&characterEncoding=utf-8&useSSL=falsedb.user=rootdb.password=123456

image-20220415184615992

3.6 modify cluster.conf

   stay nacos01 Modify in the folder /conf Under the folder cluster.conf

   There is no initial , Only one cluster.conf.example,

   We carry out orders cp cluster.conf.example cluster.conf that will do .

   And then execute vim cluster.conf Command to edit the file

image-20220415184830400

   Add the following configuration :

192.168.159.33:3333192.168.159.33:4444192.168.159.33:5555

image-20220415184927554

   Now only nacos01, We copy the file directly to nacos02 and nacos03 In the corresponding folder .

   Execute the following command :

 cp cluster.conf /usr/local/nacos02/conf/ cp cluster.conf /usr/local/nacos03/conf/

3.7 Modify the port number of three nodes

   Modify the of three nodes respectively /conf Under the folder application.properties file .

   take nacos01、nacos02、nacos03 The port numbers of are modified to 3333、4444、5555

  nacos01:

vim /usr/local/nacos01/conf/application.properties

image-20220415185226793

  nacos02:

vim /usr/local/nacos02/conf/application.properties

image-20220415185259857

  nacos03:

vim /usr/local/nacos03/conf/application.properties

image-20220415185328274

3.8 Start three nacos node

   Enter the... Of the three nodes respectively /bin Execute the following command under the file

startup.sh

image-20220415185441776

   You can see from the picture above , All in cluster mode (cluster) Starting up .

   Visit the console of three nodes to check whether it is successful :

  http://192.168.159.33:3333/nacos

image-20220415185621543

  http://192.168.159.33:4444/nacos

image-20220415185637869

  http://192.168.159.33:5555/nacos

image-20220415185652225

3.9 Use nginx Load balancing

   modify nginx Configuration file for

image-20220415185806774

vim nginx.conf

   Add the following configuration :

image-20220415185900065

   Configuration here means to make nginx monitor 1111 port ( You let it monitor 80 Ports are also OK )

   start-up nginx

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

image-20220415190033114

3.10 Cluster testing

   End here ,1 individual nginx+3 individual nacos Registry Center +1 individual mysql The configuration of is completed .

   test Nginx visit nacos:http://192.168.159.33:1111/nacos

image-20220415190142864

   Create a new configuration test :

image-20220415190208944

image-20220415190227211

   Check whether all three nodes have this configuration ,

  nacos01:

image-20220415190257245

  nacos02:

image-20220415190310434

  nacos03:

image-20220415190333193

   see linux In the server mysql Whether there is one more record

image-20220415190428555

3.11 Microservice registration test

   Microservices cloudalibaba-provider-payment9002 Start registration nacos colony

   modify application.yml Of documents nacos Registered address: :192.168.159.33:1111, Be careful , This is a nginx Listen to the address , After load balancing, it is handed over to 192.1168.159.33:3333、192.1168.159.33:4444、192.1168.159.33:5555 One of the three nodes handles .

image-20220415190619075

   After starting the micro service , Go to nacos Console to see if the registration is successful .

image-20220415190704201

   Here we are , The cluster construction and persistence configuration are all introduced , Here you can put nginx and mysql It is also built with clusters , Realize the real high availability cluster .

  == Be careful : Here, if you simulate with a single virtual machine , Memory should be in 3G That's all .==

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231836021874.html