Spring Cloud Alibaba(6)---Nacos persistent Mysql8.0 version

I have written three articles about Nacos before.

Spring Cloud Alibaba(3)---Nacos overview

Spring Cloud Alibaba(4)---Nacos (Registration Center)

Spring Cloud Alibaba(5)---Nacos (Configuration Center)

1. Background

Where are the information of our service and configuration? The official website has said

当我们使用默认配置启动Nacos时,所有配置文件都被Nacos保存在了内置的数据库中。

The built-in database referred to here is actually in memory. Since it is configured in memory, every time we restart Nacos, all the configured information will be lost. This is obviously not acceptable to us, so we need to configure ,

Let the configuration data be stored in Mysql, so that when we restart the server, the configuration data is still there.

架构信息

Nacos 1.4.2 + MYSQL 8.0.22

Two, Nacos client deployment

There are actually two official methods for client deployment. One is  to download the source code from Github . The other is  to download the compressed package directly . Official address

When writing an overview of Nacos before , I went directly to the official website to download the compressed package of nacos-server-1.3.2.tar.gz, downloaded and decompressed and then run it through commands. Here we need to use the second method, after we directly pull the nacos source code,

Tie into the nacos-server-1.4.2-SNAPSHOT.tar.gz compressed package by yourself.

Why not use the compressed package provided directly by the official, but we need to download the source code and mark it into a compressed package, so many times in one fell swoop?

It's because we are MYSQL 8.0.22 version, so we have to download the source code, change the corresponding database-connected driver jar package in the pom file to version 8.0.22, and then repack it into a compressed package.

1. Pull the source code

This currently pulls Nacos version 1.4.2, you can also specify the pull version

git clone https://github.com/alibaba/nacos.git

2. Modify the Mysql connection driver

The version of mysql-connector-java used by Nacos 1.4.2 is 8.0.21, which is modified to 8.0.22 here.

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <version>8.0.22</version>
  5. </dependency>

注意 In fact, there is no need to modify here, because the difference between 8.0.21 and 8.0.22 is very small, so we don't need to modify it. That is to say, we don’t need to pull the source code to package through commands, but directly download version 1.4.2

The compressed package will do. Of course, if your version of Nacos is below 1.3.1, then the Mysql driver is version 5.7, then this needs to be modified to version 8 or higher.

3. Packing

Enter the nacos directory

cd nacos

maven packaging

mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U

If it succeeds directly, distribution/target/there will be a corresponding unzipped package in the directory.

4. Unzip

tar -xvf nacos-server-1.4.2-SNAPSHOT.tar.gz

Next, we do not start the server, because we also need to modify some configuration files

Three, modify the configuration file

1. Initialize the database

The Nacos database script file is in the compressed package when we downloaded Nacos-server

进入nacos\conf目录,初始化文件:nacos-mysql.sql

Here I create a nacos database named  , and then execute the initialization script, after success, 12 tables will be generated

2. Modify the configuration file

Its configuration file is also in the  nacos\confdirectory, named  application.properties, add data source configuration at the bottom of the file:

  1. spring.datasource.platform=mysql
  2. db.num=1
  3. db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
  4. db.user=root
  5. db.password=123456

3. Start the server

Now that the database and configuration have been modified, then start the server

  1. //先进入bin目录
  2. cd nacos/bin

Start command (standalone represents stand-alone mode operation, non-cluster mode):

sh startup.sh -m standalone

If you are using the ubuntu system, or the running script reports an error message [[ symbol cannot be found, you can try to run as follows:

bash startup.sh -m standalone

注意 The place to execute the command is under the distribution/target/nacos-server-1.4.2-SNAPSHOT/nacos/bin compressed above   , not under the distribution bin.

After startup, if you can access the following address normally, it means that the configuration has been successful.

http://127.0.0.1:8848/nacos

If the above access fails, go to the  nacos\log  directory and name it to  nacos.log see if there is an error log

Four tests

1. Add a new configuration data on the client

Looking at the list also shows that this configuration set has been created successfully

2. View the database

Now that the client is successfully generated, let's see if this data is persisted in the database

Obviously there is this data in the data, so even if nacos restarts this data, it will still be there.

reference

1. How about learning Spring Cloud? The interviewers admire this Alibaba notebook manual thoroughly.

2. Alibaba's new generation of microservices, the notes + brain maps handwritten by the internal bosses are not to be missed, they are all the essence

3. Java code optimization skills, gold, nine, silver and ten salary doubles rely on it