Springboot practice (17) spring boot integrates Nacos configuration center

        In the previous article, we explained the download and installation of the Nacos server. In this article, we reduce the price of spring boot and integrate nacos to achieve access to the Nacos server configuration parameters.

1. Start the Nacos service and create three configuration files, as shown below

  • Springboot-Nacos-Client-dev.yaml file configuration parameters

  • Springboot-Nacos-Client.yaml file configuration parameters

  • sjl.yaml file configuration parameters

2. Establish Nacos client

1. Use MyEclipse2019 to create a web project and name it "Springboot-Nacos-Client". The overall picture of the project is as shown below:

2. Modify the pom.xml file

Add the corresponding jar package to the pom file,

<!--Registration center dependencies-->

<dependency>

    <groupId>com.alibaba.cloud</groupId>

    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

    <version>2.1.2.RELEASE</version>

</dependency>

<!-- Configuration center dependencies -->

<dependency>

    <groupId>com.alibaba.cloud</groupId>

    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>

    <version>2.1.2.RELEASE</version>

</dependency>      

<dependency>

    <groupId>com.alibaba.nacos</groupId>

    <artifactId>nacos-client</artifactId>

    <version>2.1.2</version>

</dependency>  

<dependency>

    <groupId>com.alibaba.cloud</groupId>

    <artifactId>spring-cloud-alibaba-dependencies</artifactId>

    <version>2.1.2.RELEASE</version>

    <type>pom</type>

    <scope>import</scope>

</dependency>

Note: Please note that the version of the above jar package needs to be consistent with the matching version released by the Nacos official website, otherwise various strange problems will occur.

3. Modify the application.yml file

The contents of the file are as follows:

spring:

  application:

    name: Springboot-Nacos-Client

  profiles:

    active: dev #Activate configuration

  cloud:

 nacos:

   discovery:

     server-addr: 127.0.0.1:8848

     username: nacos

     password: nacos

      config:

       enabled: true # Whether to enable the configuration center, the default is true

       server-addr: 127.0.0.1:8848 #nacos address

       username: nacos

password: nacos

       namespace: 7fe8fb2f-9e3a-438b-bf9a-1a0ca8d4c898 

       group: test

       prefix: ${spring.application.name}

       file-extension: yaml #File suffix, can be omitted and not configured      

       extension-configs:

          - data-id: sjl.yaml

            group: test

                refresh: true

             - data-id: Springboot-Nacos-Client.yaml

               group: test

               refresh: true

Remark:

spring-cloud-starter-alibaba-nacos-config default loading file

Load the basic configuration with dataid as ${spring.application.name}.${file-extension:properties} as prefix,

Load the basic configuration with dataid ${spring.application.name}-${profile}.${file-extension:properties}

extension-configs: #Extension configuration, used to introduce multiple configurations; when there are multiple configuration files, avoid system parameter configuration%

Profile priority

The relevant Data Id is automatically generated through internal related rules (application name, extension, profiles). The configuration priority is the highest. The configuration in nacos takes precedence over the local configuration. The local bootstrap.yml>bootstrap.properties>application.yml>application.yaml> application.properties

Extension configuration (extension-configs) > Shared configuration (shared-configs)

They are both extension configurations and have the following priority relationship: extension-configs[3] > extension-configs[2] > extension-configs[1] > extension-configs[0]

They are both shared configurations and have the following priority relationship: shared-configs[3] > shared-configs[2] > shared-configs[1] > shared-configs[0]

4. Add startup function

@SpringBootApplication(scanBasePackages= {"com.SJL"},exclude = {

        DataSourceAutoConfiguration.class,

        DataSourceTransactionManagerAutoConfiguration.class,

        HibernateJpaAutoConfiguration.class})

@ComponentScan(basePackages= {"com.SJL"})

@ServletComponentScan(basePackages= {"com.SJL"})

@EnableDiscoveryClient

public class ConfingClientApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConfingClientApplication.class, args);

    }              

}

5. Add Controller to access Nacos configuration file parameters

Create three Controller files, as follows:

☆NacosConfigController file

@RequestMapping("/config")

@RestController

@RefreshScope

@NacosConfigurationProperties(dataId = "Springboot-Nacos-Client-dev.yaml", groupId = "test", autoRefreshed = true)

public class NacosConfigController {

    @Value(value = "${config.name}")

    private String configName;

    @NacosInjected

    private ConfigService configService;

    @GetMapping("getConfigName")

    public String getConfigName(){

        return configName;

    }      

}

☆NacosConfigController2 file

@RequestMapping("/config2")

@RestController

@RefreshScope

@NacosConfigurationProperties(dataId = "Springboot-Nacos-Client.yaml", groupId = "test", autoRefreshed = true)

public class NacosConfigController2 {

    @Value(value = "${auther}")

    private String auther;

    @NacosInjected

    private ConfigService configService;

    @GetMapping("getAuther")

    public String getAuther(){

        return auther;

    }

}

☆NacosConfigController3 file

RequestMapping("/config3")

@RestController

@RefreshScope

@NacosConfigurationProperties(dataId = "sjl.yaml", groupId = "test", autoRefreshed = true)

public class NacosConfigController3 {

    @Value(value = "${name}")

    private String name;

    @GetMapping("getName")

    public String getName(){

      return name;

    }   

}

3. Test

In the browser, enter "localhost:2881/swagger-ui.html" and test the return results of the three controllers, as shown below, you can get the results.

Guess you like

Origin blog.csdn.net/qq_37647812/article/details/132908802