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