nacos 配置中心

nocas 添加配置列表

在这里插入图片描述
在这里插入图片描述
Group 可不填 默认即可
Data ID 是工程使用配置中心配置的标识
最后点击发布

导包

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.1</version>
        </dependency>

controller

package com.yss.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yss
 */
@RestController
public class getMessage {

    @NacosValue(value = "${message}",autoRefreshed = true)
    private String message;

    @GetMapping("getMessage")
    public String getMessage(){
        return message;
    }
}

autoRefreshed 必须配置为true,在下面会有说明为什么必须配置

yml 配置

nacos:
  config:
    server-addr: localhost:8848
  discovery:
    server-addr: localhost:8848

主函数配置

package com.yss;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@NacosPropertySource(dataId = "yss", autoRefreshed = true)
@EnableDiscoveryClient
public class SameConfigureApplication {

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

}

其中的dataId就是nacos配置的data id
autoRefreshed 必须配置为true

这里一共配置了autoRefreshed 一个在主函数了,一个在引用配置中心的字段的地方,两个必须配置,原因是来完成热加载.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值