Linux deploys Nacos and configures the MySQL data source. Put the Springboot configuration file into Nacos and use it

preface

Yesterday all sorts of strange things were as like as two peas in the nacos tutorial. They found that the nacos tutorial was basically a bit like the same. They were copied and copied, so that you could configure a lot of things. The introduction of dependencies was also a great surprise. Even the official tutorials of the library could not connect you to the configuration center successfully.

Now I've learned. Let me teach you. It's too simple. Don't look at those online and official things

1, Installing and configuring Nacos in Docker

Of course, if you don't install it in Docker, you can download files from the official website of Nacos and decompress them directly. Just skip the process of docker pull and run. Then you need to register Nacos as a service, start it up, and other steps are the same. In short, it's a little troublesome not to install it in Docker.

Install Docker

Common installation commands for all Linux systems

curl -sSL https://get.daocloud.io/docker | sh

Pull the Nacos image

Note: be sure to pull 1.4 1, because the configuration file I give below is 1.4 1. Other versions will fail to start. Nacos is so disgusting. Pulling is a little slow. Wait patiently.

docker pull nacos/nacos-server:1.4.1

Start the Nacos container

Paste it all on the command line and just press enter. It's not sticky line by line

docker run -d --name nacos \
-e PREFER_HOST_MODE=hostname \
-e MODE=standalone \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=111.111.111.111 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=root \
-p 8848:8848 nacos/nacos-server:1.4.1

111.111 above 111.111 write the IP address of your MySQL database, followed by the port, database name (the database name must be nacos, which is officially specified. The nacos database should be created by yourself. The compressed package downloaded from the official website of sql file can be extracted. For convenience, I will teach you to post it directly to you when installing MySQL and building the database), account and password

Open firewall port

systemctl status firewalld    #First, check the status of the firewall. If the firewall is not turned on, you don't need to see the following
firewall-cmd --zone=public --add-port=8848/tcp --permanent #It means release port 8848
firewall-cmd --zone=public --add-port=3306/tcp --permanent #The port of the database you specified should also be released
firewall-cmd --reload   ##Reload the fire wall to make the released port effective

Creating a nacos database in MySQL

The nacos database is used to specify the data source for nacos, otherwise it cannot be started. I will write the method of installing MySQL in Docker separately later. First, look at other versions. The configuration files before 5.6, after 5.6 and 8 are different.

Enter the MySQL container, create the nacos database, switch to the created nacos database, and click the basic operation code

then!!! Copy all the following SQL files to MySQL >, and then wait until it runs. After running, see if the last sentence doesn't run, and my last sentence doesn't run. Just hit enter again

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 '2010-05-05 00:00:00' COMMENT 'Creation time',

  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' 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';



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';



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 '2010-05-05 00:00:00' COMMENT 'Creation time',

  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' 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';


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 '2010-05-05 00:00:00' COMMENT 'Creation time',

  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' 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';



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';



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 '2010-05-05 00:00:00' COMMENT 'Creation time',

  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' 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';



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 '2010-05-05 00:00:00',

  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00',

  `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';



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 '2010-05-05 00:00:00' COMMENT 'Creation time',

  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' 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,

	constraint uk_username_role UNIQUE (username,role)

);



CREATE TABLE permissions (

    role varchar(50) NOT NULL,

    resource varchar(512) NOT NULL,

    action varchar(8) NOT NULL,

    constraint uk_role_permission UNIQUE (role,resource,action)

);



INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);



INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');


most important of all!!!, Remember to restart nacos

docker restart nacos

Log in to nacos

If your Linux server has no interface, log in on a Windows computer, because the firewall on it has opened the port. Enter IP:8848/nacos in the browser address bar. The default account password is nacos

Then create a new namespace according to the following tags. The default namespace is public, which is not recommended. It is best to use one namespace for each project.

Back to our configuration list, click on the namespace you just created


Click the blue + sign on the right to create a yaml. This Data Id is suggested to be in this format. If the project has multiple configuration files, use application XX Yaml, the default name is application yaml

This is the default for Group. It is recommended to change it. If you don't change it, there may be problems. Just maybe, I won't change it first. Then select yaml as the type below, and paste all your springboot project configuration files into the box below.

The installation and configuration of nacos are completed. Let's start to put the Springboot configuration file into nacos and use it

2, Put the Springboot configuration file into nacos and use it

1. Introduce dependency

Just these two. Don't trust those who have cited a lot on the Internet. Don't change the version. nacos is disgusting

Maven said

<dependency>
    <groupId>com.alibaba.nacos</groupId>
    <artifactId>nacos-client</artifactId>
    <version>1.4.1</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

Gradle said

compile group: 'com.alibaba.cloud', name: 'spring-cloud-stream-binder-rocketmq', version: '2.2.0.RELEASE'
compile group: 'com.alibaba.nacos', name: 'nacos-client', version: '1.4.1'

2. Configure yaml of nacos

Under resources, create a new bootstrap Properties, yml is OK

It says so

spring.application.name=test #It's not important. You can just write your project name
spring.cloud.nacos.config.group=DEFAULT_GROUP #This group is the one filled in when it was created above
spring.main.allow-bean-definition-overriding=true

#nacos configuration center
spring.cloud.nacos.config.server-addr=111.111.111.111:8848  #This IP is the address of your nacos
#Namespace
spring.cloud.nacos.config.namespace=51f82ad7-11fd-413a-9b0a-fd11ee1213d7 #This is the long string of characters on the right of the namespace test you created. Look at the picture above
#Default load configuration Prefix suffix
spring.cloud.nacos.config.prefix=application
spring.cloud.nacos.config.file-extension=yaml

#Additional configuration files. This is what I said above. If a project has multiple configuration files to start, you can create another file in nacos. I only use one, so the following comments are omitted
#database
#spring.cloud.nacos.config.shared-configs[0].data-id=application-database.yaml
#spring.cloud.nacos.config.shared-configs[0].group=DEFAULT_GROUP

#This sentence "true" means that the configuration file in nacos is used first. Note that it is only preferred. That is, both nacos and local configurations work. If there are local configurations but nacos does not, the local configurations that are more than nacos will still be loaded
#If it is false, it does not use the in nacos, but the local configuration file takes effect
spring.cloud.nacos.config.enabled=true

Then it's all over and start the project directly

summary

nacos in this article is a stand-alone deployment. Later, we will study the cluster deployment with Dubbo

Keywords: Java Linux MySQL Docker Spring Boot

Added by hkay1 on Sat, 25 Dec 2021 17:34:16 +0200