/// <summary>
        /// 消费方注册
        /// </summary>
        /// <param name="host"></param>
        /// <param name="configurator"></param>
        public void SubscribeAt(string host, IConsumeConfigurator configurator, ushort threadCount = 0)
        {
            var url        = string.Format("rabbitmq://{0}", host);
            var allEvents  = _reg.GetAllInstance <IEvent>().ToList();
            var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                cfg.Host(new Uri(url), h =>
                {
                    h.Username(rabbitMQConfig.Username);
                    h.Password(rabbitMQConfig.Password);
                });
                //cfg.UseConcurrencyLimit(2);
                foreach (var eventType in allEvents)
                {
                    var @event = eventType as Event;
                    cfg.ReceiveEndpoint(@event.QueueName, x =>
                    {
                        if (!string.IsNullOrEmpty(@event.ExchangeName))
                        {
                            x.ConfigureConsumeTopology = false;
                            x.Bind(@event.ExchangeName, a =>
                            {
                                a.RoutingKey   = @event.RoutingKey;
                                a.ExchangeType = @event.ExchangeType;
                                a.Durable      = true;
                            });
                        }
                        x.UseMessageRetry(r =>
                        {
                            //按照2,4,8,16进行4次重试,单位秒
                            r.Exponential(ConstField.RetryCount, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(16), TimeSpan.FromSeconds(2));
                            r.Handle <RequeueException>();
                        });
                        //异常记录
                        x.UseExceptionLogger();
                        //启动的线程数量,不设置的时候一般默认为cpu核数的两倍
                        //PrefetchCount:unack的数量
                        //UseConcurrencyLimit 线程的数量
                        if (threadCount > 0)
                        {
                            x.PrefetchCount = threadCount;
                            cfg.UseConcurrencyLimit(threadCount);
                        }
                        var consumers = GetQueueConsumers(eventType.GetType());
                        configurator.Configure(x, cfg, consumers);
                    });
                }
            });

            busControl.Start();
        }
예제 #2
0
        /// <summary>
        /// 配置发布者
        /// 根据IEvent消息去做配置
        /// </summary>
        /// <param name="fcg"></param>
        public void ConfigurePublisher(IRabbitMqBusFactoryConfigurator fcg)
        {
            var allEvents = _reg.GetAllInstance <IEvent>().ToList();

            foreach (var eventType in allEvents)
            {
                var type = eventType.GetType();
                Invoke(this, new Type[] { type }, "SetPublishConfig", fcg);
            }
        }