diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md index 850e0fa3e..5a23b6592 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md @@ -26,17 +26,11 @@ spring.application.name=nacos-config-example spring.cloud.nacos.config.server-addr=127.0.0.1:8848 -3. 完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。这里我们使用 @Value 注解来将对应的配置注入到 SampleController 的 userName 和 age 字段,并添加 @RefreshScope 打开动态刷新功能 - - @RefreshScope - class SampleController { - - @Value("${user.nickname:zz}") - String userName; - - @Value("${user.age:18}") - int age; - } +3. 完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。假设我们通过 Nacos 配置中心保存 Nacos 的部分配置,有以下四种例子: +- BeanAutoRefreshConfigExample: 通过将配置信息配置为bean,支持配置变自动刷新的例子 +- ConfigListenerExample: 监听配置信息的例子 +- DockingInterfaceExample: 对接 nacos 接口,通过接口完成对配置信息增删改查的例子 +- ValueAnnotationExample: 通过 @Value 注解进行配置信息获取的例子 ### 启动 Nacos Server 并添加配置 @@ -52,7 +46,7 @@ 3. 在命令行执行如下命令,向 Nacos Server 中添加一条配置。 - curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.nickname=james%0Auser.age=17" + curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=spring.cloud.nacos.config.serveraddr=127.0.0.1:8848%0Aspring.cloud.nacos.config.prefix=PREFIX%0Aspring.cloud.nacos.config.group=GROUP%0Aspring.cloud.nacos.config.namespace=NAMESPACE" **注:你也可以使用其他方式添加,遵循 HTTP API 规范即可,若您使用的 Nacos 版本自带控制台,建议直接使用控制台进行配置** @@ -61,11 +55,12 @@ dataId 为 nacos-config-example.properties group 为 DEFAULT_GROUP - 内容如下 + 内容如下: - user.id=1 - user.nickname=james - user.age=17 + spring.cloud.nacos.config.serveraddr=127.0.0.1:8848 + spring.cloud.nacos.config.prefix=PREFIX + spring.cloud.nacos.config.group=GROUP + spring.cloud.nacos.config.namespace=NAMESPACE ### 应用启动 @@ -83,18 +78,18 @@ ### 验证 #### 验证自动注入 -在浏览器地址栏输入 `http://127.0.0.1:18084/user`,并点击调转,可以看到成功从 Nacos Config Server 中获取了数据。 +在浏览器地址栏输入 `http://127.0.0.1:18084/nacos/bean`,并点击调转,可以看到成功从 Nacos Config Server 中获取了数据。 -![get](https://cdn.nlark.com/lark/0/2018/png/54319/1536986328663-5e3503c2-7e14-4c56-b5f9-72fecc6898d2.png) +![](https://tva1.sinaimg.cn/large/e6c9d24ely1h29htdsg18j20jc0aigma.jpg) #### 验证动态刷新 1. 执行如下命令,修改 Nacos Server 端的配置数据 - curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.nickname=james%0Auser.age=18" + curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=spring.cloud.nacos.config.serveraddr=127.0.0.1:8848%0Aspring.cloud.nacos.config.prefix=PREFIX%0Aspring.cloud.nacos.config.group=DEFAULT_GROUP%0Aspring.cloud.nacos.config.namespace=NAMESPACE" -2. 在浏览器地址栏输入 `http://127.0.0.1:18084/user`,并点击调转,可以看到应用从 Nacos Server 中获取了最新的数据,age 变成了 18。 +2. 在浏览器地址栏输入 `http://127.0.0.1:18084/nacos/bean`,并点击调转,可以看到应用从 Nacos Server 中获取了最新的数据,group 变成了 DEFAULT_GROUP。 -![refresh](https://cdn.nlark.com/lark/0/2018/png/54319/1536986336535-c0efdf6d-a5d3-4f33-8d26-fe3a36cdacf6.png) +![](https://tva1.sinaimg.cn/large/e6c9d24ely1h29hzf0ic4j20ks0b4mxy.jpg) ## 原理 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md index ea5a982f0..90e58fcc5 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md @@ -26,18 +26,12 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl spring.application.name=nacos-config-example spring.cloud.nacos.config.server-addr=127.0.0.1:8848 - -3. After completing the above two steps, the application will get the externalized configuration from Nacos Server and put it in the Spring Environment's PropertySources.We use the @Value annotation to inject the corresponding configuration into the userName and age fields of the SampleController, and add @RefreshScope to turn on dynamic refresh . - @RefreshScope - class SampleController { - - @Value("${user.nickname:zz}") - String userName; - - @Value("${user.age:18}") - int age; - } +3. After completing the above two steps, the application will obtain the corresponding configuration from Nacos Config and add it to the PropertySources of Spring Environment. Suppose we save part of the configuration of Nacos through the Nacos configuration center, there are the following four examples: +- BeanAutoRefreshConfigExample: An example that supports automatic refresh of configuration changes by configuring configuration information as beans +- ConfigListenerExample: Example of listening configuration information +- DockingInterfaceExample: An example of docking the nacos interface and completing the addition, deletion, modification and checking of configuration information through the interface +- ValueAnnotationExample: An example of obtaining configuration information through @Value annotation ### Start Nacos Server @@ -52,8 +46,8 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl 2. Windows , execute `cmd startup.cmd -m standalone` 3. Execute the following command to add a configuration to Nacos Server. - - curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.nickname=james%0Auser.age=17" + + curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=spring.cloud.nacos.config.serveraddr=127.0.0.1:8848%0Aspring.cloud.nacos.config.prefix=PREFIX%0Aspring.cloud.nacos.config.group=GROUP%0Aspring.cloud.nacos.config.namespace=NAMESPACE" **Note: You can also add it in other ways. If you are using the Nacos version with its own console, it is recommended to configure it directly using the console.** @@ -63,11 +57,12 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl dataId is nacos-config-example.properties group is DEFAULT_GROUP - content is + content is: - user.id=1 - user.nickname=james - user.age=17 + spring.cloud.nacos.config.serveraddr=127.0.0.1:8848 + spring.cloud.nacos.config.prefix=PREFIX + spring.cloud.nacos.config.group=GROUP + spring.cloud.nacos.config.namespace=NAMESPACE ### Start Application @@ -85,19 +80,18 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl ### Verification #### Automatic Injection -Enter `http://127.0.0.1:18084/user` in the browser address bar and click Go to, we can see the data successfully obtained from Nacos Config Server. +Enter `http://127.0.0.1:18084/nacos/bean` in the browser address bar and click Go to, we can see the data successfully obtained from Nacos Config Server. -![get](https://cdn.nlark.com/lark/0/2018/png/54319/1536986328663-5e3503c2-7e14-4c56-b5f9-72fecc6898d2.png) +![](https://tva1.sinaimg.cn/large/e6c9d24ely1h29htdsg18j20jc0aigma.jpg) #### Dynamic Refresh 1. Run the following command to modify the configuration data on the Nacos Server side. - curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.nickname=james%0Auser.age=18" + curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=spring.cloud.nacos.config.serveraddr=127.0.0.1:8848%0Aspring.cloud.nacos.config.prefix=PREFIX%0Aspring.cloud.nacos.config.group=DEFAULT_GROUP%0Aspring.cloud.nacos.config.namespace=NAMESPACE" -2. Enter `http://127.0.0.1:18084/user` in the browser address bar and click Go to, -We can see that the app got the latest data from Nacos Server and the age becomes 18. +2. Enter `http://127.0.0.1:18084/nacos/bean` in the address bar of the browser, and click Flip, you can see that the application has obtained the latest data from Nacos Server, and the group has become DEFAULT_GROUP. -![refresh](https://cdn.nlark.com/lark/0/2018/png/54319/1536986336535-c0efdf6d-a5d3-4f33-8d26-fe3a36cdacf6.png) +![](https://tva1.sinaimg.cn/large/e6c9d24ely1h29hzf0ic4j20ks0b4mxy.jpg) ## Principle diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/Application.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/Application.java index a03ff2eb8..814528bf3 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/Application.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/Application.java @@ -16,218 +16,21 @@ package com.alibaba.cloud.examples; -import java.io.IOException; -import java.io.StringReader; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.Executor; +import com.alibaba.cloud.examples.model.NacosConfigInfo; -import com.alibaba.cloud.nacos.NacosConfigManager; -import com.alibaba.nacos.api.config.listener.Listener; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.cloud.context.config.annotation.RefreshScope; -import org.springframework.context.annotation.Bean; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.boot.context.properties.EnableConfigurationProperties; /** * @author xiaojing, Jianwei Mao */ @SpringBootApplication +@EnableConfigurationProperties(NacosConfigInfo.class) public class Application { - - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } - - @Bean - public UserConfig userConfig() { - return new UserConfig(); - } - -} - -@ConfigurationProperties(prefix = "user") -class UserConfig { - - private int age; - - private String nickname; - - private String hr; - - private Map map; - - private List users; - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public String getNickname() { - return nickname; - } - - public void setNickname(String nickname) { - this.nickname = nickname; - } - - public Map getMap() { - return map; - } - - public void setMap(Map map) { - this.map = map; - } - - public List getUsers() { - return users; - } - - public void setUsers(List users) { - this.users = users; - } - - public String getHr() { - return hr; - } - - public void setHr(String hr) { - this.hr = hr; - } - - @Override - public String toString() { - return "UserConfig{" + "age=" + age + ", nickname='" + nickname + '\'' + ", map=" + map - + ", hr='" + hr + '\'' + ", users=" + users + '}'; - } - - public static class User { - - private String name; - - private String hr; - - private String avg; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getHr() { - return hr; - } - - public void setHr(String hr) { - this.hr = hr; - } - - public String getAvg() { - return avg; - } - - public void setAvg(String avg) { - this.avg = avg; - } - - @Override - public String toString() { - return "User{" + "name='" + name + '\'' + ", hr=" + hr + ", avg=" + avg + '}'; - } - - } - -} - -@Component -class SampleRunner implements ApplicationRunner { - - @Autowired - private NacosConfigManager nacosConfigManager; - - @Override - public void run(ApplicationArguments args) throws Exception { - nacosConfigManager.getConfigService().addListener( - "nacos-config-custom.properties", "DEFAULT_GROUP", new Listener() { - - /** - * Callback with latest config data. - * @param configInfo latest config data for specific dataId in Nacos - * server - */ - @Override - public void receiveConfigInfo(String configInfo) { - Properties properties = new Properties(); - try { - properties.load(new StringReader(configInfo)); - } - catch (IOException e) { - e.printStackTrace(); - } - System.out.println("config changed: " + properties); - } - - @Override - public Executor getExecutor() { - return null; - } - }); - } - -} - -@RestController -@RefreshScope -class SampleController { - - @Autowired - UserConfig userConfig; - - @Autowired - private NacosConfigManager nacosConfigManager; - - @Autowired - private Environment environment; - - @Value("${user.nickname:zz}") - String nickname; - - @Value("${user.age:25}") - Integer age; - - @RequestMapping("/user") - public String simple() { - return "Hello Nacos Config!" + "Hello " + nickname + " " + age + " [UserConfig]: " - + userConfig + "!" + nacosConfigManager.getConfigService(); - } - - @RequestMapping("/get/{nickname}") - public String getValue(@PathVariable String nickname) { - return String.valueOf(environment.getProperty(nickname)); - } - - @RequestMapping("/bool") - public boolean bool() { - return (Boolean) (userConfig.getMap().get("2")); - } - + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + } diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/configuration/BeanConfiguration.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/configuration/BeanConfiguration.java new file mode 100644 index 000000000..538475316 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/configuration/BeanConfiguration.java @@ -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(); + } +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/BeanAutoRefreshConfigExample.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/BeanAutoRefreshConfigExample.java new file mode 100644 index 000000000..eed64fe42 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/BeanAutoRefreshConfigExample.java @@ -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 getConfigInfo() { + Map 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; + } +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/ConfigListenerExample.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/ConfigListenerExample.java new file mode 100644 index 000000000..7b3c1737d --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/ConfigListenerExample.java @@ -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); + } + }); + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/DockingInterfaceExample.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/DockingInterfaceExample.java new file mode 100644 index 000000000..5428c0c4f --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/DockingInterfaceExample.java @@ -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!"; + } +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/ValueAnnotationExample.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/ValueAnnotationExample.java new file mode 100644 index 000000000..821aff30f --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/example/ValueAnnotationExample.java @@ -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 getConfigInfo() { + Map result = new HashMap<>(); + result.put("serveraddr", serveraddr); + result.put("prefix", prefix); + result.put("group", group); + result.put("namespace", namespace); + return result; + } +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/model/NacosConfigInfo.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/model/NacosConfigInfo.java new file mode 100644 index 000000000..cacfd0307 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/model/NacosConfigInfo.java @@ -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; + } +} +