Parcourir la source

增加配置文件读取

tags/v1.0.0^2
林万龙 il y a 3 ans
Parent
révision
b51c9e1ff1
2 fichiers modifiés avec 53 ajouts et 10 suppressions
  1. +5
    -0
      pom.xml
  2. +48
    -10
      src/main/java/com/telpo/dipperposition/controller/DipperPositionController.java

+ 5
- 0
pom.xml Voir le fichier

@@ -51,6 +51,11 @@
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>


+ 48
- 10
src/main/java/com/telpo/dipperposition/controller/DipperPositionController.java Voir le fichier

@@ -1,18 +1,16 @@
package com.telpo.dipperposition.controller;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.ContextLoader;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executor;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;

/**
* @program: DipperPositionController
@@ -25,7 +23,47 @@ import java.util.List;
public class DipperPositionController {

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

String positionId = "String.hello";
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(dataId, group, 5000);
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);

content = configService.getConfig(dataId, group, 5000);
log.info(content);

content = configService.getConfig(dataId, group, 5000);
log.info(content);

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

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

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

Chargement…
Annuler
Enregistrer