All the microservice modules of Spring Cloud Alibaba series are built under the Spring Cloud-Guigu parent project, which is in the Spring CloudSpring Cloud(02) — The parent project of the order-payment microservice moduleIn the building.

1. Introduction to Nacos

Nacos is dedicated to discovering, configuring, and managing microservices. It provides a simple and useful set of 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 microservices platforms. It is the infrastructure that supports a modern service-centric application architecture with a microservices or cloud-on-premise approach.

Nacos is the combination of registry + configuration center, equivalent to Eureka + Config + Bus of Spring Cloud Netflix

Comparison of registry versions: Nacos can switch between CP and CA modes.

Nacos’s official website: http://www.google.com/sea… .tin round+rock+texas)

Nacos GitHub address: github.com/alibaba/Nac…

Nacos 中文 版 : nacos. IO /zh-cn/docs/…

2. Install and run Nacos

Nacos download: github.com/alibaba/nac…

Open CMD from the bin directory and enter startup. CMD to run nacos:

Access request: localhost:8848/nacos

The account password is nacOS, click Submit:

Nacos installed and running successfully!

3. Nacos acts as a service registry

3.1 NacOS-based service providers

1. Create cloudBaba-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>
Copy the code

3. Write the YML configuration file

server:
  port: 9001

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 Configure the address of Nacos
# expose endpoints
management:
  endpoints:
    web:
      exposure:
        include: The '*'
Copy the code

4. Create the main startup class

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9001 {
    public static void main(String[] args) { SpringApplication.run(PaymentMain9001.class,args); }}Copy the code

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; 
    }
Copy the code

6, test,

  1. Start the nacos
  2. Start the CloudBaba-provider-Payment9001 module
  3. Access request: localhost:8848/nacos

The service is registered in the NACOS registry

  1. Access request: http://localhost:9001/payment/nacos/2

Test successful!

To set up the following load balancing demonstration environment: Create the 9002 module based on the 9001. The ports are different, but everything else is the same.

The service provider cluster environment is set up successfully.

3.2 NacOS-based service consumers

1. Create cloudBaba-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>
Copy the code

3. Write the YML configuration file

server:
  port: 83

spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

# Consumer 8. The name of the microservice to access
service-url:
  nacos-user-service: http://nacos-payment-provider
Copy the code

4. Create the main startup class

@SpringBootApplication
@EnableDiscoveryClient
public class OrderMain83 {
    public static void main(String[] args) { SpringApplication.run(OrderMain83.class,args); }}Copy the code

5. Write configuration classes. Because NACOS integrates with the Ribbon, you can use RestTemplate for load balancing and remote calls

@Configuration public class ConfigBean { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); }}Copy the code

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 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); }}Copy the code

7, test,

  1. Start NACOS Start
  2. Start cloudalibaba-provider-Payment9001 and Cloudalibaba-provider-Payment9002 modules
  3. Start the CloudBaba-nacOS-consumer-Order83 module
  4. Access request: http://localhost:8848/nacos, register successfully.

  1. Access request: access request: http://localhost:9001/payment/nacos/22, self-test through!

  1. Access request http://localhost:83/consumer/payment/nacos/22

First Visit:

Visit again:

Continue access and alternate between 9001 and 9002.

The default algorithm polling of load balancing is successfully implemented.

4. Nacos serves as the service configuration center

4.1. Basic Configuration

1. Create cloudBaba-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>
Copy the code

3. Write the YML configuration file

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  # specify yamL configuration
Copy the code

application.yml

spring:
  profiles:
    active: dev  # Development environment
Copy the code

4. Create the main startup class

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigClientMain3377 {
    public static void main(String[] args) { SpringApplication.run(NacosConfigClientMain3377.class,args); }}Copy the code

5. Business class

@RestController
@RefreshScope  // Dynamic refresh of NACOS is supported
public class ConfigClientController {
    
    @Value("${config.info}")
    private String configInfo;
    
    @GetMapping(value = "/config/info")
    public String getConfigInfo(a){
        returnconfigInfo; }}Copy the code

6. Add the configuration information to Nacos

In Nacos, the full format of dataId is as follows:

${prefix}-${spring.profiles.active}.${file-extension}
Copy the code
  • prefixThe default isspring.application.nameIs also available through the configuration itemspring.cloud.nacos.config.prefixTo configure.
  • spring.profiles.activeIs the profile corresponding to the current environment. Note: Ifspring.profiles.activeIf null, the corresponding concatenate-Also will not exist, the dataId splicing format becomes${prefix}.${file-extension}
  • file-exetensionFor the data format of the configuration content, you can pass the configuration itemspring.cloud.nacos.config.file-extensionTo configure. Currently only supportedpropertiesyamlType.

Create a new configuration file in NACOS with the format of the above YML configuration file and data ID and click Publish:

Successful release!

7, test,

  1. Start the nacos

  2. Start the Cloudbaba-config-nacos-Client3377 module

  3. Access request: http://localhost:3377/config/info

  1. Dynamic refresh, modify the version number of the configuration in nacos version = 2.0, access request again: http://localhost:3377/config/info, find also refreshed the configuration:

4.2 Category Configuration

SpringCloud Alibaba Nacos configuration center has three configuration methods, which are as follows:

1. Data ID configuration scheme

2. Group configuration scheme

3. NameSpace configuration scheme

The outermost namespace is used to distinguish the deployment environment, and the Group and Data IDS logically distinguish the two target objects.

