nacos-注册中心服务注册接口及订阅通知类

本文深入解读Nacos 2.x版本的实例注册接口与订阅通知机制。详细探讨了InstanceOperatorClientImpl类、临时节点业务、订阅者处理及事件通知的源码实现,包括客户端注册服务事件、异步推送数据到客户端的过程,以及通知中心的单例设计和事件发布订阅模式。
摘要由CSDN通过智能技术生成

nacos源码解读
版本基于2.x

实例注册接口

public class InstanceControllerV2{
   
	//开关类 SwitchManager对该类进行管理
	@Autowired
    private SwitchDomain switchDomain;
    
    /**
    * 实例操作类
    * InstanceOperatorClientImpl 2.x版本实现类
    * InstanceOperatorServiceImpl 1.x版本实现类
    * 都实现接口InstanceOperator
    */
    @Autowired
    private InstanceOperatorClientImpl instanceServiceV2;
    
	@CanDistro
    @PostMapping
    @Secured(action = ActionTypes.WRITE)
    public String register(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId,
            @RequestParam String serviceName, @RequestParam String ip,
            @RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String cluster,
            @RequestParam Integer port, @RequestParam(defaultValue = "true") Boolean healthy,
            @RequestParam(defaultValue = "1") Double weight, @RequestParam(defaultValue = "true") Boolean enabled,
            @RequestParam String metadata, @RequestParam Boolean ephemeral) throws Exception {
   
        
        NamingUtils.checkServiceNameFormat(serviceName);
        checkWeight(weight);
        final Instance instance = InstanceBuilder.newBuilder().setServiceName(serviceName).setIp(ip)
                .setClusterName(cluster).setPort(port).setHealthy(healthy).setWeight(weight).setEnabled(enabled)
                .setMetadata(UtilsAndCommons.parseMetadata(metadata)).setEphemeral(ephemeral).build();
        if (ephemeral == null) {
   
            instance.setEphemeral((switchDomain.isDefaultInstanceEphemeral()));
        }
        //注册实例
        instanceServiceV2.registerInstance(namespaceId, serviceName, instance);
        return "ok";
    }
}

实例操作类(2.x版本使用InstanceOperatorClientImpl)

public class InstanceOperatorClientImpl implements InstanceOperator {
   
	/**
	* 针对客户端的业务类
	* EphemeralClientOperationServiceImpl 临时节点业务类
	* PersistentClientOperationServiceImpl 持久节点业务类
	* ClientOperationServiceProxy 临时、持久节点业务代理类
	* 都实现了ClientOperationService接口
	*/
	private final ClientOperationService clientOperationService;

	@Override
    public void registerInstance(String namespaceId, String serviceName, Instance instance) {
   
        boolean ephemeral = instance.isEphemeral();
        String clientId = IpPortBasedClient.getClientId(instance.toInetAddr(), ephemeral);
        createIpPortClientIfAbsent(clientId);
        Service service = getService(namespaceId, serviceName, ephemeral);
        //注册实例
        clientOperationService.registerInstance(service, instance, clientId);
    }
}

临时节点业务类

public class EphemeralClientOperationServiceImpl implements ClientOperationService {
   
	private final ClientManager clientManager;
	@Override
    public void registerInstance(Service service, Instance instance, String clientId) {
   
        Service singleton = ServiceManager
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值