Quick Start for Nacos Spring Projects

    The quick start includes two samples:

    • How to enable Nacos server and Nacos Spring configuration modules to implement dynamic configuration management;
    • How to enable Nacos server and Nacos Spring service discovery modules to implement service registration and discovery.

    Follow instructions in Nacos Quick Start to download Nacos and start the Nacos server.

    Once you start the Nacos server, you can follow the steps below to enable the Nacos configuration management service for your Spring project.

    • Add the Nacos Spring dependency.

    The the latest version can be available in maven repositories such as "".

    • Add the annotation to enable the configuration service. In the code below, @NacosPropertySource is used to load the configuration source whose dataId is example , and autorefresh is also enabled:
    1. @Configuration
    2. @EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
    3. @NacosPropertySource(dataId = "example", autoRefreshed = true)
    4. public class NacosConfiguration {
    5. }
    • Specify the property value for the @NacosValue annotation of Nacos.
    1. @Controller
    2. @RequestMapping("config")
    3. public class ConfigController {
    4. @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
    5. private boolean useLocalCache;
    6. @RequestMapping(value = "/get", method = GET)
    7. @ResponseBody
    8. return useLocalCache;
    9. }
    • Start Tomcat and call curl http://localhost:8080/config/get to get configuration information. Because no configuration has been published, a falsemessage is returned.

    • Now you can call to publish a configruation to the Nacos server. Assume the dataId is example, and content is useLocalCache=true.

    • Access http://localhost:8080/config/getagain, and you get a return message of true, indicating that the value of useLocalCachein your application has been updated.

    Sampe project:

    • Add the Nacos Spring dependency.
    1. <dependency>
    2. <groupId>com.alibaba.nacos</groupId>
    3. <artifactId>nacos-spring-context</artifactId>
    4. <version>${latest.version}</version>
    5. </dependency>

    The the latest version can be available in maven repositories such as "mvnrepository.com".

    • Add the @EnableNacosDiscovery annotation to enable the service discovery function of Nacos:
    1. @Configuration
    2. @EnableNacosDiscovery(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
    3. public class NacosConfiguration {
    4. }
    • Use @NacosInjected to inject a Nacos NamingService instance:
    • Start Tomcat and call curl , and the return value is an empty JSON array [].

    1. curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=example&ip=127.0.0.1&port=8080'
    1. [
    2. "instanceId": "127.0.0.1#8080#DEFAULT#example",
    3. "ip": "127.0.0.1",
    4. "port": 8080,
    5. "weight": 1.0,
    6. "healthy": true,
    7. "cluster": {
    8. "serviceName": null,
    9. "name": "",
    10. "healthChecker": {
    11. "type": "TCP"
    12. },
    13. "defaultPort": 80,
    14. "defaultCheckPort": 80,
    15. "useIPPort4Check": true,
    16. "metadata": {}
    17. },
    18. "service": null,
    19. "metadata": {}
    20. ]