1

I am looking for a ZeroMQ implementation of an ICommunicationListener that I can use with a service fabric to run a ZeroMQ endpoint on Azure.

I looked for hours and I can't find any. Does anyone know a solution for this? I currently use the "Service App Fabric / .net core 2.0 stateless service" template,
which allows me to override
IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners(),
when I have the ICommunicationListener implementation for ZeroMQ,
or to override a Task RunAsync(CancellationToken cancellationToken),
when I want to setup the sockets myself.

My first try won't work:

protected override async Task RunAsync(CancellationToken cancellationToken)
{
    using (var server = new ResponseSocket("tcp://xx.xx.xx.xx:xxxxx"))
    {
        while (!cancellationToken.IsCancellationRequested)
        {
            var message = server.ReceiveFrameBytes();
            ServiceEventSource.Current.ServiceMessage(this.Context, "Message {0}",
                System.Text.Encoding.UTF8.GetString(message));
        }
    }
}

Result of the above is a service that won't start. Can't find much logging except this:

"There was an error during CodePackage activation.The service host terminated with exit code:255"

4

2 回答 2

1

如果不存在,您可以创建自己的,方法是创建一个实现ICommunicationListener并从中返回它CreateServiceInstanceListeners。用于OpenAsync打开频道并开始收听。用于CloseAsync停止聆听。

看看这个 Service Bus 的实现,以获得灵感。

于 2018-04-24T12:06:55.473 回答
0

ICommunicationListener这是ZeroMQ 实现的粗略示例。此实现将充当 ZeroMQ ResponseSocket,但可以轻松更改为RequestSocketSubscriberSocket或您喜欢的任何类型的NetMQ.Sockets.*套接字实现。当然,它需要在实现中提供更多细节,例如在检索消息时不抛出异常,但它应该清楚地了解它是如何完成的。它受到接口的现有 dotnetcore 实现的极大启发ICommunicationListener

public class ZeroMqResponseSocketCommunicationListener : ICommunicationListener, IDisposable
{
    private readonly CancellationTokenSource _cancellationToken = new CancellationTokenSource();
    private readonly ResponseSocket _responseSocket = new ResponseSocket();
    private readonly ServiceContext _serviceContext;
    private readonly string _endpointName;

    public ZeroMqResponseSocketCommunicationListener(ServiceContext serviceContext, string endpointName)
    {
        if (string.IsNullOrEmpty(endpointName))
            throw new ArgumentException("endpointName cannot be null or empty string.");

        _serviceContext = serviceContext;
        _endpointName = endpointName;
    }

    public Task<string> OpenAsync(CancellationToken cancellationToken)
    {
        var address = GetListenerUrl();
        if (address == null)
            throw new InvalidOperationException("No Url returned from ZeroMqResponseSocketCommunicationListener.GetListenerUrl");


        _responseSocket.Bind(address);

        ThreadPool.QueueUserWorkItem(state => MessageHandler(_cancellationToken.Token));

        return Task.FromResult(address);
    }

    public Task CloseAsync(CancellationToken cancellationToken)
    {
        _responseSocket.Close();
        return Task.FromResult(true);
    }

    public void Abort()
    {
        _responseSocket.Close();
    }

    private void MessageHandler(CancellationToken cancellationToken)
    {
        while (!cancellationToken.IsCancellationRequested)
        {
            var message = _responseSocket.ReceiveFrameBytes();
            if (message != null)
                throw new Exception($"Message {Encoding.UTF8.GetString(message)}");
        }
    }

    private string GetListenerUrl()
    {
        var endpoints = _serviceContext.CodePackageActivationContext.GetEndpoints();

        if (!endpoints.Contains(_endpointName))
            throw new InvalidOperationException($"{_endpointName} not found in Service Manifest.");

        var serviceEndpoint = _serviceContext.CodePackageActivationContext.GetEndpoint(_endpointName);

        if (string.IsNullOrEmpty(serviceEndpoint.IpAddressOrFqdn))
            throw new InvalidOperationException("IpAddressOrFqdn not set on endpoint");

        if (serviceEndpoint.Port <= 0)
            throw new InvalidOperationException("Port not set on endpoint");

        var listenUrl = $"{serviceEndpoint.Protocol.ToString().ToLower()}://{serviceEndpoint.IpAddressOrFqdn}:{serviceEndpoint.Port}";

        return listenUrl;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!disposing || _responseSocket == null) return;

        try
        {
            _responseSocket.Close();
            _responseSocket.Dispose();
        }
        catch (Exception ex)
        {
            ServiceEventSource.Current.Message(ex.Message);
        }
    }
}

并在您的应用结构服务中返回 ZeroMqResponseSocketCommunicationListener:

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
    yield return new ServiceInstanceListener(listener => new ZeroMqResponseSocketCommunicationListener(listener, "EndpointName"));
}

确保您在服务的 ServiceManifest.xml 中指定了一个端点:

<Resources>
  <Endpoints>
    <Endpoint Name="EndpointName" Port="80" Protocol="tcp" />
  </Endpoints>
</Resources>
于 2018-05-02T21:09:02.747 回答