Пример #1
0
        /// <inheritdoc />
        /// <summary> 创建客户端 </summary>
        /// <param name="serviceAddress">终结点。</param>
        /// <returns>传输客户端实例。</returns>
        public IMicroClient CreateClient(ServiceAddress serviceAddress)
        {
            //_logger.Debug($"准备为服务端地址:{endPoint}创建客户端。");
            try
            {
                var lazyClient = _clients.GetOrAdd(serviceAddress.ToString(), k => new Lazy <IMicroClient>(() =>
                {
                    _logger.LogDebug($"创建客户端:{serviceAddress}创建客户端。");
                    var bootstrap = _bootstrap;
                    var channel   = bootstrap.ConnectAsync(serviceAddress.ToEndPoint(false)).Result;
                    var listener  = new MessageListener();
                    var sender    = new DotNettyClientSender(_codecFactory.GetEncoder(), channel);
                    channel.GetAttribute(ListenerKey).Set(listener);
                    channel.GetAttribute(SenderKey).Set(sender);

                    channel.GetAttribute(ServiceAddressKey).Set(serviceAddress);
                    return(new MicroClient(_logger, sender, listener, _microExecutor));
                }
                                                                                                           ));
                return(lazyClient.Value);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建客户端失败");
                _clients.TryRemove(serviceAddress.ToString(), out _);
                throw;
            }
        }
Пример #2
0
        /// <summary> 创建客户端 </summary>
        /// <param name="serviceAddress"></param>
        /// <returns></returns>
        public Task <IMicroClient> CreateClient(ServiceAddress serviceAddress)
        {
            var lazyClient = _clients.GetOrAdd(serviceAddress, k => new Lazy <Task <IMicroClient> >(() =>
            {
                _logger.LogDebug($"创建客户端:{serviceAddress}创建客户端。");
                var listener = new MessageListener();
                var url      = serviceAddress.ToString();
                var sender   =
                    new HttpClientMessageSender(_loggerFactory, _clientFactory, _codecFactory, url, listener);
                IMicroClient client = new MicroClient(sender, listener, _microExecutor, _loggerFactory);
                return(Task.FromResult(client));
            }
                                                                                                    ));

            return(lazyClient.Value);
        }
Пример #3
0
        public async Task Send(DMessage message, bool flush = true)
        {
            if (!(message is InvokeMessage invokeMessage))
            {
                return;
            }

            var uri    = new Uri(new Uri(_address.ToString()), "micro/executor");
            var client = _clientFactory.CreateClient();
            var req    = new HttpRequestMessage(HttpMethod.Post, uri.AbsoluteUri);

            if (invokeMessage.Headers != null)
            {
                foreach (var header in invokeMessage.Headers)
                {
                    req.Headers.Add(header.Key, header.Value);
                }
            }

            var codec = _provider.GetClientCodec(_address.Codec);

            var data = await codec.EncodeAsync(message, _address.Gzip);

            req.Content = new ByteArrayContent(data);
            if (_address.Gzip)
            {
                req.Content.Headers.ContentEncoding.Add("gzip");
            }
            var resp = await client.SendAsync(req);

            if (!resp.IsSuccessStatusCode)
            {
                throw new SpearException($"服务请求异常,状态码{(int)resp.StatusCode}");
            }
            var content = await resp.Content.ReadAsByteArrayAsync();

            var result = await codec.DecodeAsync <MessageResult>(content, _address.Gzip);

            await _listener.OnReceived(this, result);
        }
Пример #4
0
        protected override async Task <IMicroClient> Create(ServiceAddress address)
        {
            var listener  = new MessageListener();
            var webSocket = new ClientWebSocket();
            var uri       = new Uri(new Uri(address.ToString()), "/micro/ws");
            await webSocket.ConnectAsync(uri, CancellationToken.None);

            var codec  = Provider.GetClientCodec(address.Codec);
            var sender = new WebSocketMessageSender(webSocket, codec, address.Gzip);

            var completion   = new TaskCompletionSource <object>();
            var socketClient = new WebSocketClient(webSocket, LoggerFactory, completion);

            socketClient.OnReceive += async buffer =>
            {
                var resultMessage = await codec.DecodeAsync <MessageResult>(buffer, address.Gzip);

                await listener.OnReceived(sender, resultMessage);
            };
            socketClient.OnClose += (key, socket) => Remove(address);

            _ = Task.Run(() => socketClient.ReceiveAsync(CancellationToken.None).ConfigureAwait(false));
            return(new MicroClient(sender, listener, MicroExecutor, LoggerFactory));
        }
Пример #5
0
        public IMicroClient CreateClient(ServiceAddress serviceAddress)
        {
            var lazyClient = _clients.GetOrAdd(serviceAddress, k => new Lazy <IMicroClient>(() =>
            {
                _logger.LogDebug($"创建客户端:{serviceAddress}创建客户端。");
                var listener = new MessageListener();
                var sender   = new HttpClientMessageSender(_logger, _clientFactory, _codecFactory, serviceAddress.ToString(),
                                                           listener);
                return(new MicroClient(_logger, sender, listener, _microExecutor));
            }
                                                                                            ));

            return(lazyClient.Value);
        }