kamailio的前身叫openser, 和opensips是兄弟,作为出色的sip proxy,在大并发量使用时经常用于负载均衡 媒体服务器 Asterisk、Freeswitch等实现集群。
1. 安装kamailio
参考上一篇文章安装:http://www.aiuxian.com/article/p-1454911.html
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配置文件如下:
003 | # sample config file for dispatcher module |
004 | # - load balancing of VoIP calls with round robin |
005 | # - dispatch REGISTER and presence requests |
008 | # Direct your questions about this file to: sr-users@lists.sip-router.org |
011 | # for an explanation of possible statements, functions and parameters. |
013 | # Several features can be enabled using '#!define WITH_FEATURE' directives: |
015 | # *** To run in debug mode: |
016 | # - define WITH_DEBUG |
023 | ####### Global Parameters ######### |
037 | log_facility=LOG_LOCAL0 |
042 | /* comment the next line to enable TCP */ |
045 | /* uncomment the next line to disable the auto discovery of local aliases |
046 | based on revers DNS on IPs (default on) */ |
049 | /* add local domain aliases */ |
050 | # alias="mysipserver.com" |
054 | /* uncomment and configure the following line if you want Kamailio to |
055 | bind on a specific interface/port/proto (default bind on all available) */ |
056 | # listen=udp:127.0.0.1:5060 |
057 | listen=udp:192.168.5.3:60600 |
061 | ####### Modules Section ######## |
064 | mpath="/usr/local/lib/kamailio/modules_k/:/usr/local/lib64/kamailio/modules/" |
066 | loadmodule "db_mysql.so" |
067 | loadmodule "mi_fifo.so" |
074 | loadmodule "maxfwd.so" |
075 | loadmodule "textops.so" |
076 | loadmodule "siputils.so" |
078 | loadmodule "sanity.so" |
080 | loadmodule "mi_rpc.so" |
082 | loadmodule "dispatcher.so" |
085 | # ----------------- setting module-specific parameters --------------- |
088 | # ----- mi_fifo params ----- |
089 | modparam("mi_fifo", "fifo_name", "/tmp/kamailio_fifo") |
092 | # ----- rr params ----- |
093 | # add value to ;lr param to cope with most of the UAs |
094 | modparam("rr", "enable_full_lr", 1) |
095 | # do not append from tag to the RR (no need for this script) |
096 | modparam("rr", "append_fromtag", 0) |
099 | # ----- 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") |
105 | # ----- tm params ----- |
106 | modparam("tm", "fr_timer", 2000) |
107 | modparam("tm", "fr_inv_timer", 40000) |
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 ######## |
122 | # main request routing logic |
126 | # per request initial checks |
129 | # handle requests within SIP dialogs |
132 | ### only initial requests (no To tag) |
135 | if (is_method("CANCEL")) |
144 | # record routing for dialog forming requests (in case they are routed) |
145 | # - remove preloaded route headers |
147 | if (is_method("INVITE|SUBSCRIBE")) |
150 | # account only INVITEs |
151 | if (is_method("INVITE")) |
153 | setflag(1); # do accounting |
156 | # handle presence related requests |
159 | # handle registrations |
164 | # request with no Username in RURI |
165 | sl_send_reply("484","Address Incomplete"); |
169 | # dispatch destinations |
181 | # Per SIP request initial checks |
183 | if (!mf_process_maxfwd_header("10")) { |
184 | sl_send_reply("483","Too Many Hops"); |
188 | if(!sanity_check("1511", "7")) |
190 | xlog("Malformed SIP message from $si:$sp\n"); |
195 | # Handle requests within SIP dialogs |
198 | # sequential request withing a dialog should |
199 | # take the path determined by record-routing |
201 | if (is_method("BYE")) { |
202 | setflag(1); # do accounting ... |
203 | setflag(3); # ... even if the transaction fails |
207 | if (is_method("SUBSCRIBE") && uri == myself) { |
208 | # in-dialog subscribe requests |
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 |
219 | # ACK without matching transaction ... ignore and discard. |
223 | sl_send_reply("404","Not here"); |
229 | # Handle SIP registrations |
231 | if(!is_method("REGISTER")) |
238 | # Presence server route |
240 | if(!is_method("PUBLISH|SUBSCRIBE")) |
243 | sl_send_reply("404", "Not here"); |
249 | # round robin dispatching on gateways group '1' |
250 | if(!ds_select_dst("1", "4")) |
252 | send_reply("404", "No destination"); |
255 | xlog("L_DBG", "--- SCRIPT: going to <$ru> via <$du>\n"); |
256 | t_on_failure("RTF_DISPATCH"); |
261 | # Sample failure route |
262 | failure_route[RTF_DISPATCH] { |
263 | if (t_is_canceled()) { |
266 | # next DST - only for 500 or local timeout |
267 | if (t_check_status("500") |
268 | or (t_branch_timeout() and !t_branch_replied())) |
272 | t_on_failure("RTF_DISPATCH"); |
测试:呼叫和注册送到kamailio 60600端口,默认是round robin分配算法√