Java Code Examples for com.alibaba.nacos.api.config.ConfigService#getConfig()

The following examples show how to use com.alibaba.nacos.api.config.ConfigService#getConfig() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NacosUtil.java    From anyline with Apache License 2.0 6 votes vote down vote up
/**
 * 读取配置文件 并 监听配置更新
 * @param group group
 * @param data data
 * @param listener listent
 * @return String
 * @throws NacosException NacosException
 */
public String config(String group, String data, Listener listener) throws NacosException{
	if(BasicUtil.isEmpty(group)){
		group = config.GROUP;
	}
	log.warn("[nacos config][group:{}][data:{}][listener:{}]", group, data, listener);
	Properties properties = new Properties();
	properties.put(PropertyKeyConst.NAMESPACE, config.NAMESPACE);
	properties.put(PropertyKeyConst.SERVER_ADDR, config.ADDRESS+":"+config.PORT);
	ConfigService configService = NacosFactory.createConfigService(properties);
	String content = configService.getConfig(data, group, config.TIMEOUT);
	if(null != listener) {
		configService.addListener(data, group, listener);
	}
	return content;
}
 
Example 2
Source File: DynamicRouteServiceImplByNacos.java    From spring-cloud-gateway-nacos with Apache License 2.0 6 votes vote down vote up
/**
 * 监听Nacos Server下发的动态路由配置
 * @param dataId
 * @param group
 */
public void dynamicRouteByNacosListener (String dataId, String group){
    try {
        ConfigService configService=NacosFactory.createConfigService("127.0.0.1:8848");
        String content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);
        configService.addListener(dataId, group, new Listener()  {
            @Override
            public void receiveConfigInfo(String configInfo) {
                RouteDefinition definition= JSON.parseObject(configInfo,RouteDefinition.class);
                dynamicRouteService.update(definition);
            }
            @Override
            public Executor getExecutor() {
                return null;
            }
        });
    } catch (NacosException e) {
        //todo 提醒:异常自行处理此处省略
    }
}
 
Example 3
Source File: PropertiesAssemble.java    From spring-cloud-zuul-nacos with Apache License 2.0 6 votes vote down vote up
private List<ZuulRouteEntity> listenerNacos (String dataId, String group) {
		try {
			Properties properties = new Properties();
			properties.put(PropertyKeyConst.SERVER_ADDR, "localhost:8848");
			ConfigService configService = NacosFactory.createConfigService(properties);
			String content = configService.getConfig(dataId, group, 5000);
			System.out.println("从Nacos返回的配置:" + content);
			//注册Nacos配置更新监听器
//            configService.addListener(dataId, group, new Listener()  {
//                @Override
//                public void receiveConfigInfo(String configInfo) {
//                    System.out.println("Nacos更新了!");
//
//                }
//                @Override
//                public Executor getExecutor() {
//                    return null;
//                }
//            });
			return JSONObject.parseArray(content, ZuulRouteEntity.class);
		} catch (NacosException e) {
			e.printStackTrace();
		}
		return new ArrayList<>();
	}
 
Example 4
Source File: NacosUtils.java    From nacos-spring-project with Apache License 2.0 5 votes vote down vote up
/**
 * Get content from {@link ConfigService} via dataId and groupId
 *
 * @param configService {@link ConfigService}
 * @param dataId dataId
 * @param groupId groupId
 * @return If available , return content , or <code>null</code>
 */
public static String getContent(ConfigService configService, String dataId,
		String groupId) {
	String content = null;
	try {
		content = configService.getConfig(dataId, groupId, DEFAULT_TIMEOUT);
	}
	catch (NacosException e) {
		if (logger.isErrorEnabled()) {
			logger.error("Can't get content from dataId : " + dataId + " , groupId : "
					+ groupId, e);
		}
	}
	return content;
}
 
Example 5
Source File: NacosConfigManager.java    From jboot with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化 nacos 配置监听
 */
