Spring Cloud Alibaba(02) - Nacos service registration and configuration center
Preface: all microservice modules of Spring Cloud Alibaba series are built under the springcloud Guigu parent project, which is Spring Cloud(02) -- parent project of build order payment micro service module Built in.
1. Introduction to Nacos
Nacos is committed to discovering, configuring and managing microservices. It provides a set of simple and useful features that enable dynamic service discovery, service configuration management, and service and traffic management.
Nacos makes it easier and faster to build, deliver and manage microservice platforms. It is an infrastructure that supports service-centered modern application architecture. It adopts micro service or cloud local method.
Nacos is a combination of registry + configuration center, which is equivalent to Eureka + Config + Bus of Spring Cloud Netflix
Comparison of registries of various versions: Nacos can switch between CP and CA modes.
Nacos official website: http://www.google.com/sea....tin round+rock+texas)
Nacos GitHub address: https://github.com/alibaba/Nacos
Nacos Chinese document: https://nacos.io/zh-cn/docs/what-is-nacos.html
2. Install and run Nacos
Nacos download address: https://github.com/alibaba/nacos/tags , download and unzip
Open cmd from the bin directory and enter startup cmd command, run nacos:
Access request: localhost:8848/nacos
All accounts and passwords are nacos. Click submit:
nacos installation and operation succeeded!
3. Nacos as service registry
3.1 Nacos based service providers
1. Create cloudalibaba provider payment9001 module
2. Import pom dependencies
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
3. Writing yml configuration files
server: port: 9001 spring: application: name: nacos-payment-provider cloud: nacos: discovery: server-addr: localhost:8848 # Configure the address of Nacos #Exposure endpoint management: endpoints: web: exposure: include: '*'
4. Create main startup class
@SpringBootApplication @EnableDiscoveryClient public class PaymentMain9001 { public static void main(String[] args) { SpringApplication.run(PaymentMain9001.class,args); } }
5. Write business code
@RestController public class PaymentController { @Value("$server.port") private String serverPort; @GetMapping(value = "/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id){ return " Hello Nacos serverPort: "+serverPort + "id: "+id; }
6. Testing
- Start nacos
- Start the cloudalibaba provider payment9001 module
- Access request: localhost:8848/nacos
The service is registered in the nacos registry
- Access request: http://localhost:9001/payment/nacos/2
Test successful!
In order to build the following load balancing demonstration environment: create 9002 module according to 9001. The ports are different, and everything else is the same.
The service provider cluster environment was built successfully.
3.2 Nacos based service consumers
1. Create the cloudalibaba Nacos consumer order83 module
2. Import pom dependencies
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.cheng.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
3. Writing yml configuration files
server: port: 83 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848 #Consumer 8 The name of the micro service to access service-url: nacos-user-service: http://nacos-payment-provider
4. Create main startup class
@SpringBootApplication @EnableDiscoveryClient public class OrderMain83 { public static void main(String[] args) { SpringApplication.run(OrderMain83.class,args); } }
5. Write the configuration class. Because nacos integrates ribbon, it can combine RestTemplate to realize load balancing and remote call
@Configuration public class ConfigBean { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
6. Business class
@RestController @Slf4j public class OrderController { @Resource private RestTemplate restTemplate; @Value("${server-url.nacos-user-service}") //Write the service address in the configuration file to separate the configuration and code private String Server_Url; @GetMapping(value = "/consumer/payment/nacos/{id}") public String OrderInfo(@PathVariable("id") Integer id){ return restTemplate.getForObject(Server_Url+"/payment/nacos"+id,String.class); } }
7. Testing
- Start nacos start
- Start the cloudalibaba provider payment9001 and cloudalibaba provider payment9002 modules
- Start the cloudalibaba Nacos consumer order83 module
- Access request: http://localhost:8848/nacos , registration succeeded.
-
Access request: access request: http://localhost:9001/payment/nacos/22 , self test passed!
-
Access request http://localhost:83/consumer/payment/nacos/22
First visit:
Visit again:
Continue to visit and switch between 9001 and 9002 in turn.
The default polling algorithm for load balancing is successfully implemented.
4. Nacos as service configuration center
4.1 basic configuration
1. Create cloudalibaba config Nacos client3377 module
2. Import pom dependencies
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
3. Writing yml configuration files
bootstrap.yml
server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 # nacos as the address of the registry config: server-addr: localhost:8848 # nacos as the address of the configuration center file-extension: yaml # Specifies the configuration of yaml format
application.yml
spring: profiles: active: dev #development environment
4. Create main startup class
@SpringBootApplication @EnableDiscoveryClient public class NacosConfigClientMain3377 { public static void main(String[] args) { SpringApplication.run(NacosConfigClientMain3377.class,args); } }
5. Business class
@RestController @RefreshScope //Support the dynamic refresh function of nacos public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping(value = "/config/info") public String getConfigInfo(){ return configInfo; } }
6. Add configuration information to Nacos
In Nacos, the complete format of dataId is as follows:
${prefix}-${spring.profiles.active}.${file-extension}
- prefix defaults to spring application. The value of name can also be set through the configuration item spring cloud. nacos. config. prefix to configure.
- spring.profiles.active is the profile corresponding to the current environment. Note: when spring profiles. When active is empty, the corresponding connector - will not exist, and the splicing format of dataId will become ${prefix}$ {file-extension}
- File exception is the data format of the configuration content, which can be configured through the configuration item spring cloud. nacos. config. File extension. Currently, only properties and yaml types are supported.
Combine the above yml configuration file and data id format, create a new configuration file in nacos, and click publish:
Published successfully!
7. Testing
-
Start nacos
-
Start the cloudalibaba config Nacos client3377 module
-
Access request: http://localhost:3377/config/info
- Dynamic refresh, modify the version number of the configuration file in nacos to version=2.0, and re access the request: http://localhost:3377/config/info , you will find that the configuration has also been refreshed:
4.2 classification configuration
SpringCloud Alibaba Nacos configuration center has three configuration methods:
1. Data ID configuration scheme
2. Group configuration scheme
3. NameSpace configuration scheme
The outermost namespace can be used to distinguish the deployment environment. Group and Data ID logically distinguish the two target objects.
1. Data ID configuration scheme
1. In application Switch environment to test environment in YML
spring: profiles: active: test #testing environment
2. Create a new test configuration file in the nacos configuration center for the test environment
Now our nacos configuration center has two data IDS:
3. Testing
Access request: http://localhost:3377/config/info
Successfully switched to the test environment.
2. Group configuration scheme
Environment partitioning through Group
1. Create two identical dataids in nacos, but different group s
2. In application Switching environment in YML
spring: profiles: active: info
3. In bootstrap Set the group attribute under config in YML
Set as dev of development group_ GROUP
config: server-addr: localhost:8848 # nacos as the address of the configuration center file-extension: yaml # Specifies the configuration of yaml format group: DEV_GROUP # Set as development group
Access request: http://localhost:3377/config/info
Set as development group TEST_GROUP
config: server-addr: localhost:8848 # nacos as the address of the configuration center file-extension: yaml # Specifies the configuration of yaml format group: TEST_GROUP
Access request: http://localhost:3377/config/info
3. NameSpace configuration scheme
1. Create two new namespaces in nacos, a test namespace and a dev namespace. Each space has a namespace ID
2. Create three identical dataids under the dev namespace, but different group s, as follows:
3. Switch group in dev namespace
Use TEST_GROUP
config: server-addr: localhost:8848 # nacos as the address of the configuration center file-extension: yaml # Specifies the configuration of yaml format group: TEST_GROUP namespace: b90af516-51e1-4b5c-8741-f6f51cb55595 #To use which namespace, bind the namespace ID
Similarly, if you want to use other groups, you can configure them as above, but only if they are in the same namespace
5. Nacos cluster and persistence configuration
1. Installing Nacos on Linux system
-
Compressed package download address: https://github.com/alibaba/nacos/releases/tag/
-
Upload to Linux server
-
decompression
tar -zxvf nacos-server-1.3.1.tar.gz
-
After decompression, a folder named nacos will appear. View the contents of this folder:
2. mysql database configuration on Linux server
1. Find the sql script to be executed. Under the conf directory, it is the above conf Directory:
- Copy the sql script file. The following is the content of the sql script:
/* * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /******************************************/ /* Full database name = nacos_config */ /* Table name = config_info */ /******************************************/ 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 'Creation time', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time', `src_user` text COMMENT 'source user', `src_ip` varchar(20) DEFAULT NULL COMMENT 'source ip', `app_name` varchar(128) DEFAULT NULL, `tenant_id` varchar(128) DEFAULT '' COMMENT 'Tenant field', `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'; /******************************************/ /* Full database name = nacos_config */ /* Table name = 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 'content', `gmt_modified` datetime NOT NULL COMMENT 'Modification time', `app_name` varchar(128) DEFAULT NULL, `tenant_id` varchar(128) DEFAULT '' COMMENT 'Tenant field', PRIMARY KEY (`id`), UNIQUE KEY `uk_configinfoaggr_datagrouptenantdatum` (`data_id`,`group_id`,`tenant_id`,`datum_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Add tenant field'; /******************************************/ /* Full database name = nacos_config */ /* Table name = config_info_beta */ /******************************************/ 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 'Creation time', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time', `src_user` text COMMENT 'source user', `src_ip` varchar(20) DEFAULT NULL COMMENT 'source ip', `tenant_id` varchar(128) DEFAULT '' COMMENT 'Tenant field', 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'; /******************************************/ /* Full database name = nacos_config */ /* Table name = 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 'Creation time', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time', `src_user` text COMMENT 'source user', `src_ip` varchar(20) DEFAULT NULL COMMENT 'source ip', PRIMARY KEY (`id`), UNIQUE KEY `uk_configinfotag_datagrouptenanttag` (`data_id`,`group_id`,`tenant_id`,`tag_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_tag'; /******************************************/ /* Full database name = nacos_config */ /* Table name = 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'; /******************************************/ /* Full database name = nacos_config */ /* Table name = group_capacity */ /******************************************/ 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,A null character indicates the entire cluster', `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Quota, 0 means the default value is used', `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage', `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The maximum size of a single configuration, in bytes. 0 means the default value is used', `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum number of aggregate sub configurations, 0 means the default value is used', `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 the default value is used', `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum change history quantity', `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'; /******************************************/ /* Full database 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(20) 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'; /******************************************/ /* Full database name = nacos_config */ /* Table name = tenant_capacity */ /******************************************/ 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 the default value is used', `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage', `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The maximum size of a single configuration, in bytes. 0 means the default value is used', `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum number of aggregate sub configurations', `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 the default value is used', `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum change history quantity', `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_tenant_id` (`tenant_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Tenant capacity information table'; 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 'Creation time', `gmt_modified` bigint(20) NOT NULL COMMENT 'Modification time', PRIMARY KEY (`id`), 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');
3. Copy and paste the sql script into the tmp file in the root directory
Copy and paste the script file into the tmp file in the root directory (you can transfer it with xftp), because all users of the tmp directory can read it before it can be executed:
-
Execute sql script
Login to mysql
mysql -uroot -p
Then check to see if there is a config_info database. If not, create one
Switch to config_info database and execute the script
source /tmp/nacos-mysql.sql
View config_ Contents in info database:
sql script executed successfully!
3. Switch to the database on the Linux server
1. Find application Properties configuration file
It is also in the conf Directory: when modifying a configuration file, you can use the cp command to back up a copy
2. Add content:
Use the vim editor to open application properties
The contents are as follows:
spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:myql://39.105.112.131:3306/config_info?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=root db.password=
If Linux is local, the port number is 127.0.0.1. If it is on a remote server, it is the IP of its own server.
4. Configuration of nacos cluster on Linux server
1. Find the cluster in the conf directory Conf.example file for backup. The backup name is clu conf
2. View IP addresses recognized by Linux
Execute command:
hostname -i
3. Open the clu with the vim editor Conf file, modify:
Annotate the original contents of the file, add the following contents, and then save and exit vim:
Note: the IP is the IP recognized by Linux found by hostname -i
In this way, we sort out the different service port numbers of the three nacos clusters.
5. Edit Nacos startup script startup SH to enable it to receive different boot ports
/Startup is available in mynacos/nacos/bin directory sh
Usually, the startup of the stand-alone version is/ startup.sh is enough.
However, for cluster startup, we hope to start different nacos instances by passing different port numbers similar to the shell commands of other software.
For example: Command: `/ startup.sh -p 3333 ` indicates the nacos server instance with the startup port number of 3333 and the previous clu The port number of conf configuration is consistent.
1. Open startup.exe with the vim editor SH modify
2. Add the following two configurations
nohup $JAVA -Dserver.port=${PORT} ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
Finally save and exit!
6. Configure Nginx as the load balancer
1. Locate the nginx configuration file nginx Conf, back up first
2. Open nginx.com with the vim editor Conf, modify
Before modification:
After modification:
Fill in the above ip according to your own situation, and then save and exit.
7. Testing
Switch to the bin directory of nacos and start the nacos cluster test:
nacos cluster started successfully!
Access nacos via nginx:
- nginx self test:
Self test successful!
- Nacos access request: http://39.105.112.131:1111/nacos/#/login , Nacos cluster configuration succeeded!