背景

最近在钻研sip代理服务,国内大部分举荐应用opensips,第一工夫装置opensips来学习,理论在应用中发现opensips 3.x的坑很多,很多性能在2.x版本上有,然而在3.x上还未实现。光是sip做代理服务转发申请到freeswitch上,3.x版本配置上就没有一个能胜利应用的示例,配置都是2.x的,很多参数还不适配。整了2天都没搞明确,果决弃坑,选用kamailio,此时才发现kamailio是如许敌对。

装置kamailio

本次应用centos7.9零碎,kamailio的版本是5.6。
装置步骤比较简单,依照网上流程都能装置胜利。 我抉择的是源码装置,须要留神的是make cfg时,要抉择装置一些模块 既能够 make include_modules="db_mysql xxx" cfg 抉择装置的模块,也能够关上 ./src/modules.list 找到 exclude_modules,把须要装置的模块从这里删除。 之后就make && make install 。

配置sip代理服务


  • 运行kamailio
    kamailio的配置文件地位: /usr/local/etc/kamailio/

    改kamactlrc的mysql连贯,和kamailio.cfg里的DBURL,之后kamctl start 运行服务.如果报错:

    ERROR: PID file /run/kamailio/kamailio.pid does not exist -- Kamailio start failed

    查看 /var/log/message 日志,看看报错日志。

  • 配置dispatcher 代理

    官网文档配置参考: https://github.com/kamailio/kamailio/blob/master/src/modules/dispatcher/doc/dispatcher.cfg
    kamailio代理服务应用的是dispatcher模块,配置形式有两种:

  • 应用dispatcher.list
    在kamailio的配置地位下,新建dispatcher.list, 写入须要代理的fs服务:
# $Id$# dispatcher destination sets# setit(int) destination(sip uri) flags(int,opt) priority(int,opt) attributes(str,opt)# Freeswitch IPS 1 sip:192.168.40.143:5080 1 sip:192.168.30.100:5080

full example:

1 sip:127.0.0.1:5080 0 0 duid=abc;socket=udp:192.168.0.125:5060;my=xyz;ping_from=sip:myproxy.com

flags:

   1: inactive destination    2: temprary trying destination    4: admin disabled destination    8: probing destination (sending keep alives)   16: skip DNS A/AAAA resolve at startup 

attributes:

duid: It must be an unique value to identify a destination maxload: defining the upper limit of active calls per destination weight: beteen 1 and 100     rweight: between 1 and 100 socket: used to set the sending socket for the gateway sockname: ping_from:

而后在kamailio.cfg配置中,退出dispatcher应用

loadmodule "dispatcher.so"

modparam("dispatcher", "list_file", "/usr/local/etc/kamailio/dispatcher.list")modparam("dispatcher", "table_name", "dispatcher")modparam("dispatcher", "flags", 2)modparam("dispatcher", "xavp_dst", "_dsdst_")modparam("dispatcher", "xavp_ctx", "_dsctx_")modparam("dispatcher", "ds_ping_from", "sip:dispatcher@192.168.40.45")modparam("dispatcher", "ds_ping_interval", 60)modparam("dispatcher", "ds_probing_mode", 1)modparam("dispatcher", "ds_timer_mode", 1)
  • 应用mysql
#!ifndef DBURL#!define DBURL "mysql://kamailio:kamailiorw@localhost/kamailio"#!endif

loadmodule "dispatcher.so"

modparam("dispatcher", "db_url", DBURL)modparam("dispatcher", "table_name", "dispatcher")modparam("dispatcher", "flags", 2)modparam("dispatcher", "xavp_dst", "_dsdst_")modparam("dispatcher", "xavp_ctx", "_dsctx_")modparam("dispatcher", "ds_ping_from", "sip:dispatcher@192.168.40.45")modparam("dispatcher", "ds_ping_interval", 60)modparam("dispatcher", "ds_probing_mode", 1)modparam("dispatcher", "ds_timer_mode", 1)

参数解释:
ds_ping_method: 能够自定义ping办法,默认INFO
ds_hash_expire: 如果在指定工夫内(默认7200s)BYE未收到,就删掉这个路由。
ds_hash_initexpire: 如果在指定工夫内(默认7200s)INVITE的200ok未收到,就删掉这个路由。

  1. 配置转发
    request_route 最初增加 route(DISPATCH)

    route(REGISTRAR); if ($rU==$null) {     # request with no Username in RURI     sl_send_reply("484","Address Incomplete");     exit; } # dispatch destinations route(DISPATCH);
# Dispatch requestsroute[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 routefailure_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;        }    }}

route[DISPATCH] ds_select_dst("1", "4")
"1":代表配置的freeswitch中的setid
"4": round-robin 轮询抉择。
ds_select_dst 轮询配置 参考文档: https://kamailio.org/docs/modules/5.6.x/modules/dispatcher.ht...

论断


通过配置两个freeswitch, 抉择适当的调配形式能够把申请依照肯定的比例散发到两个freeswitch上。另外如果其中一个freeswitch服务死掉kamailio检测到之后,会把之后的申请全都转到另外的一个freeswitch服务上