nacos的初步认识-服务注册-源码解析

nacos是什么?

1.1 来自于官网诠释

Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

1.2 来自于第一视角的角度来看

Nacos可以用来做服务注册,发现 以及配置管理;

2 提供者

2.1 服务注册 discover

2.1.1概述

将应用app的matedata 数据以及地址信息注册到 nacos-server端。

2.1.2 代码入口

NacosServiceRegistry.register(Registration registration)

@Override
	public void register(Registration registration) {

		if (StringUtils.isEmpty(registration.getServiceId())) {
			log.warn("No service to register for nacos client...");
			return;
		}
		/**
		获取nameservice实例 需要初始化*/
		NamingService namingService = namingService();
		/** 服务所注册服务id*/
		String serviceId = registration.getServiceId();
		/** 服务所注册服务id*/
		String group = nacosDiscoveryProperties.getGroup();
		/* 组装服务实例*/
		Instance instance = getNacosInstanceFromRegistration(registration);

		try {
		/* 注册*/
			namingService.registerInstance(serviceId, group, instance);
			log.info("nacos registry, {} {} {}:{} register finished", group, serviceId,
					instance.getIp(), instance.getPort());
		}
		catch (Exception e) {
			log.error("nacos registry, {} register failed...{},", serviceId,
					registration.toString(), e);
			// rethrow a RuntimeException if the registration is failed.
			// issue : https://github.com/alibaba/spring-cloud-alibaba/issues/1132
			rethrowRuntimeException(e);
		}
	}

2.1.3 初始化NacosNamingService

public NacosNamingService(Properties properties) throws NacosException {
        init(properties);
    }
    
    private void init(Properties properties) throws NacosException {
        ValidatorUtils.checkInitParam(properties);
        this.namespace = InitUtils.initNamespaceForNaming(properties);
        InitUtils.initSerialization();
        initServerAddr(properties);
        InitUtils.initWebRootContext();
        initCacheDir();
        initLogName(properties);
        
        this.eventDispatcher = new EventDispatcher();
        this.serverProxy = new NamingProxy(this.namespace, this.endpoint, this.serverList, properties);
        this.beatReactor = new BeatReactor(this.serverProxy, initClientBeatThreadCount(properties));
        this.hostReactor = new HostReactor(this.eventDispatcher, this.serverProxy, beatReactor, this.cacheDir,
                isLoadCacheAtStart(properties), initPollingThreadCount(properties));
    }
2.1.3.1注册NacosNamingService.registerInstance()
@Override
    public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
        String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName);
        /*instance.isEphemeral()默认是 true 表示为临时节点 */
        if (instance.isEphemeral()) {
        /*组装心跳包*/
            BeatInfo beatInfo = beatReactor.buildBeatInfo(groupedServiceName, instance);
            /*定时任务发送心跳包*/
            beatReactor.addBeatInfo(groupedServiceName, beatInfo);
        }
        /*服务注册*/
        serverProxy.registerService(groupedServiceName, groupName, instance);
    }

1.组装心跳包

public void addBeatInfo(String serviceName, BeatInfo beatInfo) {
        NAMING_LOGGER.info("[BEAT] adding beat: {} to beat map.", beatInfo);
        String key = buildKey(serviceName, beatInfo.getIp(), beatInfo.getPort());
        BeatInfo existBeat = null;
        //fix #1733
        if ((existBeat = dom2Beat.remove(key)) != null) {
            existBeat.setStopped(true);
        }
        dom2Beat.put(key, beatInfo);
        /* 观测组要代码-》BeatTask 这个对象必然有run方法*/
        executorService.schedule(new BeatTask(beatInfo), beatInfo.getPeriod(), TimeUnit.MILLISECONDS);
        MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size());
    }

BeatTask.run()

public void run() {
            if (beatInfo.isStopped()) {
                return;
            }
            /*获取心跳包发送的时期*/
            long nextTime = beatInfo.getPeriod();
            try {
                JsonNode result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled);
                long interval = result.get("clientBeatInterval").asLong();
                boolean lightBeatEnabled = false;
                if (result.has(CommonParams.LIGHT_BEAT_ENABLED)) {
                    lightBeatEnabled = result.get(CommonParams.LIGHT_BEAT_ENABLED).asBoolean();
                }
                BeatReactor.this.lightBeatEnabled = lightBeatEnabled;
                if (interval > 0) {
                    nextTime = interval;
                }
                int code = NamingResponseCode.OK;
                if (result.has(CommonParams.CODE)) {
                    code = result.get(CommonParams.CODE).asInt();
                }
                if (code == NamingResponseCode.RESOURCE_NOT_FOUND) {
                    Instance instance = new Instance();
                    instance.setPort(beatInfo.getPort());
                    instance.setIp(beatInfo.getIp());
                    instance.setWeight(beatInfo.getWeight());
                    instance.setMetadata(beatInfo.getMetadata());
                    instance.setClusterName(beatInfo.getCluster());
                    instance.setServiceName(beatInfo.getServiceName());
                    instance.setInstanceId(instance.getInstanceId());
                    instance.setEphemeral(true);
                    try {
                        serverProxy.registerService(beatInfo.getServiceName(),
                                NamingUtils.getGroupName(beatInfo.getServiceName()), instance);
                    } catch (Exception ignore) {
                    }
                }
            } catch (NacosException ex) {
                NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, code: {}, msg: {}",
                        JacksonUtils.toJson(beatInfo), ex.getErrCode(), ex.getErrMsg());
                
            }
            /*轮询发送*/
            
            executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS);
        }

