当前位置:网站首页>Config server configuration center of Nacos series

Config server configuration center of Nacos series

2022-06-13 05:40:00 codain

We talked about before Naming Server, This is Nacos One of the two functions , For registration and discovery , So the other one is Config Server, This is the configuration center , amount to SCN Medium Spring Cloud Config, So we use the direct integration method , Let's experience it Config Server


1、 Create a monomer SpringBoot engineering

POM The information is as follows :

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-discovery-spring-boot-starter</artifactId>
            <version>0.2.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

2、 add to ConfigServer Configuration information

nacos.config.server-addr=127.0.0.1:8848

3、 Create an interface class ConfigController, Test remote reading of configuration center data

@RestController
                    // Configure data sources            Whether to automatically refresh 
@NacosPropertySource(dataId = "nacos",autoRefreshed = true)
public class ConfigController {
    @NacosValue(value = "${value:Hello world}",autoRefreshed = true)
    private String value;

    @GetMapping("/config")
    public String get(){
        return value;
    }
}

4、 start-up Nacos

There are two ways to create configuration information

① Call directly Nacos Of API Interface :

http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos&group=DEFAULT_GROUP&content= Hello , The world

② Use Nacos Console direct configuration

 5、 The browser accesses the local interface

visit :http://localhost:8080/config

The return is Nacos Configure the contents of the center : Hello , The world

This is the end of the integration test , You can also continue to call API Or modify it on the console value value , Then ask again , Take a look at the configuration above autoRefreshed Whether the automatic refresh takes effect  

原网站

版权声明
本文为[codain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280508407104.html