![v2-741f6a4b3880ba503210d6a2131d67aa_1440w.jpg?source=172ae18b](http://img-02.proxy.5ce.com/view/image?&type=2&guid=1d7103bf-392f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-741f6a4b3880ba503210d6a2131d67aa_1440w.jpg?source=172ae18b)
一、概述
通过一个简单的示例来感受一下如何将服务注册到 Nacos,其实和 Eureka 没有太大差别。
二、POM
创建一个工程名为 hello-spring-cloud-alibaba-nacos-provider
的服务提供者项目,pom.xml
配置如下:
<?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>
<parent>
<groupId>com.funtl</groupId>
<artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
<version>1.0.0-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>
三、Application
通过 @EnableDiscoveryClient
注解表明是一个 Nacos 客户端,该注解是 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;
}
}
}
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: "*"
四、启动工程
通过浏览器访问 http://localhost:8848/nacos
,即 Nacos Server 网址
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LClSnzLr-1599546025856)(/assets/Lusifer_20190105063653.png)]
你会发现一个服务已经注册在服务中了,服务名为 nacos-provider
这时打开 http://localhost:8081/echo/hi
,你会在浏览器上看到:
Hello Nacos Discovery hi
五、服务的端点检查
spring-cloud-starter-alibaba-nacos-discovery 在实现的时候提供了一个 EndPoint, EndPoint 的访问地址为 http://ip:port/actuator/nacos-discovery
。 EndPoint 的信息主要提供了两类:
1、subscribe: 显示了当前有哪些服务订阅者
2、NacosDiscoveryProperties: 显示了当前服务实例关于 Nacos 的基础配置
通过浏览器访问 http://localhost:8081/actuator/nacos-discovery
你会在浏览器上看到:(略)
查看详情或获取完整的Java微服务架构,spring全家桶视频课程,请点此学习。
附:Nacos Starter 更多配置项信息
![v2-08faa88611e167514d1c7cd710e972e4_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=1d7103bf-392f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-08faa88611e167514d1c7cd710e972e4_b.jpg)