微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NetMQ (ZeroMQ) 如何使“无经纪人可靠性自由职业者模式”发挥作用

如何解决NetMQ (ZeroMQ) 如何使“无经纪人可靠性自由职业者模式”发挥作用

我从 ZeroMQ 指南中得到的示例中遇到了一些问题,看起来类 ZSocket 和 ZContext 不存在。 我对 ZeroMQ 完全陌生(刚开始学习),我正在关注“ØMQ - The Guide”。第一个关于 REQ-REP 的例子非常简单,效果很好。但是现在我正在尝试更类似于我的目标“Brokerless Reliability (Freelance Pattern)”的方法,但这个方法不起作用。

我使用的是带有 C# 代码的 Visual Studio 2019,我创建了一个新项目,通过 Nuget 添加了 NetMQ V4.0.1.6 并将 server code 复制到我的项目中。我在使用 ZContext 和 ZSocket 时遇到错误。我已经检查了 API V3API V4,它们明显不同。该指南完全基于版本 3,我使用的是 V 4。我没有找到任何关于更改或更新或等效函数/类/方法的文档,我不知道如何将示例转换为 NetMQ V4 .

这是我的测试代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using NetMQ;

namespace Examples
{
    static partial class Program
    {
        public static void FLServer1(string[] args)
        {
            //
            // Freelance server - Model 1
            // Trivial echo service
            //
            // Author: Metadings
            //

            if (args == null || args.Length < 1)
            {
                Console.WriteLine();
                Console.WriteLine("Usage: ./{0} FLServer1 [Endpoint]",AppDomain.CurrentDomain.FriendlyName);
                Console.WriteLine();
                Console.WriteLine("    Endpoint  Where FLServer1 should bind on.");
                Console.WriteLine("              Default is tcp://127.0.0.1:7780");
                Console.WriteLine();
                args = new string[] { "tcp://127.0.0.1:7780" };
            }

            using (var context = new ZContext())
            using (var server = new ZSocket(context,ZSocketType.REP))
            {
                server.Bind(args[0]);

                Console.WriteLine("I: echo service is ready at {0}",args[0]);

                ZMessage message;
                ZError error;
                while (true)
                {
                    if (null != (message = server.ReceiveMessage(out error)))
                    {
                        using (message)
                        {
                            server.Send(message);
                        }
                    }
                    else
                    {
                        if (error == ZError.ETERM)
                            return; // Interrupted
                        throw new ZException(error);
                    }
                }
            }
        }
    }
}

解决方法

经过长时间的尝试理解该逻辑后,我发现了与 ZeroMQ V3 和 V4 的不同之处: https://github.com/zeromq/netmq/wiki/Migrating-to-v4

另外,我不小心找到了我要找的例子: https://github.com/NetMQ/Samples/tree/master/src/Brokerless%20Reliability%20(Freelance%20Pattern)/Model%20One

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。