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 = "" XMLNS: xsi = "" Xsi: schemaLocation = "" > < 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></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></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></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; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import; 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) {, args); } @RestController public class EchoController { @GetMapping(value = "/echo/{message}") public String echo(@PathVariable String message) { return "Hello Nacos Discovery " + message; }}}Copy the code


Spring: Application: name: nacos-provider cloud: nacos: Discovery: server-addr: 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