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

本文原创自 http://blog.csdn.net/voipmaker


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.kamailio.org/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  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值