Nacos is a microservice developed by Alibaba for discovery, configuration and management. Perfect combination with Spring Cloud Alibaba. Nacos provides an easy-to-use feature set that helps you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management. Nacos is domestic, so the Chinese documentation is also rich.

Enable configuration management

Introduction of depend on

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> < version > 2.2.5. RELEASE < / version > < / dependency > < / dependencies > <! -- SpringCloud dependencies, > <dependencyManagement> < Dependencies > < Dependency > < grouppid >com.alibaba.cloud</ grouppid > <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${com-alibaba-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

configuration

In the application/SRC/main/resources/bootstrap the properties in the configuration file configuration Nacos Config metadata

Spring. The application. The name = spring - the cloud - alibaba - nacos - config - example spring. Cloud. Nacos. Config. The server - addr = 127.0.0.1:8848

You need to start Nacos first, otherwise you will get some errors

After completing the above two steps, the application will obtain the corresponding configuration from the Nacos Config and add it to the PropertySources of the Spring Environment. Here we use the @Value annotation to inject the corresponding configuration into the Username and Age fields of SampleController, and add @RefreshScope to enable dynamic refresh

@RefreshScope
 class SampleController {

     @Value("${user.name}")
     String userName;

     @Value("${user.age}")
     int age;
 }

Start Nacos Server and add the configuration

Download Nacos

Making address: https://github.com/alibaba/na…

Start the Server

Go into its relative folder nacos/bin and execute the following command against the actual operating system. Refer to official documentation for details.

  • Linux/ UNIX /Mac operating system, execute commandssh startup.sh -m standalone
  • Windows operating system, execute the commandcmd startup.cmd

Execute the following command from the command line to add a configuration to Nacos Server.

POST "http://127.0.0.1:8848/nacos/v1/cs/configs? dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17"

Start service discovery

Introduction of depend on

The parent

< properties > < Java version > 1.8 < / Java version > < com - alibaba - cloud. Version > 2.2.5. RELEASE < / com - alibaba - cloud. Version > </properties> <modules> <module>nacos-discovery-consumer</module> <module>nacos-discovery-provider</module> </modules> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <! -- SpringCloud dependencies, > <dependencyManagement> < Dependencies > < Dependency > < grouppid >com.alibaba.cloud</ grouppid > <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${com-alibaba-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

consumer

< the properties > < spring - cloud - netflix. Version > 2.2.5. RELEASE < / spring - cloud - netflix. Version > < / properties > < dependencies > <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> <version>${spring-cloud-netflix.version}</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <exclusions> <exclusion> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </dependency> </dependencies>

provider

<dependencies>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
    </dependency>
</dependencies>

Add annotations

Enable service registration discovery using the Spring Cloud native annotation @EnableDiscoveryClient

/ / unlock the function of service registry found @ SpringBootApplication @ EnableDiscoveryClient public class NacosDiscoveryProviderApplication {public static void main(String[] args) { SpringApplication.run(NacosDiscoveryProviderApplication.class, args); }}

configuration

Configure the address of the Nacos Server in Application.properties

consumer

Server port = 18083 spring. Application. Name = service - consumer spring. Cloud. Nacos. Discovery. The server - addr = 127.0.0.1:8848 # Nacos authentication information spring. Cloud. Nacos. Username = nacos spring. Cloud. Nacos. Password = nacos

provier

Server port = 18082 spring. Application. Name = service provider - spring. Cloud. Nacos. Discovery. The server - addr = 127.0.0.1:8848 # Nacos authentication information spring. Cloud. Nacos. Username = nacos spring. Cloud. Nacos. Password = nacos

consumer

  1. config

Enable service registration discovery using the Spring Cloud native annotation @EnableDiscoveryClient. Add the @LoadBalanced annotation to the RestTemplate instance to enable @LoadBalanced integration with the Ribbon

@Configuration public class NacosConfig { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); }}
  1. controller
@RestController public class TestController { @Autowired private 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); }}

provier

  1. controller
@RestController public class TestController { @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) public String echo(@PathVariable String string) { return "Hello Nacos Discovery " + string; }}

Finally start ProviderApplication and ConsumerApplication, called http://localhost:8080/echo/2021, return to the content of 2021 ` Hello Nacos Discovery.

References

[1] Nacos Config Example: https://github.com/alibaba/sp…

[2] Nacos Spring Cloud: https://nacos.io/zh-cn/docs/q…

[3] Nacos: https://nacos.io/zh-cn/docs/q…