本文介紹SpringBoot應用使用Nacos服務發現。html
上一篇文章介紹了SpringBoot使用Nacos作配置中心,本文介紹SpringBoot使用Nacos作服務發現。java
相信到如今,Eureka 2.0 閉源已經不是什麼新鮮事了。在2017-2018年,幾乎在國內掀起了一陣SpringCloud的熱潮,幾乎很大一部分人羣隨着對SpringBoot的關注,都開始關注起來了SpringCloud。而因爲Eureka註冊中心的易整合等優勢,更是大多數使用SpringCloud的首選註冊中心。可是隨着Eureka官網的宣告,以下。git
大體意思就是開源工做已經中止之類的話,這裏就不作介紹了,感興趣能夠上Eureka的Github地址上查看github.com/Netflix/eur…。github
Nacos也是一個優秀的註冊中心,而且由阿里巴巴開源,而且最近的熱度很高,已經更新到0.8.0版本了,基本上更新的很頻繁,也是一個Eureka閉源後的好的選擇。web
首先,須要啓動Nacos,這裏不作過多介紹。spring
建立項目,加入Nacos的服務發現的依賴nacos-discovery-spring-boot-starter,完整pom如代碼清單所示。apache
<?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服務的地址,如代碼清單所示。api
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服務註冊,而且建立方法根據實例名稱獲取實例,完整啓動類如代碼清單所示。springboot
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的方法,以下:
到這裏就配置完成了,啓動項目,查看Nacos服務如圖所示。
在瀏覽器訪問http://localhost:8080/get?serviceName=springboot2-nacos-discovery,如圖所示,也能夠查詢到剛剛註冊的實例。
還有不少Nacos Api供咱們使用,能夠查看Nacos Api頁面:nacos.io/zh-cn/docs/…