springboot使用nacos配置中心-01入门

添加maven依赖

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

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

application.properties

nacos.config.server-addr=118.184.218.184:8848

springboot启动类

@EnableAutoConfiguration
@SpringBootApplication
@NacosPropertySource(
        name = "custom",
        dataId = "nacos_config",
        first = true,
        groupId = "nacos_group",
        autoRefreshed = true, //自动刷新
        before = SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,
        after = SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME
)
public class ConfigApplication {

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

}

controller测试类

@RestController
public class NacosController {

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

    @Autowired
    private User user;

    @RequestMapping(value = "/sayHello")
    public String dubboSayHello(){
        System.out.println(name);
        return name;
    }

    //监听nacos配置文件的变化
    @NacosConfigListener(
            dataId = "nacos_config",
            groupId = "nacos_group",
            timeout = 500
    )
    public void onChange(String newContent) throws Exception {
        System.out.println("onChange : " + newContent);
    }

    @NacosConfigListener(
            dataId = "nacos_config2",
            groupId = "nacos_group",
            timeout = 500
    )
    public void onChange2(String newContent) throws Exception {
        System.out.println("onChange : " + newContent);
    }

}

User对象

@Data
@NacosConfigurationProperties(dataId = "nacos_config2" ,groupId = "nacos_group" ,autoRefreshed = true)
@Component
public class User {

    private String name;

}

配置中心添加配置

测试

会发现对象和变量都有值 修改配置中心的值会立马刷新变量的值

json配置的测试

       try {
            String serverAddr = "118.184.218.184:8848";
            String dataId = "json";
            String group = "DEFAULT_GROUP";
            Properties properties = new Properties();
            properties.put("serverAddr", serverAddr);
            //这里设置了dev空间的配置
            properties.put("namespace" ,"7d32a129-a6ca-4ba1-a75e-96aaed89da33");
            ConfigService configService = NacosFactory.createConfigService(properties);
            // Actively get the configuration.
            String content = configService.getConfig(dataId, group, 5000);
            System.out.println(content);
        } catch (NacosException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

demo:https://github.com/ciweigg2/springboot-nacos-config.git


文章作者: Ciwei
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Ciwei !
 上一篇
springboot使用nacos配置中心-02典型的应用场景 springboot使用nacos配置中心-02典型的应用场景
数据库连接信息曾经有朋友跟我聊过一个问题,“业务飞速发展,团队越来越大,人员流动也相对频繁起来,怎么才能更好的保证数据的安全性,不被泄露呢?”。他提到这样一个场景,公司创立初期,服务后端的代码都是他一行一行码出来的,当时只有他一个人,后端与
2019-01-13
下一篇 
docker搭建nacos注册中心 docker搭建nacos注册中心
操作步骤 Clone 项目git clone https://github.com/nacos-group/nacos-docker.git cd nacos-docker 单机模式 docker-compose -f examp
2019-01-13
  目录