2.服务注册
serverProxy.registerService(groupedServiceName, groupName, instance);

public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
        
    .....
        
        final Map<String, String> params = new HashMap<String, String>(16);
        params.put(CommonParams.NAMESPACE_ID, namespaceId);
        params.put(CommonParams.SERVICE_NAME, serviceName);
        params.put(CommonParams.GROUP_NAME, groupName);
        params.put(CommonParams.CLUSTER_NAME, instance.getClusterName());
        params.put("ip", instance.getIp());
        params.put("port", String.valueOf(instance.getPort()));
        params.put("weight", String.valueOf(instance.getWeight()));
        params.put("enable", String.valueOf(instance.isEnabled()));
        params.put("healthy", String.valueOf(instance.isHealthy()));
        params.put("ephemeral", String.valueOf(instance.isEphemeral()));
        params.put("metadata", JacksonUtils.toJson(instance.getMetadata()));
        
        reqApi(UtilAndComs.nacosUrlInstance, params, HttpMethod.POST);
        
    }

组装信息发送http请求

 public String reqApi(String api, Map<String, String> params, Map<String, String> body, List<String> servers,
            String method) throws NacosException {
        
        params.put(CommonParams.NAMESPACE_ID, getNamespaceId());
        
        if (CollectionUtils.isEmpty(servers) && StringUtils.isEmpty(nacosDomain)) {
            throw new NacosException(NacosException.INVALID_PARAM, "no server available");
        }
        
        NacosException exception = new NacosException();
        //获取nacos服务集群列表
        if (servers != null && !servers.isEmpty()) {
            /*随机轮询一个服务列表*/
            Random random = new Random(System.currentTimeMillis());
            int index = random.nextInt(servers.size());
            
            for (int i = 0; i < servers.size(); i++) {
                String server = servers.get(index);
                try {
                /*注册数据*/
                    return callServer(api, params, body, server, method);
                } catch (NacosException e) {
                    exception = e;
                    if (NAMING_LOGGER.isDebugEnabled()) {
                        NAMING_LOGGER.debug("request {} failed.", server, e);
                    }
                }
                index = (index + 1) % servers.size();
            }
        }
        
        if (StringUtils.isNotBlank(nacosDomain)) {
            for (int i = 0; i < UtilAndComs.REQUEST_DOMAIN_RETRY_COUNT; i++) {
                try {
                    return callServer(api, params, body, nacosDomain, method);
                } catch (NacosException e) {
                    exception = e;
                    if (NAMING_LOGGER.isDebugEnabled()) {
                        NAMING_LOGGER.debug("request {} failed.", nacosDomain, e);
                    }
                }
            }
        }
        
        NAMING_LOGGER.error("request: {} failed, servers: {}, code: {}, msg: {}", api, servers, exception.getErrCode(),
                exception.getErrMsg());
        
        throw new NacosException(exception.getErrCode(),
                "failed to req API:" + api + " after all servers(" + servers + ") tried: " + exception.getMessage());
        
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nacos-sdk-go是一个用于服务发现的开源软件包。它提供了一种简单且可靠的方式来实现服务发现功能,可以帮助开发人员更方便地构建分布式应用程序。 nacos-sdk-go基于Nacos开源项目开发,Nacos是阿里巴巴开源的一个服务发现和配置管理平台。nacos-sdk-go提供了一系列的API和函数,可以用于注册、发现和管理服务。它支持HTTP和GRPC协议,能够与不同编程语言和框架进行集成。 使用nacos-sdk-go进行服务发现非常简单。首先,我们需要在应用程序中导入nacos-sdk-go的包,并初始化一个Nacos客户端。然后,我们可以使用该客户端注册服务、获取服务列表以及注销服务。例如,我们可以使用RegisterInstance函数将一个实例注册Nacos服务注册表中。 当其他应用程序需要使用我们的服务时,它们可以使用nacos-sdk-go的DiscoverInstances函数来获取可用的服务实例列表。这样,我们的服务就可以被其他应用程序发现和使用了。 除了服务发现功能,nacos-sdk-go还提供了一些其他功能,如配置管理、动态配置刷新等。它可以帮助我们更好地管理和维护分布式应用程序的配置和服务。 总结来说,nacos-sdk-go是一个功能强大的服务发现工具,它可以帮助开发人员更方便地构建分布式应用程序。通过使用nacos-sdk-go,我们可以实现服务注册、发现和管理,并能够与其他应用程序进行无缝集成,提高应用程序的可用性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值