SpringBoot使用Nacos服務發現
本文介紹SpringBoot應用使用Nacos服務發現。
上一篇文章介紹了SpringBoot使用Nacos做配置中心,本文介紹SpringBoot使用Nacos做服務發現。
1.Eureka閉源
相信到現在,Eureka 2.0 閉源已經不是什麼新鮮事了。在2017-2018年,幾乎在國內掀起了一陣SpringCloud的熱潮,幾乎很大一部分人群隨著對SpringBoot的關注,都開始關注起來了SpringCloud。而由於Eureka註冊中心的易整合等優點,更是大多數使用SpringCloud的首選註冊中心。但是隨著Eureka官網的宣告,如下。

大致意思就是開源工作已經停止之類的話,這裡就不做介紹了,感興趣可以上Eureka的Github地址上檢視 github.com/Netflix/eur… 。
Nacos也是一個優秀的註冊中心,並且由阿里巴巴開源,並且最近的熱度很高,已經更新到0.8.0版本了,基本上更新的很頻繁,也是一個Eureka閉源後的好的選擇。
2.SpringBoot使用Nacos服務發現
首先,需要啟動Nacos,這裡不做過多介紹。
建立專案,加入Nacos的服務發現的依賴nacos-discovery-spring-boot-starter,完整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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dalaoyang</groupId> <artifactId>springboot2_nacos_discovery</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot2_nacos_discovery</name> <description>springboot2_nacos_discovery</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>nacos-discovery-spring-boot-starter</artifactId> <version>0.2.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 複製程式碼
配置檔案配置Nacos服務的地址,如程式碼清單所示。
server.port=8080 spring.application.name=springboot2-nacos-discovery nacos.discovery.server-addr=127.0.0.1:8848 複製程式碼
SpringBoot使用Nacos服務發現需要想Nacos服務註冊,可以選擇使用Nacos Api來直接註冊,如程式碼清單所示。
//curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=springboot2-nacos-discovery&ip=127.0.0.1&port=8080' 複製程式碼
本文使用註解@PostConstruct,在服務啟動後向Nacos服務註冊,並且建立方法根據例項名稱獲取例項,完整啟動類如程式碼清單所示。
package com.dalaoyang; import com.alibaba.nacos.api.annotation.NacosInjected; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.annotation.PostConstruct; import java.net.InetAddress; import java.util.List; import static org.springframework.web.bind.annotation.RequestMethod.GET; //curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=springboot2-nacos-discovery&ip=127.0.0.1&port=8080' @SpringBootApplication @RestController public class Springboot2NacosDiscoveryApplication { @NacosInjected private NamingService namingService; @Value("${server.port}") private int serverPort; @Value("${spring.application.name}") private String applicationName; @PostConstruct public void registerInstance() throws NacosException{ namingService.registerInstance(applicationName,"127.0.0.1",serverPort); } @RequestMapping(value = "/getInstance", method = GET) @ResponseBody public List<Instance> getInstance(@RequestParam String serviceName) throws NacosException { return namingService.getAllInstances(serviceName); } public static void main(String[] args) { SpringApplication.run(Springboot2NacosDiscoveryApplication.class, args); } } 複製程式碼
本文用到了兩個Nacos的方法,如下:
- registerInstance:註冊例項,有多個方法,本文使用的方法需要傳入三個引數,分別是:服務名,ip和埠號。
- getAllInstances:獲取例項,傳入服務名。
到這裡就配置完成了,啟動專案,檢視Nacos服務如圖所示。

在瀏覽器訪問 http://localhost:8080/get?serviceName=springboot2-nacos-discovery ,如圖所示,也可以查詢到剛剛註冊的例項。

還有很多Nacos Api供我們使用,可以檢視Nacos Api頁面: nacos.io/zh-cn/docs/…