public async Task InvokeAsync(RemoteInvokeContext context, CancellationToken cancellationToken) { var mqttContext = context as MqttRemoteInvokeContext; if (mqttContext != null) { var invokeMessage = context.InvokeMessage; var host = NetUtils.GetHostAddress(); var addresses = await _mqttBrokerEntryManger.GetMqttBrokerAddress(mqttContext.topic); addresses = addresses.Except(new AddressModel[] { host }); foreach (var address in addresses) { try { var endPoint = address.CreateEndPoint(); if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug($"使用地址:'{endPoint}'进行调用。"); } var client = _transportClientFactory.CreateClient(endPoint); await client.SendAsync(invokeMessage).WithCancellation(cancellationToken); } catch (CommunicationException) { await _healthCheckService.MarkFailure(address); } catch (Exception exception) { _logger.LogError(exception, $"发起请求中发生了错误,服务Id:{invokeMessage.ServiceId}。"); } } } }
/// <summary> /// Registers the MQTT broker. /// </summary> /// <param name="topic">The topic.</param> protected async Task RegisterMqttBroker(string topic) { var addresses = await _mqttBrokerEntryManger.GetMqttBrokerAddress(topic); var host = NetUtils.GetHostAddress(); if (addresses == null || !addresses.Any(p => p.ToString() == host.ToString())) { await _mqttBrokerEntryManger.Register(topic, host); } }