1. Data ID configuration scheme

1. Switch environment to test environment in application.yml

spring:
  profiles:
    active: test    # Test environment
Copy the code

2. Create a test test configuration file in the NACOS configuration center for testing the environment

We now have two Data ids in the configuration center of nacOS:

3, test,

Access request: http://localhost:3377/config/info

Successfully switch to the test environment.

2. Group configuration scheme

Implement environment partitioning through groups

Create two identical dataids in NACOS, but with different groups

2. Switch the environment in application.yml

spring:
  profiles:
    active: info
Copy the code

3. Set the group attribute under config in bootstrap.yml

Set this parameter to DEV_GROUP

config:
  server-addr: localhost:8848  # nacOS as the address of the configuration center
  file-extension: yaml  # specify yamL configuration
  group: DEV_GROUP  Set to development group
Copy the code

Access request: http://localhost:3377/config/info

Set this to TEST_GROUP

config:
  server-addr: localhost:8848  # nacOS as the address of the configuration center
  file-extension: yaml  # specify yamL configuration
  group: TEST_GROUP
Copy the code

Access request: http://localhost:3377/config/info

3. NameSpace configuration scheme

Create two new namespaces in nacOS, one for test and one for dev, each with a namespace ID

Select * from dev; select * from dev; select * from dev; select * from dev;

3, implement the dev namespace group switch

Using TEST_GROUP

config:
  server-addr: localhost:8848  # nacOS as the address of the configuration center
  file-extension: yaml  # specify yamL configuration
  group: TEST_GROUP
  namespace: b90af516-51e1-4b5c-8741-f6f51cb55595  Bind the namespace ID to which namespace to use
Copy the code

Similarly, to use other groups, configure as above, but only in the same namespace

5. Nacos clustering and persistence configuration

1. Install Nacos on Linux

  1. Download package: github.com/alibaba/nac…

  2. Upload to the Linux server

  3. Unpack the

    The tar - ZXVF nacos - server - 1.3.1. Tar. GzCopy the code
  4. When unzipped, a folder named nacos appears. Check the contents of this folder:

Mysql database configuration on Linux server

1. Locate the SQL script to be executed in the conf directory:

  1. Copy the SQL script file. The following is the contents of the SQL script:
Licensed under the Apache License, Version 2.0 (the "License"); 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. */

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full 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';

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full 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';


/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full 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';

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full 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';

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full 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';

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full 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, null character for whole cluster ',
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Quota, 0 means default',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum size of a single configuration, in bytes, with 0 indicating default',
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum number of aggregate sub-configurations, where 0 indicates default',
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Upper subconfiguration size of a single aggregate data, in bytes, with 0 indicating default.',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum change history',
  `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 and 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(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 Conversion';


/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full 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 default',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum size of a single configuration, in bytes, with 0 indicating default',
  `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 'Upper subconfiguration size of a single aggregate data, in bytes, with 0 indicating default.',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum change history',
  `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');

Copy the code

3. Copy and paste the SQL script to the TMP file in the root directory

Copy and paste the script file into the TMP file in the root directory (XFTP can be used for transfer), because the TMP directory is accessible to all users before it can be executed:

  1. Executing SQL Scripts

    Log on to the mysql

    mysql  -uroot -p
    Copy the code

Then check to see if the CONFIG_info database exists and create one if it does not

Switch to the config_info database and execute the script

source /tmp/nacos-mysql.sql
Copy the code

Look at the config_info database:

SQL script executed successfully!

3. Switch to the database on the Linux server

1. Locate the application.properties configuration file

Also in the conf directory: When modifying a configuration file, you can run the cp command to back up a copy

2. Add content:

Use the Vim editor to open application.properties

As follows:

Spring. The 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=
Copy the code

The port number is 127.0.0.1 if Linux is local, or the IP address of your own server if it is on a remote server.

4. Configuration of nacOS cluster on Linux server

1. Locate the cluster.conf.example file in the conf directory and back up the file named clu.conf

2. Check the IP that Linux can recognize

Execute command:

hostname -i
Copy the code

3, Open clu.conf file with vim editor and modify:

Comment out the original contents of the file and add the following content, then save and exit vim:

Note: IP is the IP identified by Linux from hostname -i

In this way, we tease out the different service port numbers of the three NACOS clusters.

5. Edit the startup script startup.sh of Nacos so that it can accept different startup ports

Startup. sh exists in the /mynacos/nacos/bin directory

/startup.sh is used to start a standalone server.

But for cluster startup, we want to be able to start different nacOS instances by passing different port numbers like shell commands of other software.

For example, run the following command: ‘./startup.sh -p 3333 ‘to start the nacOS server instance whose port number is 3333, which is the same as that configured in clu.conf in the previous step.

1. Use vim to open startup.sh

2. Add the following two configurations

nohup $JAVA -Dserver.port=${PORT} ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
Copy the code

Finally save and exit!

6. Configure Nginx as a load balancer

1, find the nginx configuration file nginx.conf and back it up

2. Open nginx.conf with vim editor and modify it

Modify before:

Revised:

Fill in the above IP according to its own situation, then save and exit.

7, test,

Switch to the bin directory of nacOS and enable the NACOS cluster test:

Nacos cluster started successfully!

Access nacOS via nginx:

  1. Nginx self-test:

Self test successful!

  1. Visit nacos: access request: http://39.105.112.131:1111/nacos/#/login, nacos cluster configuration success!