public void init(JbootConfigManager configManager) {

    NacosServerConfig nacosServerConfig = configManager.get(NacosServerConfig.class);
    if (!nacosServerConfig.isEnable() || !nacosServerConfig.isConfigOk()) {
        return;
    }

    try {

        ConfigService configService = NacosFactory.createConfigService(nacosServerConfig.toProperties());
        String content = configService.getConfig(nacosServerConfig.getDataId()
                , nacosServerConfig.getGroup(), 3000);

        if (StrUtil.isNotBlank(content)) {
            contentProperties = str2Properties(content);
            if (contentProperties != null) {
                configManager.setRemoteProperties(contentProperties);
            }
        }

        new NacosConfigIniter(this, configManager).initListener(configService, nacosServerConfig);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: FlinkNacosTest2.java    From flink-learning with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args));
    env.setParallelism(1);

    String serverAddr = "localhost";
    String dataId = "test";
    String group = "DEFAULT_GROUP";
    Properties properties = new Properties();
    properties.put("serverAddr", serverAddr);
    ConfigService configService = NacosFactory.createConfigService(properties);
    final String[] content = {configService.getConfig(dataId, group, 5000)};
    configService.addListener(dataId, group, new Listener() {
        @Override
        public Executor getExecutor() {
            return null;
        }

        @Override
        public void receiveConfigInfo(String configInfo) {
            System.out.println("===============");
            content[0] = configInfo;
            env.getCheckpointConfig().setCheckpointInterval(Long.valueOf(content[0]));
            System.out.println("----------");
            System.out.println(env.getCheckpointConfig().getCheckpointInterval());
        }
    });
    System.out.println(content[0]);

    env.getCheckpointConfig().setCheckpointInterval(Long.valueOf(content[0]));
    env.getCheckpointConfig().setCheckpointTimeout(1000);
    env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
    env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.DELETE_ON_CANCELLATION);

    env.addSource(new SourceFunction<Tuple2<String, Long>>() {
        @Override
        public void run(SourceContext<Tuple2<String, Long>> ctx) throws Exception {
            while (true) {
                ctx.collect(new Tuple2<>("zhisheng", System.currentTimeMillis()));
                Thread.sleep(800);
            }
        }

        @Override
        public void cancel() {

        }
    }).print();


    env.execute();
}
 
Example 7
Source File: FlinkNacosTest2.java    From flink-learning with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(ParameterTool.fromArgs(args));
    env.setParallelism(1);

    String serverAddr = "localhost";
    String dataId = "test";
    String group = "DEFAULT_GROUP";
    Properties properties = new Properties();
    properties.put("serverAddr", serverAddr);
    ConfigService configService = NacosFactory.createConfigService(properties);
    final String[] content = {configService.getConfig(dataId, group, 5000)};
    configService.addListener(dataId, group, new Listener() {
        @Override
        public Executor getExecutor() {
            return null;
        }

        @Override
        public void receiveConfigInfo(String configInfo) {
            System.out.println("===============");
            content[0] = configInfo;
            env.getCheckpointConfig().setCheckpointInterval(Long.valueOf(content[0]));
            System.out.println("----------");
            System.out.println(env.getCheckpointConfig().getCheckpointInterval());
        }
    });
    System.out.println(content[0]);

    env.getCheckpointConfig().setCheckpointInterval(Long.valueOf(content[0]));
    env.getCheckpointConfig().setCheckpointTimeout(1000);
    env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
    env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.DELETE_ON_CANCELLATION);

    env.addSource(new SourceFunction<Tuple2<String, Long>>() {
        @Override
        public void run(SourceContext<Tuple2<String, Long>> ctx) throws Exception {
            while (true) {
                ctx.collect(new Tuple2<>("zhisheng", System.currentTimeMillis()));
                Thread.sleep(800);
            }
        }

        @Override
        public void cancel() {

        }
    }).print();


    env.execute();
}