Quick Start for Nacos 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
- Add the Nacos Spring Cloud 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.
- Configure the Nacos Server address and Specify the application name in
bootstrap.properties
:
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:
- The value of
prefix
is the value ofspring.application.name
by default. You can also configure this value inspring.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 ofspring.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 inspring.cloud.nacos.config.file-extension
. Currently only theproperties
andyaml
type is supported.
- Add the native
@RefreshScope
annotation of Spring Cloud to enable autorefresh of configuration updates:
- Call Nacos Open API to publish a configuration to the Nacos server. Assume the dataId is
example.properties
,and the content isuseLocalCache=true
.
-
Run
NacosConfigApplication
and callcurl http://localhost:8080/config/get
,You will get a returned value oftrue
. -
Call Nacos Open API again to publish an updated configuration to the Nacos server. Assume the dataId is
example.properties
,and the content isuseLocalCache=false
.
- Access
http://localhost:8080/config/get
again and the returned value becamefalse
,indicating that the value ofuseLocalCache
in 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
- Add the Nacos Spring Cloud 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.
- Configure the service provider, so that it can register its services to the Nacos server.
i. Add the Nacos server address in application.properties
:
ii. Enable service discovery by adding the Spring Cloud native annotation of @EnableDiscoveryClient
:
- 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
:
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:
- Start
ProviderApplication
andConsumerApplication
, and callhttp://localhost:8080/echo/2018
. You will get a returned message ofHello Nacos Discovery 2018
.