Update java.md
This commit is contained in:
parent
ace88ac67d
commit
b149d9dfcc
79
java.md
79
java.md
|
@ -1,64 +1,27 @@
|
||||||
/*
|
// Refer to document: https://github.com/nacos-group/nacos-examples/tree/master/nacos-spring-boot-example/nacos-spring-boot-config-example
|
||||||
* Demo for Nacos
|
package com.alibaba.nacos.example.spring.boot.controller;
|
||||||
* pom.xml
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba.nacos</groupId>
|
|
||||||
<artifactId>nacos-client</artifactId>
|
|
||||||
<version>${version}</version>
|
|
||||||
</dependency>
|
|
||||||
*/
|
|
||||||
package com.alibaba.nacos.example;
|
|
||||||
|
|
||||||
import java.util.Properties;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import java.util.concurrent.Executor;
|
import org.springframework.stereotype.Controller;
|
||||||
import com.alibaba.nacos.api.NacosFactory;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import com.alibaba.nacos.api.config.ConfigService;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import com.alibaba.nacos.api.config.listener.Listener;
|
|
||||||
import com.alibaba.nacos.api.exception.NacosException;
|
|
||||||
|
|
||||||
/**
|
import static org.springframework.web.bind.annotation.RequestMethod.GET;
|
||||||
* Config service example
|
|
||||||
*
|
|
||||||
* @author Nacos
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class ConfigExample {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws NacosException, InterruptedException {
|
@Controller
|
||||||
String serverAddr = "localhost";
|
@RequestMapping("config")
|
||||||
String dataId = "application-prod.yml";
|
public class ConfigController {
|
||||||
String group = "DEFAULT_GROUP";
|
|
||||||
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) {
|
|
||||||
System.out.println("receive:" + configInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Value("${useLocalCache:false}")
|
||||||
public Executor getExecutor() {
|
private boolean useLocalCache;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
boolean isPublishOk = configService.publishConfig(dataId, group, "content");
|
public void setUseLocalCache(boolean useLocalCache) {
|
||||||
System.out.println(isPublishOk);
|
this.useLocalCache = useLocalCache;
|
||||||
|
}
|
||||||
|
|
||||||
Thread.sleep(3000);
|
@RequestMapping(value = "/get", method = GET)
|
||||||
content = configService.getConfig(dataId, group, 5000);
|
@ResponseBody
|
||||||
System.out.println(content);
|
public boolean get() {
|
||||||
|
return useLocalCache;
|
||||||
boolean isRemoveOk = configService.removeConfig(dataId, group);
|
}
|
||||||
System.out.println(isRemoveOk);
|
}
|
||||||
Thread.sleep(3000);
|
|
||||||
|
|
||||||
content = configService.getConfig(dataId, group, 5000);
|
|
||||||
System.out.println(content);
|
|
||||||
Thread.sleep(300000);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue