Example #1
0
        /// <summary>
        /// 服务订阅
        /// </summary>
        /// <param name="serviceType">服务类型</param>
        /// <param name="host">服务Host</param>
        /// <param name="port">服务端口</param>
        /// <param name="serializerName">序列化器名称</param>
        /// <returns></returns>
        public IServicePublisher Publish(Type serviceType, string host, int port, string serializerName)
        {
            logger.LogDebug($"Publish service begin. ServiceType={serviceType.FullName}, Host={host}, Port={port}, SerializerName={serializerName}");

            var serviceAttr = serviceType.GetServiceAttribute();

            if (serviceAttr == null)
            {
                throw new InvalidOperationException($"ServiceType has not set ServiceAttribute. Type={serviceType.FullName}");
            }

            var serviceId = serviceAttr.ServiceId;

            CreateBaseNode();

            var servicePath = CreateServiceNode(serviceId);

            var servicePublishInfo = new ServicePublishInfo()
            {
                Host           = host,
                Port           = port,
                SerializerName = serializerName
            };

            CreateHostNode(servicePath, servicePublishInfo);

            logger.LogDebug($"Publish service finished. ServiceType={serviceType.FullName}, Host={host}, Port={port}, SerializerName={serializerName}");

            return(this);
        }
Example #2
0
        private void CreateHostNode(string servicePath, ServicePublishInfo servicePublishInfo)
        {
            var hostName = Utils.GetHostName(servicePublishInfo.Host, servicePublishInfo.Port);
            var hostPath = GetHostPath(servicePath, hostName);
            var data     = MessagePackSerializer.Serialize(servicePublishInfo);

            if (!client.ExistsAsync(hostPath).Result)
            {
                try
                {
                    client.CreateEphemeralAsync(hostPath, data).Wait();
                }
                catch (AggregateException ex)
                {
                    if (!(ex.InnerException is NodeExistsException))
                    {
                        throw ex;
                    }
                }
            }
        }