Example #1
0
        public static void Main(string[] args)
        {
            //1) 创建连接到ZBUS总线
            ConnectionConfig connCfg = new ConnectionConfig();

            connCfg.Host = "127.0.0.1";
            connCfg.Port = 15555;
            Connection conn = new Connection(connCfg);

            //2) 注册服务(MyService)
            WorkerConfig workerCfg = new WorkerConfig();

            workerCfg.Service = "MyService";
            workerCfg.Mode    = WorkerConfig.MODE_LB;//负载均衡模式
            Worker worker = new Worker(conn, workerCfg);

            Console.WriteLine("C# Simple Worker Running...");
            //3) 服务业务逻辑循环体(等待ZBUS总线分发请求,处理请求,返回请求结果)
            while (true)
            {
                try
                {
                    //3.1) 等待ZBUS总线分发请求消息
                    ZMsg msg = worker.Recv();

                    //3.2) 业务逻辑处理请求消息
                    msg.Dump();

                    //3.3) 返回处理后的消息
                    msg = new ZMsg();
                    msg.PushBack("this is from c# loadbalancer");
                    worker.Send(msg);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    break;
                }
            }

            //4) 清除连接相关
            conn.Destroy();
            worker.Destroy();
        }
Example #2
0
        public Worker(Connection connection, WorkerConfig config)
        {
            if (config.Service == null || config.Service == "")
            {
                throw new ZBusException("WorkerConfig, missing service");
            }
            if (config.Mode != WorkerConfig.MODE_LB && config.Mode != WorkerConfig.MODE_BC &&
                config.Mode != WorkerConfig.MODE_PUBSUB)
            {
                throw new ZBusException("worker mode wrong");
            }
            this.connection    = connection;
            this.service       = config.Service;
            this.mode          = config.Mode;
            this.registerToken = config.RegisterToken;
            this.accessToken   = config.AccessToken;

            IntPtr c_cfg = C.zbuswrk_cfg_new(this.service, this.mode, this.registerToken, this.accessToken);

            this._handle = C.zbuswrk_new(c_cfg);
            C.zbuswrk_cfg_destroy(out c_cfg);
        }
Example #3
0
        protected void RunHandler(int threadCount, object handler)
        {
            if (config.Service == null)
            {
                throw new ZBusException("service required for config");
            }
            if (!(handler is ServiceHandler) && !(handler is WorkerHandler))
            {
                throw new ZBusException("handler invalid");
            }

            ZContext ctx = null;

            if (config.Ctx == null)
            {
                ctx        = new ZContext(1);
                config.Ctx = ctx;
            }

            List <Thread> threads = new List <Thread>();

            foreach (string broker in this.config.Brokers)
            {
                ConnectionConfig connCfg   = new ConnectionConfig();
                WorkerConfig     workerCfg = new WorkerConfig();
                string[]         parts     = broker.Split(':');
                if (parts.Length != 2)
                {
                    string msg = string.Format("broker invalid: {0}", broker);
                    throw new ZBusException(msg);
                }
                connCfg.Host    = parts[0];
                connCfg.Port    = Convert.ToInt32(parts[1]);
                connCfg.Verbose = this.config.Verbose;
                connCfg.Ctx     = ctx;

                workerCfg.Service       = this.config.Service;
                workerCfg.Mode          = this.config.Mode;
                workerCfg.RegisterToken = this.config.RegisterToken;
                workerCfg.AccessToken   = this.config.AccessToken;


                for (int i = 0; i < threadCount; i++)
                {
                    WorkerThreadStart start  = new WorkerThreadStart(connCfg, workerCfg, handler);
                    Thread            thread = new Thread(new ThreadStart(start.Run));
                    threads.Add(thread);
                }
            }

            foreach (Thread thread in threads)
            {
                thread.Start();
            }
            foreach (Thread thread in threads)
            {
                thread.Join();
            }

            if (ctx != null)
            {
                ctx.Dispose();
            }
        }