kamailio dispatch负载均衡

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配置文件如下:


001 #!KAMAILIO
002 #
003 # sample config file for dispatcher module
004 # - load balancing of VoIP calls with round robin
005 # - dispatch REGISTER and presence requests
006 #
007 #
008 # Direct your questions about this file to: [email protected]
009 #
010 # Refer to the Core CookBook at http://www.kamailio.org/dokuwiki/doku.php
011 # for an explanation of possible statements, functions and parameters.
012 #
013 # Several features can be enabled using '#!define WITH_FEATURE' directives:
014 #
015 # *** To run in debug mode:
016 #     - define WITH_DEBUG
017 #
018  
019 #!ifndef DBURL
020 #!define DBURL "mysql://kamailio:kamailiorw@localhost/kamailio"
021 #!endif
022  
023 ####### Global Parameters #########
024  
025 #!ifdef WITH_DEBUG
026 debug=4
027 log_stderror=yes
028 #!else
029 debug=2
030 log_stderror=no
031 #!endif
032  
033  
034 memdbg=5
035 memlog=5
036  
037 log_facility=LOG_LOCAL0
038  
039 fork=yes
040 children=4
041  
042 /* comment the next line to enable TCP */
043 disable_tcp=no
044  
045 /* uncomment the next line to disable the auto discovery of local aliases
046    based on revers DNS on IPs (default on) */
047 auto_aliases=no
048  
049 /* add local domain aliases */
050 # alias="mysipserver.com"
051  
052 port=60600
053  
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
058  
059 sip_warning=no
060  
061 ####### Modules Section ########
062  
063 #set module path
064 mpath="/usr/local/lib/kamailio/modules_k/:/usr/local/lib64/kamailio/modules/"
065  
066 loadmodule "db_mysql.so"
067 loadmodule "mi_fifo.so"
068 loadmodule "kex.so"
069 loadmodule "tm.so"
070 loadmodule "tmx.so"
071 loadmodule "sl.so"
072 loadmodule "rr.so"
073 loadmodule "pv.so"
074 loadmodule "maxfwd.so"
075 loadmodule "textops.so"
076 loadmodule "siputils.so"
077 loadmodule "xlog.so"
078 loadmodule "sanity.so"
079 loadmodule "ctl.so"
080 loadmodule "mi_rpc.so"
081 loadmodule "acc.so"
082 loadmodule "dispatcher.so"
083  
084  
085 # ----------------- setting module-specific parameters ---------------
086  
087  
088 # ----- mi_fifo params -----
089 modparam("mi_fifo", "fifo_name", "/tmp/kamailio_fifo")
090  
091  
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)
097  
098  
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")
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

你可能感兴趣的:(sip,sip)