暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Nacos Java SDK(获取配置getConfig)

安全链 2021-08-20
2007

POM文件

在POM中增加如下依赖


<dependency>
    <groupId>com.alibaba.nacos</groupId>
    <artifactId>nacos-client</artifactId>
    <version>1.4.1</version>
</dependency>


获取配置

用于服务启动的时候从 Nacos 获取配置


package com.SCND.Controller;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

@RestController
public class GetConfig {

    @GetMapping("/getConfig")
    public Map getConfig() throws NacosException {
        Map map = new HashMap();
        try {
            String serverAddr = "127.0.0.1:8848";
            String dataId = "example.properties";
            String group = "DEFAULT_GROUP";
            int timeoutMs = 5000;
            Properties properties = new Properties();
            properties.put("serverAddr", serverAddr);
            ConfigService configService = NacosFactory.createConfigService(properties);
            String content = configService.getConfig(dataId, group, timeoutMs);
            map.put("content", content);
            System.out.println(content);
        } catch (NacosException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }


使用浏览器访问


http://localhost:8080/getConfig?timeoutMs=5000


图1  浏览器访问


图2  工程启动


图3  Nacos配置界面



链接:https://pan.baidu.com/s/1beliohUojI9e15UzMgJ30Q 
提取码:fyer


文章转载自安全链,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论