springboot2.X integrates nacos to realize registration center and configuration center

nacos official manual https://nacos.io/zh-cn/docs/what-is-nacos.html

1. Environmental preparation

This article uses spring boot 2 1.9. RELEASE,nacos1.3.2 ,JDK 1.8+ ,mysql5.6.5 + this paper adopts Windows deployment environment

2. Download the source code or installation package

You can get Nacos through source code and distribution package.

1. Download the source code from Github 2. Download the compressed package after compilation

For the convenience of this article, download the zip package and unzip it. Address: Latest stable version Download Nacos server - $version Zip package.

3. Start the server

Nacos supports three deployment modes

  • Stand alone mode - used for testing and stand-alone trial.
  • Cluster mode - used in production environment to ensure high availability.
  • Multi cluster mode - used in multi data center scenarios.
  1. Stand alone deployment:

Unzip nacos-server-1.3.2 Zip package, go to nacos\conf, and you can see the mysql initialization script. You can refer to the selection of Nacos data storage mode https://blog.csdn.net/thinkingcao/article/details/105444718 , because this paper adopts single machine deployment, for the convenience of data viewing, mysql database is used to store data.

Copy and execute Nacos mysql SQL script creates and initializes the database. Modify application The properties configuration file configures database connection information

#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql

### Count of DB:
db.num=1

### Connect URL of DB:
db.url.0=jdbc:mysql://localhost:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=root

nacos is started by cluster by default. You can modify startup CMD configuration file is in stand-alone startup mode: stand-alone stands for stand-alone mode and non cluster mode

Double click Startup CMD start the nacos server. visit http://localhost:8848/nacos/index.html , login default account: nacos, password: nacos

Nacos configuration

Namespace

nacos uses namespace for environment isolation, which can specify different environments to better manage the configuration file management of development, testing and production

Resource allocation

Data ID

The format of Data ID is as follows:

reference resources: https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

${prefix}-${spring.profile.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.profile.active is the profile corresponding to the current environment. Note: when spring profile. 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.

    Group

    The default is DEFAULT_GROUP, you can group and manage different types of micro service configuration files. Configuration file through spring cloud. nacos. config. Group = AAA.

Configuration content

The configuration file format supports several types of TEXT, JSON, XML, YAML, HTML and Properties

Historical version

Each modification of the resource file will record a historical version. The default saving time of the historical record is 30 days. You can see the content of each update according to the historical record. You can also roll back the specified log file to the previous version.

Listening query

You can listen to which ip addresses access each specific resource file

2. Cluster deployment:

Modify the configuration file cluster Conf.example , rename to cluster Conf and modify the content

Note: ip is required for Windows deployment.   

Just start.

4. Spring boot integrates nacos as the registration center and configuration center

  • Add dependency to pom file
  <properties>
    <spring.cloud.alibaba.version>2.1.0.RELEASE</spring.cloud.alibaba.version>
    <spring-boot.version>2.1.9.RELEASE</spring-boot.version>
  </properties>
  <dependencyManagement>
  <dependencies>

      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    <!--Spring cloud alibaba 2.1.0.RELEASE-->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>${spring.cloud.alibaba.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
  </dependencyManagement>

<dependencies>
    <!--nacos-config-->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <!--nacos-discovery-->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!--web + actuator-->
    <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>
  </dependencies>
  • Create a new bootstrp. In the resources directory Properties file

Note: the bootstrap is loaded by the parent Spring ApplicationContext. The parent ApplicationContext is loaded before using the application. When using Spring Cloud, the configuration information is usually loaded from the config server. In order to obtain the configuration information (such as password), you need some early or boot configuration. Therefore, put the config server information in the bootstrap to load the really needed configuration information.

server:
  port: 8080
# nacos configuration
spring:
  application:
    #The reason why you need to configure spring application. Name, because it is part of the Nacos configuration management dataId field.
    name: nacos-config
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #Nacos service registry address
        namespace: e5b58bd9-88b8-4acd-a81f-55c0e53426b4  # Default public namespace when namespace is not specified
      config:
        server-addr: 127.0.0.1:8848 #Nacos as configuration center address
        file-extension: yaml #Specifies the configuration of yaml format
        group: DEFAULT_GROUP
        namespace: e5b58bd9-88b8-4acd-a81f-55c0e53426b4 # Default public namespace when namespace is not specified

# Naming rules for configuration files in Nacos
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# nacos-config-client-dev.yaml this file name is the file name of the new configuration file in Nacos

* application found during use YML and bootstrap YML has the problem of attribute coverage, that is, when the same attribute is configured in both configuration files, the application configuration will overwrite the bootstrap attribute value. During configuration, pay more attention to the bifurcation and avoid repeated configuration.

  • Add annotation information to startup class @ EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
    public static void main(String [] args){
        SpringApplication.run(Application.class, args);
    }
}

To make the configuration file effective in real time, you need to add comments: @ RefreshScope

*When the annotation @ RefreshScope is not added during use, the dynamic loading of configuration can also be realized, and the specific situation is treated on a case by case basis.

@RequestMapping("/testNacosControlle")
@RestController
@RefreshScope
public class TestNacosControlle {

}

 

Tags: Nacos

Posted by gotDNS on Sat, 07 May 2022 12:36:33 +0300