栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot使用Nacos服务发现

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot使用Nacos服务发现

本文介绍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闭源后的好的选择。

2.SpringBoot使用Nacos服务发现

首先,需要启动Nacos,这里不做过多介绍。

创建项目,加入Nacos的服务发现的依赖nacos-discovery-spring-boot-starter,完整pom如代码清单所示。



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.1.RELEASE
		 
	
	com.dalaoyang
	springboot2_nacos_discovery
	0.0.1-SNAPSHOT
	springboot2_nacos_discovery
	springboot2_nacos_discovery

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-devtools
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
			com.alibaba.boot
			nacos-discovery-spring-boot-starter
			0.2.1
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


配置文件配置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 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页面:https://nacos.io/zh-cn/docs/open-API.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/234942.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号