Skip to content

Nacos with Spring Cloud Projects

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

For more details about Nacos Spring Cloud: Nacos Config and Nacos Discovery.

The quick start includes two samples:

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

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 Cloud project.

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

  1. Add the Nacos Spring Cloud dependency.
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${latest.version}</version>
</dependency>

Note: Version 2.1.x.RELEASE is compatible with the Spring Boot 2.1.x line. Version 2.0.x.RELEASE is compatible with the Spring Boot 2.0.x line. Version 1.5.x.RELEASE is compatible with the Spring Boot 1.5.x line.

  1. Configure the Nacos Server address and Specify the application name in bootstrap.properties :
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.application.name=example

Note: The value of spring.application.name will be used to construct part of the dataId in Nacos configuration management.

In Nacos Spring Cloud, the format of dataId is as follows:

${prefix}-${spring.profiles.active}.${file-extension}
  • The value of prefix is the value of spring.application.name by default. You can also configure this value in spring.cloud.nacos.config.prefix.
  • spring.profiles.active is the profile of the current environment. For more details, refer to Spring Boot Document. Note: When the value of spring.profiles.active is empty, the corresponding hyphen - will be deleted, and the format of dataId becomes: ${prefix}.${file-extension}
  • file-exetension is the data format of the configuration content, and can be configured in spring.cloud.nacos.config.file-extension . Currently only the properties and yaml type is supported.
  1. Add the native @RefreshScope annotation of Spring Cloud to enable autorefresh of configuration updates:
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${useLocalCache:false}")
private boolean useLocalCache;
@RequestMapping("/get")
public boolean get() {
return useLocalCache;
}
}
  1. Call Nacos Open API to publish a configuration to the Nacos server. Assume the dataId is example.properties,and the content is useLocalCache=true.
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=true"
  1. Run NacosConfigApplicationand call curl http://localhost:8080/config/get,You will get a returned value of true.

  2. Call Nacos Open API again to publish an updated configuration to the Nacos server. Assume the dataId isexample.properties,and the content is useLocalCache=false.

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=false"
  1. Access http://localhost:8080/config/getagain and the returned value became false,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 Cloud project.

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

  1. Add the Nacos Spring Cloud dependency.
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${latest.version}</version>
</dependency>

Note: Version 2.1.x.RELEASE is compatible with the Spring Boot 2.1.x line. Version 2.0.x.RELEASE is compatible with the Spring Boot 2.0.x line. Version 1.5.x.RELEASE is compatible with the Spring Boot 1.5.x line.

  1. Configure the service provider, so that it can register its services to the Nacos server.

i. Add the Nacos server address in application.properties :

server.port=8070
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

ii. Enable service discovery by adding the Spring Cloud native annotation of @EnableDiscoveryClient:

@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
@RestController
class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
}
}
  1. Configure the service consumer so that it can discover the services that it would like to call on the Nacos server.

i. Configure the Nacos server address in application.properties :

server.port=8080
spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

ii. Add the Spring Cloud native annotation of @EnableDiscoveryClient to enable service discovery. Add the @LoadBalanced annotation for the RestTemplate instance,  and enable the integration of @LoadBalanced and Ribbon:

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
@RestController
public class TestController {
private final RestTemplate restTemplate;
@Autowired
public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
public String echo(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
}
}
}
  1. Start ProviderApplication and ConsumerApplication , and call http://localhost:8080/echo/2018. You will get a returned message of Hello Nacos Discovery 2018.