环境
负载均衡+数据库服务器
Centos-5.4 + kamailio-3.1.4 + mysql-5.0.77-4.el5_6.6
IP:192.168.1.30
asterisk服务器1
Centos-5.4 + asterisk-1.8
IP:192.168.1.31
asterisk服务器2
Centos-5.4 + asterisk-1.8
IP:192.168.1.32
目的
1:假设一个简易的asterisk 集群,通过realtime 技术,在集中的一个数据库中管理所有集群节点的分机号和拨号规则
2:asterisk服务器1上分机号段为 8100-8199
asterisk服务器2上分机号段为 8200-8299
3: 实现同一台asteisk服务器上分机互打
4:实现同一号段分机互打
5:实现不同号段分机互打(由kamailio服务器转发)
配置
1:asterisk服务器1
[root@localhost asterisk]# cat /etc/asterisk/res_config_mysql.conf
[general]
dbhost = 192.168.1.30
dbname = asterisk
dbuser = root
dbpass = luckytuo@gmail.com
dbport = 3306
[root@localhost asterisk]# cat /etc/asterisk/extconfig.conf
[settings]
sipusers => mysql,general,ast_sipfriends ; edit by tolooney
sippeers => mysql,general,ast_sipfriends ; edit by tolooney
extensions => mysql,general,ast_extensions_01 ; edit by tolooney
[root@localhost asterisk]# cat /etc/asterisk/extensions.conf
[internal]
switch => Realtime/internal@extensions ; edit by tolooney
[root@localhost asterisk]# cat /etc/asterisk/sip.conf
[general]
context=internal
rtcachefriends=yes
disallow=all
allow=ulaw
allow=ilbc
2:asterisk服务器2
[root@localhost asterisk]# cat /etc/asterisk/res_config_mysql.conf
[general]
dbhost = 192.168.1.30
dbname = asterisk
dbuser = root
dbpass = luckytuo@gmail.com
dbport = 3306
[root@localhost asterisk]# cat /etc/asterisk/extconfig.conf
[settings]
sipusers => mysql,general,ast_sipfriends ; edit by tolooney
sippeers => mysql,general,ast_sipfriends ; edit by tolooney
extensions => mysql,general,ast_extensions_02 ; edit by tolooney
[root@localhost asterisk]# cat /etc/asterisk/extensions.conf
[internal]
switch => Realtime/internal@extensions ; edit by tolooney
[root@localhost asterisk]# cat /etc/asterisk/sip.conf
[general]
context=internal
rtcachefriends=yes
disallow=all
allow=ulaw
allow=ilbc
备注:分机号存放在ast_sipfriends表中,拨号规则分别存在ast_extensions_01,ast_extensions_02..表中
3:负载均衡+数据库服务器
执行如下sql,以生成realtime数据
– phpMyAdmin SQL Dump
– version 3.4.3.2
– http://www.phpmyadmin.net
–
– 主机: localhost
– 生成日期: 2011 年 08 月 07 日 11:05
– 服务器版本: 5.0.77
– PHP 版本: 5.3.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
–
– 数据库: `asterisk`
–
– ——————————————————–
–
– 表的结构 `ast_extensions_01`
–
CREATE TABLE IF NOT EXISTS `ast_extensions_01` (
`id` int(11) NOT NULL auto_increment,
`context` varchar(20) NOT NULL,
`exten` varchar(20) NOT NULL,
`priority` tinyint(4) NOT NULL default '0',
`app` varchar(20) NOT NULL,
`appdata` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
–
– 转存表中的数据 `ast_extensions_01`
–
INSERT INTO `ast_extensions_01` (`id`, `context`, `exten`, `priority`, `app`, `appdata`) VALUES
(6, 'internal', '8201', 1, 'Dial', 'SIP/8201@192.168.1.30'),
(4, 'internal-vm', '8102', 2, 'VoiceMail', 'SIP/8102'),
(3, 'internal', '8102', 1, 'Dial', 'SIP/8102'),
(1, 'internal', '8101', 1, 'Dial', 'SIP/8101'),
(2, 'internal-vm', '8101', 2, 'VoiceMail', 'SIP/8101'),
(7, 'internal', '8202', 1, 'Dial', 'SIP/8202@192.168.1.30');
– ——————————————————–
–
– 表的结构 `ast_extensions_02`
–
CREATE TABLE IF NOT EXISTS `ast_extensions_02` (
`id` int(11) NOT NULL auto_increment,
`context` varchar(20) NOT NULL,
`exten` varchar(20) NOT NULL,
`priority` tinyint(4) NOT NULL default '0',
`app` varchar(20) NOT NULL,
`appdata` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
–
– 转存表中的数据 `ast_extensions_02`
–
INSERT INTO `ast_extensions_02` (`id`, `context`, `exten`, `priority`, `app`, `appdata`) VALUES
(1, 'internal', '8201', 1, 'Dial', 'SIP/8201'),
(2, 'internal-vm', '8201', 2, 'VoiceMail', 'SIP/8201'),
(3, 'internal', '8202', 1, 'Dial', 'SIP/8202'),
(4, 'internal-vm', '8202', 2, 'VoiceMail', 'SIP/8202'),
(6, 'internal', '8101', 1, 'Dial', 'SIP/8101@192.168.1.30'),
(7, 'internal', '8102', 1, 'Dial', 'SIP/8102@192.168.1.30');
– ——————————————————–
–
– 表的结构 `ast_sipfriends`
–
CREATE TABLE IF NOT EXISTS `ast_sipfriends` (
`name` varchar(40) NOT NULL default '',
`type` varchar(10) NOT NULL default '',
`username` varchar(40) default NULL,
`fromuser` varchar(40) default NULL,
`fromdomain` varchar(40) default NULL,
`secret` varchar(40) default NULL,
`md5secret` varchar(40) default NULL,
`auth` varchar(10) default NULL,
`mailbox` varchar(20) default NULL,
`subscribemwi` varchar(10) default NULL,
`vmexten` varchar(20) default NULL,
`callerid` varchar(40) default NULL,
`cid_number` varchar(40) default NULL,
`callingpres` varchar(20) default NULL,
`usereqphone` varchar(10) default NULL,
`language` varchar(10) default NULL,
`incominglimit` varchar(10) default NULL,
`context` varchar(40) NOT NULL default '',
`subscribecontext` varchar(40) default NULL,
`amaflags` varchar(20) default NULL,
`accountcode` varchar(20) default NULL,
`musicclass` varchar(20) default NULL,
`mohsuggest` varchar(20) default NULL,
`allowtransfer` varchar(20) default NULL,
`callgroup` varchar(20) default NULL,
`pickupgroup` varchar(20) default NULL,
`autoframing` varchar(10) default NULL,
`disallow` varchar(20) default 'all',
`allow` varchar(20) default NULL,
`maxcallbitrate` varchar(15) default NULL,
`host` varchar(40) default 'dynamic',
`outboundproxy` varchar(40) default NULL,
`ipaddr` varchar(40) NOT NULL default '',
`defaultip` varchar(20) default NULL,
`port` int(6) NOT NULL default '0',
`fullcontact` varchar(40) default NULL,
`insecure` varchar(20) default NULL,
`qualify` varchar(15) default NULL,
`regseconds` int(11) NOT NULL default '0',
`defaultuser` varchar(128) default NULL,
`regexten` varchar(20) default NULL,
`regserver` varchar(20) default NULL,
`useragent` varchar(128) default NULL,
`rtptimeout` varchar(15) default NULL,
`rtpholdtimeout` varchar(15) default NULL,
`rtpkeepalive` varchar(15) default NULL,
`lastms` int(11) NOT NULL default '-1',
`setvar` varchar(200) default NULL,
PRIMARY KEY (`name`),
KEY `host` (`host`,`port`),
KEY `ipaddr` (`ipaddr`,`port`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
–
– 转存表中的数据 `ast_sipfriends`
–
INSERT INTO `ast_sipfriends` (`name`, `type`, `username`, `fromuser`, `fromdomain`, `secret`, `md5secret`, `auth`, `mailbox`, `subscribemwi`, `vmexten`, `callerid`, `cid_number`, `callingpres`, `usereqphone`, `language`, `incominglimit`, `context`, `subscribecontext`, `amaflags`, `accountcode`, `musicclass`, `mohsuggest`, `allowtransfer`, `callgroup`, `pickupgroup`, `autoframing`, `disallow`, `allow`, `maxcallbitrate`, `host`, `outboundproxy`, `ipaddr`, `defaultip`, `port`, `fullcontact`, `insecure`, `qualify`, `regseconds`, `defaultuser`, `regexten`, `regserver`, `useragent`, `rtptimeout`, `rtpholdtimeout`, `rtpkeepalive`, `lastms`, `setvar`) VALUES
('8102', 'friend', '8102', NULL, NULL, '8102', NULL, NULL, NULL, NULL, NULL, '8102', NULL, NULL, NULL, NULL, NULL, 'internal', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'all', 'all', NULL, 'dynamic', NULL, '(null)', NULL, 0, '', 'port,invite', NULL, 1312768532, NULL, NULL, '', '', NULL, NULL, NULL, 0, ''),
('8101', 'friend', '8101', NULL, NULL, '8101', NULL, NULL, NULL, NULL, NULL, '8101', NULL, NULL, NULL, NULL, NULL, 'internal', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'all', 'all', NULL, 'dynamic', NULL, '(null)', NULL, 0, '', 'port,invite', NULL, 1312771340, NULL, NULL, '', '', NULL, NULL, NULL, 0, ''),
('8201', 'friend', '8201', NULL, NULL, '8201', NULL, NULL, NULL, NULL, NULL, '8201', NULL, NULL, NULL, NULL, NULL, 'internal', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'all', 'all', NULL, 'dynamic', NULL, '(null)', NULL, 0, '', 'port,invite', NULL, 1312771140, NULL, NULL, '', '', NULL, NULL, NULL, 0, ''),
('8202', 'friend', '8202', NULL, NULL, '8202', NULL, NULL, NULL, NULL, NULL, '8202', NULL, NULL, NULL, NULL, NULL, 'internal', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'all', 'all', NULL, 'dynamic', NULL, '192.168.1.100', NULL, 9766, 'sip:8101@192.168.1.100:9766^3Brinstance=', 'port,invite', NULL, 1312766961, NULL, NULL, '', 'eyeBeam release 1011d stamp 40820', NULL, NULL, NULL, 0, '');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
vi /etc/kamailil/dispatcher.list
# line format
# setit(integer) destination(sip uri) flags (integer, optional)
1 sip:192.168.1.31:5060
2 sip:192.168.1.32:5060
vi /etc/kamailio/kamailil.cfg
loadmodule "dispatcher.so"
modparam("dispatcher", "list_file", "/etc/kamailio/dispatcher.list")
route{
if (!mf_process_maxfwd_header("10")) {
sl_send_reply("483","Too Many Hops");
exit;
}
if (uri=~"^sip:81[0-9]+@192.168.1.30$") {
if (is_method("INVITE")) {
ds_select_dst("1", "0");
forward();
exit;
}
}
if (uri=~"^sip:82[0-9]+@192.168.1.30$") {
if (is_method("INVITE")) {
ds_select_dst("2", "0");
forward();
exit;
}
}
sl_send_reply("404","Not here");
exit;
}
测试
注册8101和8102到192.168.1.31,测试相同号段分机互打,成功
注册8101到192.168.1.31 8102到192.168.1.32,测试不同号段分机互打,成功