예제 #1
0
파일: ProducterInfo.cs 프로젝트: xwx2015/MQ
 /// <summary>
 /// //顺序轮询节点获取节点及分区,从而达到负载均衡的目的
 /// </summary>
 /// <param name="sendmessagecount"></param>
 /// <returns></returns>
 public LoadBalanceNodeInfo GetLoadBalanceNodeInfo()
 {
     try
     {
         lock (_operatorlock)
         {
             LoadBalanceNodeInfo info = new LoadBalanceNodeInfo();
             var p = LoadBalance.GetLoadBalancePartitionInfo();
             if (p == null)
                 return null;
             var partitionidinfo = PartitionRuleHelper.GetPartitionIDInfo(p.PartitionId);
             info.DataNodeModel = DataNodeModelDic[partitionidinfo.DataNodePartition]; info.MQPathPartitionModel = p.MQPathParitionModel;
             return info;
         }
     }
     catch (Exception exp)
     {
         ErrorLogHelper.WriteLine(-1, "", "GetLoadBalanceNodeInfo", "生产者负载均衡出错", exp);
         throw exp;
     }
 }
 /// <summary>
 /// //顺序轮询节点获取节点及分区,从而达到负载均衡的目的
 /// </summary>
 /// <param name="sendmessagecount"></param>
 /// <returns></returns>
 public LoadBalanceNodeInfo GetLoadBalanceNodeInfo()
 {
     try
     {
         lock (_operatorlock)
         {
             LoadBalanceNodeInfo info = new LoadBalanceNodeInfo();
             var p = LoadBalance.GetLoadBalancePartitionInfo();
             if (p == null)
             {
                 return(null);
             }
             var partitionidinfo = PartitionRuleHelper.GetPartitionIDInfo(p.PartitionId);
             info.DataNodeModel = DataNodeModelDic[partitionidinfo.DataNodePartition]; info.MQPathPartitionModel = p.MQPathParitionModel;
             return(info);
         }
     }
     catch (Exception exp)
     {
         ErrorLogHelper.WriteLine(-1, "", "GetLoadBalanceNodeInfo", "生产者负载均衡出错", exp);
         throw exp;
     }
 }
예제 #3
0
 /// <summary>
 /// 发送消息
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="objmsg"></param>
 public void SendMessage <T>(T objmsg)
 {
     try
     {
         var json = "";
         ProducterTimeWatchInfo.JsonHelperSerializer += Log.TimeWatchLog.Debug(() =>
         {
             if (!(objmsg is string))
             {
                 json = new Serialization.JsonHelper().Serializer(objmsg);
             }
             else
             {
                 json = objmsg as string;
             }
         });
         //发送消息有n次重试机会
         int errortrycount = 0;
         while (errortrycount < Context.ProducterInfo.LoadBalance.SendMessageErrorTryAgainCount)
         {
             LoadBalanceNodeInfo loadbalancenodeinfo = null;
             ProducterTimeWatchInfo.GetLoadBalanceNodeInfo += Log.TimeWatchLog.Debug(() =>
             {
                 loadbalancenodeinfo = Context.ProducterInfo.GetLoadBalanceNodeInfo();
             });
             if (loadbalancenodeinfo == null)
             {
                 throw new BusinessMQException(string.Format("无法获取队列{0}的可用的负载均衡数据节点", MQPath));
             }
             string datanodeconnectstring = new ProducterBLL().GetDataNodeConnectString(SystemParamConfig.Producter_DataNode_ConnectString_Template_ToSendMessage, loadbalancenodeinfo.DataNodeModel);
             var    partitionidinfo       = PartitionRuleHelper.GetPartitionIDInfo(loadbalancenodeinfo.MQPathPartitionModel.partitionid);
             var    manageservertime      = Context.ManageServerTime;//.AddSeconds(1);发送消息要比标准时间提前1s,这样消息分表可以提前1s
             string tablename             = PartitionRuleHelper.GetTableName(partitionidinfo.TablePartition, manageservertime);
             try
             {
                 ProducterTimeWatchInfo.SendMessage += Log.TimeWatchLog.Debug(() =>
                 {
                     double inserttime    = 0;
                     double allinserttime = Log.TimeWatchLog.Debug(() =>
                     {
                         inserttime = Log.TimeWatchLog.Debug(() =>
                         {
                             tb_messagequeue_dal dal = new tb_messagequeue_dal();
                             dal.TableName           = tablename;
                             SqlHelper.ExcuteSql(datanodeconnectstring, (c) =>
                             {
                                 dal.Add2(c, new tb_messagequeue_model()
                                 {
                                     message       = json,
                                     mqcreatetime  = DateTime.Now,
                                     sqlcreatetime = manageservertime
                                     ,
                                     source = (int)EnumMessageSource.Common,
                                     state  = (int)EnumMessageState.CanRead
                                 });
                             });
                         });
                     });
                     //ProducterTimeWatchTest.AddMessages(string.Format("总插入消息:{0}s,插入消息:{1}s",allinserttime,inserttime));
                 });
                 NetCommand.SendMessage(mqpath);
                 return;
             }
             catch (SqlException exp)
             {
                 ErrorLogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "SendMessage", string.Format("发送消息出现节点错误,节点:{0}", loadbalancenodeinfo.DataNodeModel.datanodepartition), exp);
                 Context.ProducterInfo.RemoveMQPathPartition(loadbalancenodeinfo.DataNodeModel.datanodepartition);//数据层出错视为数据节点异常,则移除。将在一定时间内尝试恢复
                 Context.ProducterInfo.LoadBalance.AddError(new ErrorLoadBalancePartitionInfo()
                 {
                     PartitionId = loadbalancenodeinfo.MQPathPartitionModel.partitionid, PartitionIndex = loadbalancenodeinfo.MQPathPartitionModel.partitionindex
                 });
                 //Context.IsNeedReload = true;
                 if (Context.SendMessageErrorTime == null)
                 {
                     Context.SendMessageErrorTime = DateTime.Now;
                 }
             }
             errortrycount++;
         }
         throw new BusinessMQException(string.Format("发送消息出现系统级错误,并超过重试次数,请检查。队列:{0}", MQPath));
     }
     catch (Exception exp)
     {
         ErrorLogHelper.WriteLine(Context.GetMQPathID(), Context.GetMQPath(), "SendMessage", "生产者发送消息出错", exp);
         throw exp;
     }
 }