Browse Source

增加配置文件读取

tags/v1.0.0^2
林万龙 4 years ago
parent
commit
0f95841bde
2 changed files with 9 additions and 38 deletions
  1. +6
    -1
      src/main/java/com/telpo/dipperposition/config/PositionConfig.java
  2. +3
    -37
      src/main/java/com/telpo/dipperposition/controller/DipperPositionController.java

+ 6
- 1
src/main/java/com/telpo/dipperposition/config/PositionConfig.java View File

@@ -1,5 +1,8 @@
package com.telpo.dipperposition.config; package com.telpo.dipperposition.config;


import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
@@ -7,6 +10,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;


import java.util.Properties;

/** /**
* @program: gateway * @program: gateway
* @description: jwt设置 * @description: jwt设置
@@ -20,7 +25,7 @@ import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "position") @ConfigurationProperties(prefix = "position")
public class PositionConfig { public class PositionConfig {


@Value("${hello}")
//@Value("${hello}")
private String hello; private String hello;


} }

+ 3
- 37
src/main/java/com/telpo/dipperposition/controller/DipperPositionController.java View File

@@ -24,48 +24,14 @@ public class DipperPositionController {


@RequestMapping("/getPos") @RequestMapping("/getPos")
public String getPos() throws NacosException, InterruptedException { public String getPos() throws NacosException, InterruptedException {
String serverAddr = "localhost";
String dataId = "dipperposition-service-dev";
String group = "DEFAULT_GROUP"; String group = "DEFAULT_GROUP";

String positionId = "String.hello";
String positionId = "position.hello";
Properties properties = new Properties(); Properties properties = new Properties();
String serverAddr = "172.16.192.26";
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties); ConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(dataId, group, 10000);
String content = configService.getConfig(positionId, group, 10000);
System.out.println(content); System.out.println(content);
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
log.info("recieve:" + configInfo);
}

@Override
public Executor getExecutor() {
return null;
}
});

boolean isPublishOk = configService.publishConfig(dataId, group, "content");
log.info(String.valueOf(isPublishOk));
Thread.sleep(3000);

configService = NacosFactory.createConfigService(properties);
content = configService.getConfig(dataId, group, 10000);
log.info(content);

content = configService.getConfig(positionId, group, 10000);
log.info(content);

//boolean isRemoveOk = configService.removeConfig(dataId, group);
//log.info(String.valueOf(isRemoveOk));
//Thread.sleep(3000);

//content = configService.getConfig(dataId, group, 10000);
//log.info(content);
//Thread.sleep(30000);

//System.out.println(content);


return "Helle world!"; //ContextLoader.getCurrentWebApplicationContext().toString(); return "Helle world!"; //ContextLoader.getCurrentWebApplicationContext().toString();
} }


Loading…
Cancel
Save