diff --git a/pom.xml b/pom.xml index 3a9aa0a..382d7f3 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,11 @@ spring-cloud-starter-alibaba-nacos-config + + com.alibaba.nacos + nacos-client + + org.springframework.boot spring-boot-starter-webflux diff --git a/src/main/java/com/telpo/dipperposition/controller/DipperPositionController.java b/src/main/java/com/telpo/dipperposition/controller/DipperPositionController.java index 6a9b7bc..bb3f9d2 100644 --- a/src/main/java/com/telpo/dipperposition/controller/DipperPositionController.java +++ b/src/main/java/com/telpo/dipperposition/controller/DipperPositionController.java @@ -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(); } }