創(chuàng)建一個工程名為 hello-spring-cloud-alibaba-nacos-provider 的服務提供者項目畔塔,pom.xml 配置如下:
POM
<?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.wj</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>
<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>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.wj.hello.spring.cloud.alibaba.nacos.provider.NacosProviderApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Application
通過 @EnableDiscoveryClient
注解表明是一個 Nacos 客戶端怜瞒,該注解是 Spring Cloud 提供的原生注解
package com.wj.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 網(wǎng)址
你會發(fā)現(xiàn)一個服務已經(jīng)注冊在服務中了窃判,服務名為 nacos-provider
這時打開 http://localhost:8081/echo/hi ,你會在瀏覽器上看到:Hello Nacos Discovery hi
服務的端點檢查
spring-cloud-starter-alibaba-nacos-discovery 在實現(xiàn)的時候提供了一個 EndPoint, EndPoint 的訪問地址為 http://ip:port/actuator/nacos-discovery
喇闸。 EndPoint 的信息主要提供了兩類:
1袄琳、subscribe: 顯示了當前有哪些服務訂閱者
2、NacosDiscoveryProperties: 顯示了當前服務實例關(guān)于 Nacos 的基礎(chǔ)配置
通過瀏覽器訪問 http://localhost:8081/actuator/nacos-discovery 你會在瀏覽器上看到:
附:Nacos Starter 更多配置項信息
配置項 | Key | 默認值 | 說明 |
---|---|---|---|
服務端地址 | spring.cloud.nacos.discovery.server-addr | 無 | Nacos Server 啟動監(jiān)聽的ip地址和端口 |
服務名 | spring.cloud.nacos.discovery.service | ${spring.application.name} | 給當前的服務命名 |
權(quán)重 | spring.cloud.nacos.discovery.weight | 1 | 取值范圍 1 到 100燃乍,數(shù)值越大唆樊,權(quán)重越大 |
網(wǎng)卡名 | spring.cloud.nacos.discovery.network-interface | 無 | 當IP未配置時,注冊的IP為此網(wǎng)卡所對應的IP地址刻蟹,如果此項也未配置逗旁,則默認取第一塊網(wǎng)卡的地址 |
注冊的IP地址 | spring.cloud.nacos.discovery.ip | 無 | 優(yōu)先級最高 |
注冊的端口 | spring.cloud.nacos.discovery.port | -1 | 默認情況下不用配置,會自動探測 |
命名空間 | spring.cloud.nacos.discovery.namespace | 無 | 常用場景之一是不同環(huán)境的注冊的區(qū)分隔離舆瘪,例如開發(fā)測試環(huán)境和生產(chǎn)環(huán)境的資源(如配置片效、服務)隔離等。 |
AccessKey | spring.cloud.nacos.discovery.access-key | 無 | 當要上阿里云時英古,阿里云上面的一個云賬號名 |
SecretKey | spring.cloud.nacos.discovery.secret-key | 無 | 當要上阿里云時淀衣,阿里云上面的一個云賬號密碼 |
Metadata | spring.cloud.nacos.discovery.metadata | 無 | 使用 Map 格式配置,用戶可以根據(jù)自己的需要自定義一些和服務相關(guān)的元數(shù)據(jù)信息 |
日志文件名 | spring.cloud.nacos.discovery.log-name | 無 | |
接入點 | spring.cloud.nacos.discovery.enpoint | UTF-8 | 地域的某個服務的入口域名召调,通過此域名可以動態(tài)地拿到服務端地址 |
是否集成 Ribbon | ribbon.nacos.enabled | true | 一般都設(shè)置成 true 即可 |