美文网首页
基于nacos实现外部配置config

基于nacos实现外部配置config

作者: 凌康ACG | 来源:发表于2019-12-16 02:07 被阅读0次

    基于上一篇文章的项目实现:https://www.jianshu.com/p/e11ff097424b

    一、运行nacos

    nacos官网https://nacos.io下载即可
    运行,访问:http://127.0.0.1:8848/nacos/index.html账号密码都是nacos
    添加一个配置

    image.png
    将配置填写后,右下角的发布按钮:
    image.png
    在消费者的pom.xml加入依赖:
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
                <version>0.9.0.RELEASE</version>
            </dependency>
    

    删除application.yml配置,添加bootstrap.properties:

    #名称对上nacos创建的 consumer,注意此处少了后缀 .yaml
    spring.application.name=consumer
    spring.cloud.nacos.config.server-addr=127.0.0.1:8848
    spring.cloud.nacos.config.file-extension=yaml
    

    完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中,在controller中添加:


    image.png

    1、启动生产者

    2、启动消费者

    访问http://localhost:8080/sayHi/lingkang

    image.png
    然后在nacos改下值:
    image.png
    image.png

    相关文章

      网友评论

          本文标题:基于nacos实现外部配置config

          本文链接:https://www.haomeiwen.com/subject/bxhrnctx.html