Nacos installation and cluster construction

1.Windows installation

During the development stage, it can be installed on a single machine.

1.1. Download the installation package

On the GitHub page of Nacos, a download link is provided to download the compiled Nacos server or source code:

GitHub homepage: https://github.com/alibaba/nacos

GitHub's Release download page: https://github.com/alibaba/nacos/releases

As shown in the picture:

Just use the package for the windows version nacos-server-1.4.1.zip.

1.2. Unzip

Unzip this package to any non-Chinese directory, as shown in the figure:

Catalog description:

  • bin: startup script

  • conf: configuration file

1.3.Port configuration

The default port of Nacos is 8848. If other processes on your computer occupy port 8848, please try to close the process first.

If you cannot close the process occupying port 8848 , you can also enter the conf directory of nacos and modify the port in the configuration file:

Modify the contents:

1.4.Startup

Startup is very simple, enter the bin directory, the structure is as follows:

Then execute the command:

  • windows command:

  •  

    -m: Select the startup mode standalone: ​​stand-alone startup

    startup.cmd -m standalone

The effect after execution is as follows:

1.5.Access

Enter the address in the browser: http://127.0.0.1:8848/nacos :

The default account and password are nacos. After entering:

2.Linux installation

The Linux or Mac installation method is similar to Windows.

2.1.Install JDK

Nacos relies on JDK to run, and JDK also needs to be installed on Linux.

Upload jdk installation package:

Upload to a directory, for example:/usr/local/

Then unzip:

tar -xvf jdk-8u144-linux-x64.tar.gz

Then rename to java

Configure environment variables:

export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin

Set environment variables:

source /etc/profile

2.2. Upload the installation package

As shown in the picture:

Upload to a directory on the Linux server, for example /usr/local/src:

2.3. Unzip

Command to decompress the installation package:

tar -xvf nacos-server-1.4.1.tar.gz

Then delete the installation package:

rm -rf nacos-server-1.4.1.tar.gz

Final styles in catalog:

Inside the directory:

2.4.Port configuration

Similar to windows

2.5.Startup

In the nacos/bin directory, enter the command to start Nacos:

sh startup.sh -m standalone

3.Nacos dependencies

Parent project:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2.2.5.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

Client:

<!-- nacos客户端依赖包 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

Nacos cluster construction

1. Cluster structure diagram

Official Nacos cluster diagram:

It contains 3 nacos nodes, and then a load balancer proxies 3 Nacos.

This load balancer can use nginx.

Planned cluster structure:

The addresses of the three nacos nodes:

node ip port
nacos1 192.168.150.1 8845
nacos2 192.168.150.1 8846
nacos3 192.168.150.1 8847

2. Build a cluster

Basic steps to build a cluster:

  • Build the database and initialize the database table structure

  • Download nacos installation package

  • Configure nacos

  • Start nacos cluster

  • nginx reverse proxy

2.1.Initialize the database

Nacos default data is stored in the embedded database Derby, which is not a production-available database.

Here we take a single-point database as an example to explain.

First create a new database, name it nacos, and then import the following SQL:

CREATE TABLE `config_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(255) DEFAULT NULL,
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  `app_name` varchar(128) DEFAULT NULL,
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  `c_desc` varchar(256) DEFAULT NULL,
  `c_use` varchar(64) DEFAULT NULL,
  `effect` varchar(64) DEFAULT NULL,
  `type` varchar(64) DEFAULT NULL,
  `c_schema` text,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfo_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info';
​
/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = config_info_aggr   */
/******************************************/
CREATE TABLE `config_info_aggr` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(255) NOT NULL COMMENT 'group_id',
  `datum_id` varchar(255) NOT NULL COMMENT 'datum_id',
  `content` longtext NOT NULL COMMENT '内容',
  `gmt_modified` datetime NOT NULL COMMENT '修改时间',
  `app_name` varchar(128) DEFAULT NULL,
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfoaggr_datagrouptenantdatum` (`data_id`,`group_id`,`tenant_id`,`datum_id`) 
/**************** *******************************/
/* Table name = config_info_beta */
/* Database full name = nacos_config */
​​/

************************ ******************/
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Add tenant field'; 
CREATE TABLE `config_info_beta` ( 
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `beta_ips` varchar(1024) DEFAULT NULL COMMENT 'betaIps',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfobeta_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_beta';
​
/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = config_info_tag   */
/******************************************/
CREATE TABLE `config_info_tag` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
  `tag_id` varchar(128) NOT NULL COMMENT 'tag_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfotag_datagrouptenanttag` (`data_id`,`group_id`,`tenant_id`,`tag_id`) 
/* Database full name = nacos_config */
​/
****************************** ***************/
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_tag';
/*   表名称 = config_tags_relation   */
/******************************************/
CREATE TABLE `config_tags_relation` (
  `id` bigint(20) NOT NULL COMMENT 'id',
  `tag_name` varchar(128) NOT NULL COMMENT 'tag_name',
  `tag_type` varchar(64) DEFAULT NULL COMMENT 'tag_type',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
  `nid` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`nid`),
  UNIQUE KEY `uk_configtagrelation_configidtag` (`id`,`tag_name`,`tag_type`),
  KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_tag_relation';
​
/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = group_capacity   */
/******************************************/
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum number of change history',
CREATE TABLE `group_capacity` ( 
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary key ID', 
  `group_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Group ID, null character represents the entire cluster', 
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Quota, 0 means use the default value', 
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage', 
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The upper limit of the size of a single configuration, in bytes, 0 means using the default value', ` 
  max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The maximum number of aggregate sub-configurations,,0 Indicates using the default value', 
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The upper limit of the sub-configuration size of a single aggregate data, in bytes, 0 means using the default value',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time', 
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time', 
  PRIMARY KEY (`id`), 
  UNIQUE KEY `uk_group_id` (`group_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Cluster, each Group capacity information table'; 
​/
****************************** *************/ 
/* Database full name = nacos_config */ 
/* Table name = his_config_info */ 
/****************** ************************/ 
CREATE TABLE `his_config_info` ( 
  `id` bigint(64) unsigned NOT NULL, 
  `nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
  `data_id` varchar(255) NOT NULL, 
  `group_id` varchar(128) NOT NULL, 
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name', 
  `content` longtext NOT NULL,
  `md5` varchar(32) DEFAULT NULL, 
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, 
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, 
  `src_user` text, 
  `src_ip` varchar(50) DEFAULT NULL, 
  `op_type` char(10) DEFAULT NULL, 
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'Tenant field', 
  PRIMARY KEY (`nid`), 
  KEY `idx_gmt_create` (`gmt_create`), 
  KEY `idx_gmt_modified` (`gmt_modified`), 
  KEY `idx_did ` (`data_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Multi-tenant transformation'; 
​/ 
/* Database full name = nacos_config */ 
/* Table name = tenant_capacity */
/******************************************/
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The upper limit of the sub-configuration size of a single aggregate data, in bytes, 0 means using the default value',
/******************************************/ 
CREATE TABLE `tenant_capacity` ( 
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary key ID', 
  `tenant_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID', 
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Quota, 0 means using the default value', 
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage', 
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Single configuration size limit , the unit is bytes, 0 means using the default value', 
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum number of aggregate sub-configurations', 
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0 ' COMMENT 'Maximum number of change history',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='租户容量信息表';
CREATE TABLE `tenant_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `kp` varchar(128) NOT NULL COMMENT 'kp',
  `tenant_id` varchar(128) default '' COMMENT 'tenant_id',
  `tenant_name` varchar(128) default '' COMMENT 'tenant_name',
  `tenant_desc` varchar(256) DEFAULT NULL COMMENT 'tenant_desc',
  `create_source` varchar(32) DEFAULT NULL COMMENT 'create_source',
  `gmt_create` bigint(20) NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`),
  `gmt_modified` bigint(20) NOT NULL COMMENT 'Modification time',
  UNIQUE KEY `uk_tenant_info_kptenantid` (`kp`,`tenant_id`),
  KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='tenant_info';
​
CREATE TABLE `users` (
    `username` varchar(50) NOT NULL PRIMARY KEY,
    `password` varchar(500) NOT NULL,
    `enabled` boolean NOT NULL
);
​
CREATE TABLE `roles` (
    `username` varchar(50) NOT NULL,
    `role` varchar(50) NOT NULL,
    UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE
);
​
CREATE TABLE `permissions` (
    `role` varchar(50) NOT NULL,
    `resource` varchar(255) NOT NULL,
    `action` varchar(8) NOT NULL,
    UNIQUE INDEX `uk_role_permission` (`role`,`resource`,`action`) USING BTREE
);
​
INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);
​
INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');

2.2. Download nacos

nacos has a download address on GitHub: Tags · alibaba/nacos · GitHub . You can choose any version to download.

In this example only version 1.4.1 is used:

2.3.Configuring Nacos

Unzip this package to any non-Chinese directory, as shown in the figure:

Catalog description:

  • bin: startup script

  • conf: configuration file

Enter the conf directory of nacos, modify the configuration file cluster.conf.example, and rename it to cluster.conf:

Then add content:

127.0.0.1:8845
127.0.0.1.8846
127.0.0.1.8847

Then modify the application.properties file and add database configuration

spring.datasource.platform=mysql
​
db.num=1
​
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=123

2.4.Startup

Copy the nacos folder into three copies and name them respectively: nacos1, nacos2, nacos3

Then modify the application.properties in the three folders respectively,

nacos1:

server.port=8845

nacos2:

server.port=8846

nacos3:

server.port=8847

Then start three nacos nodes respectively:

startup.cmd

2.5.nginx reverse proxy

 

Unzip to any non-Chinese directory:

 

Modify the conf/nginx.conf file and configure it as follows:

upstream nacos-cluster {
    server 127.0.0.1:8845;
    server 127.0.0.1:8846;
    server 127.0.0.1:8847;
}
​
server {
    listen       80;
    server_name  localhost;
​
    location /nacos {
        proxy_pass http://nacos-cluster;
    }
}

Then visit: http://localhost/nacos in the browser .

The application.yml file in the code is configured as follows:

spring:
  cloud:
    nacos:
      server-addr: localhost:80 # Nacos地址

2.6.Optimization

  • During actual deployment, you need to set a domain name for the nginx server that is used as a reverse proxy, so that if there is a server that migrates the nacos client later, there is no need to change the configuration.

  • Each node of Nacos should be deployed to multiple different servers to ensure disaster recovery and isolation.

Guess you like

Origin blog.csdn.net/weixin_55008454/article/details/131856706