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: sr-users@lists.sip-router.org
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
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                     # 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分配算法√

### Kamailio 中实现和配置负载均衡 Kamailio负载均衡功能主要依赖于其内置的 **Dispatcher 模块**来完成[^1]。该模块允许管理员定义一组目标服务器并将 SIP 请求分发到这些服务器上,从而达到负载均衡的目的。 #### 1. Dispatcher 模块简介 Dispatcher 模块通过维护一个目的地列表并根据预设算法将请求发送到不同的服务器上来实现负载均衡。它支持多种分配策略,例如轮询(Round Robin)、随机选择以及基于权重的选择等。 #### 2. 安装与启用 Dispatcher 模块 要使用 Dispatcher 模块,需确保在编译 Kamailio 时已启用了此模块。如果未安装,则可以通过重新编译 Kamailio 或者从包管理器中获取带有 Dispatcher 支持的版本[^2]。 编辑 `kamailio.cfg` 文件,在加载模块部分添加如下内容以启用 Dispatcher: ```bash loadmodule "dispatcher.so" ``` 接着初始化 Dispatcher 数据库表结构(如果有数据库存储需求),运行以下 SQL 命令创建所需表格: ```sql mysql> source /usr/share/kamailio/mysql/dispatcher.sql; ``` #### 3. 配置 Dispatcher 参数 在配置文件中设置 Dispatcher 的参数,指定目标地址及其属性。以下是典型的配置示例: ```cfg modparam("dispatcher", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio") modparam("dispatcher", "table_name", "dispatcher") modparam("dispatcher", "setid_col", "setid") modparam("dispatcher", "destination_col", "destination") modparam("dispatcher", "flags_col", "flags") modparam("dispatcher", "priority_col", "priority") modparam("dispatcher", "attrs_col", "attrs") modparam("dispatcher", "description_col", "description") route { ... if (is_method("INVITE")) { $du = ""; ds_select_dst("1", "0"); t_relay(); } } ``` 上述脚本片段展示了如何针对 INVITE 方法应用负载均衡逻辑。其中 `"ds_select_dst"` 函数用于选取下一个可用的目标节点,“1”表示使用的调度集 ID,而 “0” 则代表不考虑任何特殊标志位[^3]。 #### 4. 测试环境搭建实例 对于实际部署场景中的测试案例可以参考具体操作指南[^4]。比如在一个包含两台 Asterisk PBX 组成的小型集群环境中验证负载均衡效果。假设这两台设备分别位于 IP 地址 `192.168.1.31` 和 `192.168.1.32` 上,那么可以在 Dispatcher 表格里录入相应的记录条目以便后续查询匹配。 --- ### 总结 综上所述,借助 Kamailio 提供的强大插件体系——特别是 Dispatcher 插件的支持下,能够轻松构建起一套高效的 SIP 协议层面的负载均衡解决方案。这不仅有助于提升系统的整体性能表现,同时也增强了服务可靠性水平。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值