分享

Freeswitch 高级主题之用kamailio负载均衡

 icecity1306 2016-09-18

kamailio的前身叫openser, 和opensips是兄弟,作为出色的sip proxy,在大并发量使用时经常用于负载均衡 媒体服务器 Asterisk、Freeswitch等实现集群。


1. 安装kamailio
参考上一篇文章安装:Centos 6.4 上源码安装 kamailio 4.1


2. 配置 kamailio


下面为配置好的主配置文件,dispacher模块有一些参数需要注意,默认下一跳地址的配置支持数据库和文本文件,本文测试默认使用文本文件方式,
不用MySQL数据库存储,注释掉下面两行。
#modparam("dispatcher", "db_url", DBURL)
#modparam("dispatcher", "table_name", "dispatcher")
设置默认配置下一跳的文本文件位置:
modparam("dispatcher", "list_file", "/usr/local/etc/kamailio/dispatcher.list")


/usr/local/etc/kamailio/dispatcher.list
文件添加freeswitch地址
1 sip:192.168.5.4:5080
1 sip:192.168.5.5:5080
1 sip:192.168.5.6:5080


通过path 支持注册消息下一跳的记录。


定制化的kamailio配置文件如下:


  1. #!KAMAILIO  
  2. #  
  3. # sample config file for dispatcher module  
  4. # - load balancing of VoIP calls with round robin  
  5. # - dispatch REGISTER and presence requests  
  6. #  
  7. #  
  8. # Direct your questions about this file to: sr-users@lists.sip-router.org  
  9. #  
  10. # Refer to the Core CookBook at http://www./dokuwiki/doku.php  
  11. # for an explanation of possible statements, functions and parameters.  
  12. #  
  13. # Several features can be enabled using '#!define WITH_FEATURE' directives:  
  14. #  
  15. # *** To run in debug mode:   
  16. #     - define WITH_DEBUG  
  17. #  
  18.   
  19. #!ifndef DBURL  
  20. #!define DBURL "mysql://kamailio:kamailiorw@localhost/kamailio"  
  21. #!endif  
  22.   
  23. ####### Global Parameters #########  
  24.   
  25. #!ifdef WITH_DEBUG  
  26. debug=4  
  27. log_stderror=yes  
  28. #!else  
  29. debug=2  
  30. log_stderror=no  
  31. #!endif  
  32.   
  33.   
  34. memdbg=5  
  35. memlog=5  
  36.   
  37. log_facility=LOG_LOCAL0  
  38.   
  39. fork=yes  
  40. children=4  
  41.   
  42. /* comment the next line to enable TCP */  
  43. disable_tcp=no  
  44.   
  45. /* uncomment the next line to disable the auto discovery of local aliases  
  46.    based on revers DNS on IPs (default on) */  
  47. auto_aliases=no  
  48.   
  49. /* add local domain aliases */  
  50. # alias="mysipserver.com"  
  51.   
  52. port=60600  
  53.   
  54. /* uncomment and configure the following line if you want Kamailio to   
  55.    bind on a specific interface/port/proto (default bind on all available) */  
  56. # listen=udp:127.0.0.1:5060  
  57. listen=udp:192.168.5.3:60600  
  58.   
  59. sip_warning=no  
  60.   
  61. ####### Modules Section ########  
  62.   
  63. #set module path  
  64. mpath="/usr/local/lib/kamailio/modules_k/:/usr/local/lib64/kamailio/modules/"  
  65.   
  66. loadmodule "db_mysql.so"  
  67. loadmodule "mi_fifo.so"  
  68. loadmodule "kex.so"  
  69. loadmodule "tm.so"  
  70. loadmodule "tmx.so"  
  71. loadmodule "sl.so"  
  72. loadmodule "rr.so"  
  73. loadmodule "pv.so"  
  74. loadmodule "maxfwd.so"  
  75. loadmodule "textops.so"  
  76. loadmodule "siputils.so"  
  77. loadmodule "xlog.so"  
  78. loadmodule "sanity.so"  
  79. loadmodule "ctl.so"  
  80. loadmodule "mi_rpc.so"  
  81. loadmodule "acc.so"  
  82. loadmodule "dispatcher.so"  
  83.   
  84.   
  85. # ----------------- setting module-specific parameters ---------------  
  86.   
  87.   
  88. # ----- mi_fifo params -----  
  89. modparam("mi_fifo", "fifo_name", "/tmp/kamailio_fifo")  
  90.   
  91.   
  92. # ----- rr params -----  
  93. # add value to ;lr param to cope with most of the UAs  
  94. modparam("rr", "enable_full_lr", 1)  
  95. # do not append from tag to the RR (no need for this script)  
  96. modparam("rr", "append_fromtag", 0)  
  97.   
  98.   
  99. # ----- acc params -----  
  100. modparam("acc", "log_flag", 1)  
  101. modparam("acc", "failed_transaction_flag", 3)  
  102. modparam("acc", "log_extra",  
  103.     "src_user=$fU;src_domain=$fd;dst_ouser=$tU;dst_user=$rU;dst_domain=$rd;src_ip=$si")  
  104.   
  105. # ----- tm params -----  
  106. modparam("tm", "fr_timer", 2000)  
  107. modparam("tm", "fr_inv_timer", 40000)  
  108.   
  109. # ----- dispatcher params -----  
  110. #modparam("dispatcher", "db_url", DBURL)  
  111. #modparam("dispatcher", "table_name", "dispatcher")  
  112. modparam("dispatcher", "list_file", "/usr/local/etc/kamailio/dispatcher.list")  
  113. modparam("dispatcher", "flags", 2)  
  114. modparam("dispatcher", "dst_avp", "$avp(AVP_DST)")  
  115. modparam("dispatcher", "grp_avp", "$avp(AVP_GRP)")  
  116. modparam("dispatcher", "cnt_avp", "$avp(AVP_CNT)")  
  117. modparam("dispatcher", "sock_avp", "$avp(AVP_SOCK)")  
  118. modparam("path", "use_received", 1)  
  119. ####### Routing Logic ########  
  120.   
  121.   
  122. # main request routing logic  
  123.   
  124. route {  
  125.   
  126.     # per request initial checks  
  127.     route(REQINIT);  
  128.   
  129.     # handle requests within SIP dialogs  
  130.     route(WITHINDLG);  
  131.   
  132.     ### only initial requests (no To tag)  
  133.   
  134.     # CANCEL processing  
  135.     if (is_method("CANCEL"))  
  136.     {  
  137.         if (t_check_trans())  
  138.             t_relay();  
  139.         exit;  
  140.     }  
  141.   
  142.     t_check_trans();  
  143.   
  144.     # record routing for dialog forming requests (in case they are routed)  
  145.     # - remove preloaded route headers  
  146.     remove_hf("Route");  
  147.     if (is_method("INVITE|SUBSCRIBE"))  
  148.         record_route();  
  149.   
  150.     # account only INVITEs  
  151.     if (is_method("INVITE"))  
  152.     {  
  153.         setflag(1); # do accounting  
  154.     }  
  155.   
  156.     # handle presence related requests  
  157.     route(PRESENCE);  
  158.   
  159.     # handle registrations  
  160.     route(REGISTRAR);  
  161.   
  162.     if ($rU==$null)  
  163.     {  
  164.         # request with no Username in RURI  
  165.         sl_send_reply("484","Address Incomplete");  
  166.         exit;  
  167.     }  
  168.   
  169.     # dispatch destinations  
  170.     route(DISPATCH);  
  171. }  
  172.   
  173.   
  174. route[RELAY] {  
  175.     if (!t_relay()) {  
  176.         sl_reply_error();  
  177.     }  
  178.     exit;  
  179. }  
  180.   
  181. # Per SIP request initial checks  
  182. route[REQINIT] {  
  183.     if (!mf_process_maxfwd_header("10")) {  
  184.         sl_send_reply("483","Too Many Hops");  
  185.         exit;  
  186.     }  
  187.   
  188.     if(!sanity_check("1511", "7"))  
  189.     {  
  190.         xlog("Malformed SIP message from $si:$sp\n");  
  191.         exit;  
  192.     }  
  193. }  
  194.   
  195. # Handle requests within SIP dialogs  
  196. route[WITHINDLG] {  
  197.     if (has_totag()) {  
  198.         # sequential request withing a dialog should  
  199.         # take the path determined by record-routing  
  200.         if (loose_route()) {  
  201.             if (is_method("BYE")) {  
  202.                 setflag(1); # do accounting ...  
  203.                 setflag(3); # ... even if the transaction fails  
  204.             }  
  205.             route(RELAY);  
  206.         } else {  
  207.             if (is_method("SUBSCRIBE") && uri == myself) {  
  208.                 # in-dialog subscribe requests  
  209.                 route(PRESENCE);  
  210.                 exit;  
  211.             }  
  212.             if ( is_method("ACK") ) {  
  213.                 if ( t_check_trans() ) {  
  214.                     # non loose-route, but stateful ACK;  
  215.                     # must be ACK after a 487 or e.g. 404 from upstream server  
  216.                     t_relay();  
  217.                     exit;  
  218.                 } else {  
  219.                     # ACK without matching transaction ... ignore and discard.  
  220.                     exit;  
  221.                 }  
  222.             }  
  223.             sl_send_reply("404","Not here");  
  224.         }  
  225.         exit;  
  226.     }  
  227. }  
  228.   
  229. # Handle SIP registrations  
  230. route[REGISTRAR] {  
  231.     if(!is_method("REGISTER"))  
  232.         return;  
  233.     add_path_received();  
  234.     route(DISPATCH);  
  235.     exit;  
  236. }  
  237.   
  238. # Presence server route  
  239. route[PRESENCE] {  
  240.     if(!is_method("PUBLISH|SUBSCRIBE"))  
  241.         return;  
  242.   
  243.     sl_send_reply("404", "Not here");  
  244.     exit;  
  245. }  
  246.   
  247. # Dispatch requests  
  248. route[DISPATCH] {  
  249.     # round robin dispatching on gateways group '1'  
  250.     if(!ds_select_dst("1", "4"))  
  251.     {  
  252.         send_reply("404", "No destination");  
  253.         exit;  
  254.     }  
  255.     xlog("L_DBG", "--- SCRIPT: going to <$ru> via <$du>\n");  
  256.     t_on_failure("RTF_DISPATCH");  
  257.     route(RELAY);  
  258.     exit;  
  259. }  
  260.   
  261. # Sample failure route  
  262. failure_route[RTF_DISPATCH] {  
  263.     if (t_is_canceled()) {  
  264.         exit;  
  265.     }  
  266.     # next DST - only for 500 or local timeout  
  267.     if (t_check_status("500")  
  268.             or (t_branch_timeout() and !t_branch_replied()))  
  269.     {  
  270.         if(ds_next_dst())  
  271.         {  
  272.             t_on_failure("RTF_DISPATCH");  
  273.             route(RELAY);  
  274.             exit;  
  275.         }  
  276.     }  
  277. }  


测试:呼叫和注册送到kamailio 60600端口,默认是round robin分配算法


本文原创自 http://blog.csdn.net/voipmaker  转载注明出处,欢迎加入我的freeswitch 群:45211986

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多