Nacos was developed by Alibaba to discover, configure and manage microservices. 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 abundant.

Start 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> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${com-alibaba-cloud.version}</version> <type>pom</type> <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
Copy the code

configuration

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

spring.application.name=spring-cloud-alibaba-nacos-config-example
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
Copy the code

Nacos needs to be started first, otherwise some errors will be reported

After completing the above two steps, the application gets the configuration from Nacos Config and adds it to Spring Environment’s PropertySources. Here we use the @Value annotation to inject the corresponding configuration into the userName and age fields of SampleController, and add the @refreshScope to enable dynamic refresh

@RefreshScope
 class SampleController {

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

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

Start Nacos Server and add the configuration

Download Nacos

GitHub address: github.com/alibaba/nac…

Start the Server

Go to the corresponding folder nacos/bin and run the following command based on the actual operating system. Please refer to the official documentation for details.

  • Linux, Unix, or Mac, run the sh startup.sh -m standalone command

  • On Windows, run CMD startup. CMD

Run 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"Copy the code

Enabling 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> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${com-alibaba-cloud.version}</version> <type>pom</type> <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Copy the code

consumer

<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>
Copy the code

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>
Copy the code

Add annotations

Enable service registration discovery through the Spring Cloud native annotation @enableDiscoveryClient

// Enable the service registration discovery function
@SpringBootApplication
@EnableDiscoveryClient
public class NacosDiscoveryProviderApplication {
    public static void main(String[] args) { SpringApplication.run(NacosDiscoveryProviderApplication.class, args); }}Copy the code

configuration

Configure the address of the Nacos Server in application.properties

consumer

server.port=18083
spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# NACOS authentication information
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
Copy the code

provier

server.port=18082
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# NACOS authentication information
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
Copy the code

consumer

  1. config

Enable service registration discovery through 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(a) {
        return newRestTemplate(); }}Copy the code
  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); }}Copy the code

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; }}Copy the code

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: github.com/alibaba/spr…

[2] Nacos Spring Cloud: Nacos. IO /zh-cn/docs/…

[3] Nacos: Nacos. IO/useful – cn/docs /…