本文介绍 SpringBoot 应用使用 Nacos 服务发现.
上一篇文章介绍了 SpringBoot 使用 Nacos 做配置中心, 本文介绍 SpringBoot 使用 Nacos 做服务发现.
1.Eureka 闭源
相信到现在, Eureka 2.0 闭源已经不是什么新鲜事了. 在 2017-2018 年, 几乎在国内掀起了一阵 SpringCloud 的热潮, 几乎很大一部分人群随着对 SpringBoot 的关注, 都开始关注起来了 SpringCloud. 而由于 Eureka 注册中心的易整合等优点, 更是大多数使用 SpringCloud 的首选注册中心. 但是随着 Eureka 官网的宣告, 如下.
大致意思就是开源工作已经停止之类的话, 这里就不做介绍了, 感兴趣可以上 Eureka 的 GitHub 地址上查看 https://github.com/Netflix/eureka/wiki .
Nacos 也是一个优秀的注册中心, 并且由阿里巴巴开源, 并且最近的热度很高, 已经更新到 0.8.0 版本了, 基本上更新的很频繁, 也是一个 Eureka 闭源后的好的选择.
首先, 需要启动 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 服务如图所示.
在浏览器访问, 如图所示, 也可以查询到刚刚注册的实例.
还有很多 Nacos API 供我们使用, 可以查看 Nacos API 页面: https://nacos.io/zh-cn/docs/open-API.html
来源: https://yq.aliyun.com/articles/689619