Quick Start for Nacos Spring Boot Projects

This quick start introduces how to enable Nacos configuration management and service discovery features for your Spring Boot project.

For more details about Nacos Spring Boot: nacos-spring-boot-project.

The quick start includes two samples:

  • How to enable dynamic configuration updates with Nacos server and nacos-config-spring-boot-starter;
  • How to enable service registration and discovery with Nacos Server and nacos-discovery-spring-boot-starter.

Prerequisite

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

Enable Configuration Service

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

Sample project: nacos-spring-boot-config-example

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

Note: Version 0.2.x.RELEASE is compatible with the Spring Boot 2.0.x line. Version 0.1.x.RELEASE is compatible with the Spring Boot 1.x line.

  • Configure the Nacos server address in application.properties :
  1. nacos.config.server-addr=127.0.0.1:8848
  • Use @NacosPropertySource to load the configuration source whose dataId is example , and enable autorefresh of configuration updates:
  1. @SpringBootApplication
  2. @NacosPropertySource(dataId = "example", autoRefreshed = true)
  3. public class NacosConfigApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(NacosConfigApplication.class, args);
  6. }
  7. }
  • Specify the property value of 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. public boolean get() {
  9. return useLocalCache;
  10. }
  11. }
  • Start NacosConfigApplicationand call curl http://localhost:8080/config/get. You will get a return message of false, as no configuration has been published so far.

  • Call Nacos Open API to publish a configuration to the Nacos server. Assume the dataId is example, and the content is useLocalCache=true.

  1. curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example&group=DEFAULT_GROUP&content=useLocalCache=true"
  • Access http://localhost:8080/config/getagain, and the returned value will betrue,indicating that the value of useLocalCachein your application has been updated.

Enable Service Discovery

Now you would also like to enable the service discovery feature of Nacos in your Spring Boot project.

Sample project: nacos-spring-boot-discovery-example

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

Note: Version 0.2.x.RELEASE is compatible with the Spring Boot 2.0.x line. Version 0.1.x.RELEASE is compatible with the Spring Boot 1.x line.

  • Configure the Nacos server address in application.properties :
  1. nacos.discovery.server-addr=127.0.0.1:8848
  • Use @NacosInjected to inject a Nacos NamingService instance:
  1. @Controller
  2. @RequestMapping("discovery")
  3. public class DiscoveryController {
  4. @NacosInjected
  5. private NamingService namingService;
  6. @RequestMapping(value = "/get", method = GET)
  7. @ResponseBody
  8. public List<Instance> get(@RequestParam String serviceName) throws NacosException {
  9. return namingService.getAllInstances(serviceName);
  10. }
  11. }
  12. @SpringBootApplication
  13. public class NacosDiscoveryApplication {
  14. public static void main(String[] args) {
  15. SpringApplication.run(NacosDiscoveryApplication.class, args);
  16. }
  17. }
  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. {
  3. "instanceId": "127.0.0.1-8080-DEFAULT-example",
  4. "ip": "127.0.0.1",
  5. "port": 8080,
  6. "weight": 1.0,
  7. "healthy": true,
  8. "cluster": {
  9. "serviceName": null,
  10. "name": "",
  11. "healthChecker": {
  12. "type": "TCP"
  13. },
  14. "defaultPort": 80,
  15. "defaultCheckPort": 80,
  16. "useIPPort4Check": true,
  17. "metadata": {}
  18. },
  19. "service": null,
  20. "metadata": {}
  21. }
  22. ]