Browse Source

增加配置文件读取

tags/v1.0.0^2
林万龙 3 years ago
parent
commit
8ff69ce56f
1 changed files with 10 additions and 4 deletions
  1. +10
    -4
      src/main/java/com/telpo/dipperposition/controller/DipperPositionController.java

+ 10
- 4
src/main/java/com/telpo/dipperposition/controller/DipperPositionController.java View File

@@ -1,11 +1,14 @@
package com.telpo.dipperposition.controller;

import com.alibaba.cloud.nacos.parser.NacosDataParserHandler;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import com.alibaba.nacos.api.NacosFactory;
@@ -23,16 +26,19 @@ import com.alibaba.nacos.api.config.listener.Listener;
public class DipperPositionController {

@RequestMapping("/getPos")
public String getPos() throws NacosException, InterruptedException {
public String getPos() throws NacosException, InterruptedException, IOException {
String group = "DEFAULT_GROUP";
String dataId = "dipperposition-service";
String positionId = "position.hello";
Properties properties = new Properties();
String serverAddr = "172.16.192.26";
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(positionId, group, 10000);
String content = configService.getConfig(dataId, group, 10000);
System.out.println(content);

return "Helle world!"; //ContextLoader.getCurrentWebApplicationContext().toString();
//log.info(positionId + ":" + content.positionId)
Map<String, Object> dataMap = NacosDataParserHandler.getInstance().parseNacosData(content,"yaml");
return dataMap == null ? "" : (String)dataMap.get(positionId);
//return "Helle world!"; //ContextLoader.getCurrentWebApplicationContext().toString();
}
}

Loading…
Cancel
Save