The last article covered Spring Cloud Alibaba-NacOS – Service Registration and Discovery. This article will continue to cover Spring Cloud Alibaba’s Nacos- service provider.

An overview,

A simple example to get a feel for how to register a service with Nacos is not that different from Eureka.

Second, the POM

Create a service provider project named hello-spring-cloud-alibaba-nacos-provider and configure POM.xml as follows:

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > com. Funtl < / groupId > < artifactId > hello - spring - the cloud - alibaba - dependencies < / artifactId > < version > 1.0.0 - the SNAPSHOT < / version > < relativePath >). /hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath> </parent> <artifactId>hello-spring-cloud-alibaba-nacos-provider</artifactId> <packaging>jar</packaging> <name>hello-spring-cloud-alibaba-nacos-provider</name> <url>http://www.funtl.com</url> <inceptionYear>2018-Now</inceptionYear> <dependencies> <! -- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! -- Spring Boot End --> <! -- Spring Cloud Begin --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <! -- Spring Cloud End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.funtl.hello.spring.cloud.alibaba.nacos.provider.NacosProviderApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>Copy the code

Third, the Application

A Nacos client is indicated by the @enableDiscoveryClient annotation, which is a native annotation provided by Spring Cloud.

package com.funtl.hello.spring.cloud.alibaba.nacos.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableDiscoveryClient public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); } @RestController public class EchoController { @GetMapping(value = "/echo/{message}") public String echo(@PathVariable String message) { return "Hello Nacos Discovery " + message; }}}Copy the code

application.yml

Spring: Application: name: nacos-provider cloud: nacos: Discovery: server-addr: 127.0.0.1:8848 Server: port: 8081 management: endpoints: web: exposure: include: "*"Copy the code

Four, start the project

Through the browser to http://localhost:8848/nacos, namely Nacos Server url

[img-lclsnzLR-1599546025856] (/assets/ lusifer_20190105063653.png)

You will find that a service has been registered with a service called nacos-Provider

Then open the http://localhost:8081/echo/hi, you can see on the browser:

Hello Nacos Discovery hi
Copy the code

5. Endpoint checking of services

Spring-cloud-starter-alibaba-nacos-discovery provides an EndPoint when implemented, The EndPoint access address to http://ip:port/actuator/nacos-discovery. There are two main types of EndPoint information:

1, the subscribe: shows the current 2, what are the service subscriber NacosDiscoveryProperties: shows the current service instance based on Nacos configurationCopy the code

Through the browser to http://localhost:8081/actuator/nacos-discovery you will see: (slightly) on the browser

For more details or to get the full Java microservices architecture, spring Family Bucket video course, please click here.

Additional information about Nacos Starter configuration items