Kamailio负载均分配置

 

        根据不同的目的,Kamailio提供了不同的模块完成各种消息分发策略。比如LCR模块,dispatcher模块。本文描述怎样利用dispatcher模块实现SIP消息的负载均分。

        假设kamailio安装在/usr/local/kamailio/目录下。

 

添加dispatcher.list

/usr/local/kamailio/etc/kamailio下添加dispatcher.list文件并编辑它,内容:

# line format

# setid(int) destination(sip uri) flags(int,opt) priority(int,opt) attributes(str,opt)

1 sip:192.168.1.8:5060

1 sip:192.168.1.9:5060

说明:dispatcher支持从数据库里配置,也支持从配置文件里配置,这里选择配置文件。
           配置里面的setid很重要,后面的配置脚本里会用到,每个ID表示一组,可以配置多个不同的分发组。
           这里假设需要分发给两个处理节点,IP分别中192.168.1.8和192.168.1.9
           dispatcher.list文件的位置可以放在你自己习惯的位置。

 

配置kamailio.cfg

编辑kamailio.cfg文件:

在模块加载区域里添加一行:

loadmodule "dispatcher.so"

在模块参数区域(# ----------------- setting module-specific parameters ---------------)里添加dispatcher模块的配置:

# ----- dispatcher params -----

modparam("dispatcher", "list_file", "/usr/local/kamailio/etc/kamailio/dispatcher.list")

modparam("dispatcher", "force_dst", 1)

 

在request_route段的route(LOCATION);前添加以下内容   

# dispatch destinations

route(DISPATCH);

 

在kamailio.cfg的文件末尾添加路由定义:

# Dispatch requests
route[DISPATCH] {
   # round robin dispatching on gateways group '1'
   if(!ds_select_dst("1", "4")) {
          send_reply("404", "No destination");
          exit;
   }

   xdbg("--- SCRIPT: going to <$ru> via <$du> (attrs: $xavp(_dsdst_=>attrs))\n");
   t_on_failure("RTF_DISPATCH");
   route(RELAY);
   exit;
}



# Try next destionations in failure route
failure_route[RTF_DISPATCH] {
   if (t_is_canceled()) {
          exit;
   }

   # next DST - only for 500 or local timeout
   if (t_check_status("500")
      or (t_branch_timeout() and !t_branch_replied())) {
          if(ds_next_dst()) {
                 xdbg("--- SCRIPT: retrying to <$ru> via <$du> (attrs: $xavp(_dsdst_=>attrs))\n");
                 t_on_failure("RTF_DISPATCH");
                 route(RELAY);
                 exit;
          }
   }
}

说明: if(!ds_select_dst("1", "4"))调用里的第一个参数,对应的就是dispatcher.list里配置的setid,第二个参数表示选路时使用的算法,具体可选算法请参考手册。