add nacos examples
parent
017bce3794
commit
04a55e4790
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2013-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.examples.configuration;
|
||||
|
||||
import com.alibaba.cloud.examples.model.NacosConfigInfo;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Bean definition
|
||||
*
|
||||
* @author lixiaoshuang
|
||||
*/
|
||||
@Configuration
|
||||
public class BeanConfiguration {
|
||||
|
||||
@Bean
|
||||
public NacosConfigInfo nacosConfigInfo() {
|
||||
return new NacosConfigInfo();
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2013-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.examples.example;
|
||||
|
||||
import com.alibaba.cloud.examples.model.NacosConfigInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Dynamic bean refresh example
|
||||
*
|
||||
* @author lixiaoshuang
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/nacos/bean")
|
||||
public class BeanAutoRefreshConfigExample {
|
||||
|
||||
@Autowired
|
||||
private NacosConfigInfo nacosConfigInfo;
|
||||
|
||||
@GetMapping
|
||||
public Map<String, String> getConfigInfo() {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("serveraddr", nacosConfigInfo.getServeraddr());
|
||||
result.put("prefix", nacosConfigInfo.getPrefix());
|
||||
result.put("group", nacosConfigInfo.getGroup());
|
||||
result.put("namespace", nacosConfigInfo.getNamespace());
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2013-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.examples.example;
|
||||
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||
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.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Configuration listener example.
|
||||
*
|
||||
* @author lixiaoshuang
|
||||
*/
|
||||
@Component
|
||||
public class ConfigListenerExample {
|
||||
|
||||
public static final String DATA_ID = "nacos-config-example.properties";
|
||||
|
||||
public static final String GROUP = "DEFAULT_GROUP";
|
||||
|
||||
@Autowired
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws NacosException {
|
||||
ConfigService configService = nacosConfigManager.getConfigService();
|
||||
|
||||
configService.addListener(DATA_ID, GROUP, new Listener() {
|
||||
@Override
|
||||
public Executor getExecutor() {
|
||||
return Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
System.out.println("[dataId]:[" + DATA_ID + "],Configuration changed to:" + configInfo);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright 2013-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.examples.example;
|
||||
|
||||
import com.alibaba.cloud.commons.lang.StringUtils;
|
||||
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||
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.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* Example of docking with Nacos interface
|
||||
*
|
||||
* @author lixiaoshuang
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("nacos")
|
||||
public class DockingInterfaceExample {
|
||||
|
||||
public static final String DEFAULT_GROUP = "DEFAULT_GROUP";
|
||||
|
||||
@Autowired
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
/**
|
||||
* Get configuration information
|
||||
*
|
||||
* @param dataId
|
||||
* @param group
|
||||
* @return
|
||||
* @throws NacosException
|
||||
*/
|
||||
@RequestMapping("getConfig")
|
||||
public String getConfig(@RequestParam("dataId") String dataId,
|
||||
@RequestParam(value = "group", required = false) String group) throws NacosException {
|
||||
if (StringUtils.isEmpty(group)) {
|
||||
group = DEFAULT_GROUP;
|
||||
}
|
||||
ConfigService configService = nacosConfigManager.getConfigService();
|
||||
return configService.getConfig(dataId, group, 2000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish configuration
|
||||
*
|
||||
* @param dataId
|
||||
* @param group
|
||||
* @param content
|
||||
* @return
|
||||
* @throws NacosException
|
||||
*/
|
||||
@RequestMapping("publishConfig")
|
||||
public boolean publishConfig(@RequestParam("dataId") String dataId,
|
||||
@RequestParam(value = "group", required = false) String group, @RequestParam("content") String content)
|
||||
throws NacosException {
|
||||
if (StringUtils.isEmpty(group)) {
|
||||
group = DEFAULT_GROUP;
|
||||
}
|
||||
ConfigService configService = nacosConfigManager.getConfigService();
|
||||
return configService.publishConfig(dataId, group, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete configuration
|
||||
*
|
||||
* @param dataId
|
||||
* @param group
|
||||
* @return
|
||||
* @throws NacosException
|
||||
*/
|
||||
@RequestMapping("remoteConfig")
|
||||
public boolean remoteConfig(@RequestParam("dataId") String dataId,
|
||||
@RequestParam(value = "group", required = false) String group) throws NacosException {
|
||||
if (StringUtils.isEmpty(group)) {
|
||||
group = DEFAULT_GROUP;
|
||||
}
|
||||
ConfigService configService = nacosConfigManager.getConfigService();
|
||||
return configService.removeConfig(dataId, group);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add listener configuration information
|
||||
*
|
||||
* @param dataId
|
||||
* @param group
|
||||
* @throws NacosException
|
||||
*/
|
||||
@RequestMapping("listener")
|
||||
public String listenerConfig(@RequestParam("dataId") String dataId,
|
||||
@RequestParam(value = "group", required = false) String group) throws NacosException {
|
||||
if (StringUtils.isEmpty(group)) {
|
||||
group = DEFAULT_GROUP;
|
||||
}
|
||||
ConfigService configService = nacosConfigManager.getConfigService();
|
||||
configService.addListener(dataId, group, new Listener() {
|
||||
@Override
|
||||
public Executor getExecutor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
System.out.println("[Listen for configuration changes]:" + configInfo);
|
||||
}
|
||||
});
|
||||
return "Add Lister successfully!";
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2013-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.examples.example;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Use the @Value annotation to get configuration example
|
||||
*
|
||||
* @author lixiaoshuang
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/nacos/annotation")
|
||||
@RefreshScope
|
||||
public class ValueAnnotationExample {
|
||||
|
||||
@Value("${spring.cloud.nacos.config.serveraddr:}")
|
||||
private String serveraddr;
|
||||
|
||||
@Value("${spring.cloud.nacos.config.prefix:}")
|
||||
private String prefix;
|
||||
|
||||
@Value("${spring.cloud.nacos.config.group:}")
|
||||
private String group;
|
||||
|
||||
@Value("${spring.cloud.nacos.config.namespace:}")
|
||||
private String namespace;
|
||||
|
||||
@GetMapping
|
||||
public Map<String, String> getConfigInfo() {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("serveraddr", serveraddr);
|
||||
result.put("prefix", prefix);
|
||||
result.put("group", group);
|
||||
result.put("namespace", namespace);
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.examples.model;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
|
||||
@ConfigurationProperties(prefix = "spring.cloud.nacos.config")
|
||||
@RefreshScope
|
||||
public class NacosConfigInfo {
|
||||
|
||||
/**
|
||||
* Nacos server address
|
||||
*/
|
||||
private String serveraddr;
|
||||
|
||||
/**
|
||||
* Data Id prefix
|
||||
*/
|
||||
private String prefix;
|
||||
|
||||
/**
|
||||
* Nacos group
|
||||
*/
|
||||
private String group;
|
||||
|
||||
/**
|
||||
* Nacos namespace
|
||||
*/
|
||||
private String namespace;
|
||||
|
||||
public String getServeraddr() {
|
||||
return serveraddr;
|
||||
}
|
||||
|
||||
public void setServeraddr(String serveraddr) {
|
||||
this.serveraddr = serveraddr;
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public void setPrefix(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public void setGroup(String group) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue