1955°

nacos入门--配置中心Demo

nacos启动 添加配配置

首先下载启动nacos (文档地址)[https://nacos.io]

或者看(上一篇文章)[http://liflag.cn/post/39]

启动后添加一个配置

springboot集成

pom文件中添加nacos依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
	
    <!--nacos配置中心依赖-->
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-config-spring-boot-starter</artifactId>
        <version>0.2.1</version>
    </dependency>
</dependencies>

启动类中添加nacos配置注解

/**
 * dataId 在nacos中配置的dataId
 * autoRefreshed 自动推送到
 */
@SpringBootApplication
@NacosPropertySource(dataId = "TEST", autoRefreshed = true)
public class NacosFirstApplication {

	public static void main(String[] args) {
		SpringApplication.run(NacosFirstApplication.class, args);
	}

}

controlle类中读取配置展示


@Controller
@RequestMapping(value = "config")
public class NacosController {

    @NacosValue(value = "${test:welcome}", autoRefreshed = true)
    private String useLocalCache;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public String get() {
        return useLocalCache;
    }


}

别忘了配置文件中添加nacos地址

nacos.config.server-addr=127.0.0.1:8848

启动成功后 访问地址


已有 0 条评论

    我有话说: