Nacos2.0学习(一) 客户端到服务端

先从客户端开始看吧,基于2.0.0
nacos的服务注册服务发现只需引入一个依赖
对应本次流程

image.png

网上搜到的
https://www.processon.com/view/link/5ea27ca15653bb6efc68eb8c


            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        

这个依赖自动装配了两类东西,第一个对应@EnableAutoConfiguration注解,一个用于读取bootstrap里的配置信息。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.alibaba.cloud.nacos.NacosDiscoveryAutoConfiguration,\
  com.alibaba.cloud.nacos.ribbon.RibbonNacosAutoConfiguration,\
  com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpointAutoConfiguration,\
  com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientAutoConfiguration,\
  com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
  com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration

先从@EnableAutoConfiguration注解开始入手,就不贴源码了,具体Bean的作用都在开头的图里。其实就是将配置信息装配起来并交由spring管理,真正的与服务端连接的实现逻辑交由nacos-client实现,先看注册流程,可以看到有二种不同的实现方式,grpc与http,nacos2.0就开始使用grpc了,所以还是看grpc吧哈哈哈


image.png

这里插播一下概念。
image.png

然后就是grpc调用了,并放入本地缓存
@Override
    public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
        NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance {}", namespaceId, serviceName,
                instance);
        InstanceRequest request = new InstanceRequest(namespaceId, serviceName, groupName,
                NamingRemoteConstants.REGISTER_INSTANCE, instance);
        requestToServer(request, Response.class);
        namingGrpcConnectionEventListener.cacheInstanceForRedo(serviceName, groupName, instance);
    }
//这是namingGrpcConnectionEventListener.cacheInstanceForRedo里面的存储map,不过多展开
private final ConcurrentMap> registeredInstanceCached = new ConcurrentHashMap>();
//真正的发送是在package com.alibaba.nacos.api.grpc.auto;不过多展开,该方法的封装是在package com.alibaba.nacos.common.remote.client.grpc;
public void request(com.alibaba.nacos.api.grpc.auto.Payload request,
                io.grpc.stub.StreamObserver responseObserver) {
            asyncUnaryCall(getChannel().newCall(getRequestMethod(), getCallOptions()), request, responseObserver);
        }

好了从这里就开始进入nacos服务端了。发送到nacos-naming的registerInstance,其中经历一个filter

@Component
public class InstanceRequestHandler extends RequestHandler {
    
    private final EphemeralClientOperationServiceImpl clientOperationService;
    
    public InstanceRequestHandler(EphemeralClientOperationServiceImpl clientOperationService) {
        this.clientOperationService = clientOperationService;
    }
    
    @Override
    public InstanceResponse handle(InstanceRequest request, RequestMeta meta) throws NacosException {
        Service service = Service
                .newService(request.getNamespace(), request.getGroupName(), request.getServiceName(), true);
        switch (request.getType()) {
            case NamingRemoteConstants.REGISTER_INSTANCE:
                return registerInstance(service, request, meta);
            case NamingRemoteConstants.DE_REGISTER_INSTANCE:
                return deregisterInstance(service, request, meta);
            default:
                throw new NacosException(NacosException.INVALID_PARAM,
                        String.format("Unsupported request type %s", request.getType()));
        }
    }
    
    private InstanceResponse registerInstance(Service service, InstanceRequest request, RequestMeta meta) {
        clientOperationService.registerInstance(service, request.getInstance(), meta.getConnectionId());
        return new InstanceResponse(NamingRemoteConstants.REGISTER_INSTANCE);
    }
    
    private InstanceResponse deregisterInstance(Service service, InstanceRequest request, RequestMeta meta) {
        clientOperationService.deregisterInstance(service, request.getInstance(), meta.getConnectionId());
        return new InstanceResponse(NamingRemoteConstants.DE_REGISTER_INSTANCE);
    }
    
}

下面就到了nacos的EventBus事件分配处理了,开下一章讲。

你可能感兴趣的:(Nacos2.0学习(一) 客户端到服务端)