微博縮短網(wǎng)址是如何實現(xiàn)的_負(fù)載集群教程
實現(xiàn)原理很簡單,主要是將用戶提交的 url 地址轉(zhuǎn)化成一個唯一的字串,這個字串就對應(yīng)著真實的 url,怎么樣實現(xiàn)這種轉(zhuǎn)換呢?
數(shù)據(jù)庫只有兩個字段seq(自增長數(shù)字)和url(數(shù)字的url地址,建立索引)。
用戶輸入一個url地址,查詢表是否包含此url,如果存在,則返回seq的數(shù)字,
如果不存在,則插入數(shù)據(jù)庫,得到一個新增加的自增seq數(shù)字,為了縮短數(shù)字占用的字符數(shù),我們可以把a(bǔ)bc等字母的大小寫用上。這樣10個數(shù)字,26個小寫字母,26個大小字母就組成了一個62進(jìn)制了。比如數(shù)字10000000000(100億)轉(zhuǎn)換后就是aUKYOA,只有6位了,這樣就能縮短很多的網(wǎng)址了。
<?php
//十進(jìn)制轉(zhuǎn)到其他制
function dec2any($num, $base=62, $index=false)
{
if (!$base)
{
$base = strlen($index);
}
elseif(!$index)
{
$index = substr("0123456789abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base );
}
$out = "";
for ($t = floor(log10($num) / log10($base )); $t >= 0; $t-- )
{
$a = floor( $num / pow( $base, $t ) );
$out = $out . substr( $index, $a, 1 );
$num = $num - ( $a * pow( $base, $t ) );
}
return $out;
}
function any2dec($num, $base=62, $index=false)
{
if (!$base)
{
$base = strlen( $index );
}
elseif(!$index)
{
$index = substr("0123456789abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base);
}
$out = 0;
$len = strlen($num) - 1;
for ($t = 0; $t <= $len; $t++)
{
$out = $out + strpos($index, substr($num, $t, 1 )) * pow($base, $len - $t);
}
return $out;
}
?>
得到縮短的網(wǎng)址以后,怎樣實現(xiàn)網(wǎng)址的轉(zhuǎn)發(fā)呢?可以利用 ttserver,將縮短網(wǎng)縮字串當(dāng)作key,真實的 url 地址當(dāng)作 value,存入ttserver中。ttserver本身就提供 http 訪問,只需要稍加修改就可以直接利用 ttserver 進(jìn)行縮短網(wǎng)址的轉(zhuǎn)發(fā):
在 ttserver 源碼目錄下找到 ttserver.c 這個文件,這里我用的是 tokyotyrant-1.1.39 ,跳到第 2981 行,將下面的幾行改成圖中所示:

保存退出,編譯安裝 ttserver,網(wǎng)上有很多安裝教程,可以參考。
啟動 ttserver,并向里面寫入一條 key 為 aaaaaa,value為 http://www.baidu.com 的值。
curl -X PUT http://127.0.0.10:11221/aaaaaa -d "http://www.baidu.com"
主要目的是用 http 訪問 ttserver 時直接取得到真實的 url 并做轉(zhuǎn)發(fā)。這樣做很方便,但不安全,ttserver 的 http 還支持刪除、修改、插入數(shù)據(jù)(當(dāng)然也可以修改 ttserver 的 http 訪問入口,屏蔽掉這幾種操作)。負(fù)載均衡方面,可以通過添加多條 A 記錄隨機(jī)轉(zhuǎn)發(fā)到不同的 ttserver 機(jī)器上,但這樣每臺機(jī)器上存放的數(shù)據(jù)必須相同,網(wǎng)上也有說過ttserver 存過千萬左右的數(shù)據(jù)以后不太穩(wěn)定。
利用 nginx 就能很好解決直接用 ttserver 的問題,用 nginx 過濾掉 http 訪問 ttserver 的刪除、修改、插入的操作,并為多臺 ttserver 提供反向代理的功能。如下圖所示:

安裝 nginx,我這里采用的是 nginx-0.8.36.tar.gz。安裝 nginx 請參考:http://blog.s135.com/nginx_php_v6。
打開 nginx.conf 配置文件:
#user nobody;
#啟動 8 個 nginx 進(jìn)程
worker_processes 8;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
# 用 epoll,最大連接數(shù)
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
# 由于只做轉(zhuǎn)發(fā),將超時時間設(shè)為 0
keepalive_timeout 0;
#gzip on;
# 反向代理 ttserver 1 號機(jī),這里我放在一臺機(jī)器上開了三個不同端口
upstream backend_1 {
server 127.0.0.10:11221 weight=5 max_fails=3 fail_timeout=1s;
}
# 反向代理 ttserver 2 號機(jī)
upstream backend_2 {
server 127.0.0.10:11221 weight=5 max_fails=3 fail_timeout=1s;
}
# 反向代理 ttserver 3 號機(jī)
upstream backend_3 {
server 127.0.0.10:11221 weight=5 max_fails=3 fail_timeout=1s;
}
server {
listen 80;
server_name url.cn;
#charset koi8-r;
#access_log logs/host.access.log main;
#當(dāng)路徑包含/count的時候,則代理到ttserver后端進(jìn)行請求數(shù)據(jù)。
#請注意,這里屏蔽了PUT,DELETE,POST方法,只是使用了GET,主要目的是為了安全性,
#因為DELETE,POST,PUT是可以修改數(shù)據(jù)的
location ~* /count(.*) {
if ($request_method = PUT ) {
return 403;
}
if ($request_method = DELETE ) {
return 403;
}
if ($request_method = POST ) {
return 403;
}
proxy_method GET;
}
#將以 a-z 為第一個字符的 url 代理到 ttserver 1 號機(jī)
location ~* "^/([a-z]{1})([a-zA-Z0-9]{5})" {
proxy_pass http://backend_1;
}
#將以 A-Z 為第一個字符的 url 代理到 ttserver 2 號機(jī)
location ~* "^/([A-Z]{1})([a-zA-Z0-9]{5})" {
proxy_pass http://backend_2;
}
#將以 0-9 為第一個字符的 url 代理到 ttserver 3 號機(jī)
location ~* "^/([0-9]{1})([a-zA-Z0-9]{5})" {
proxy_pass http://backend_3;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
保存 nginx.conf 退出,現(xiàn)在就可以啟動 ttserver了,我這里做演示,為了方便就在一臺機(jī)器的三個端口啟動了三個 ttserver。如圖:
![]()
這里用 /ttserver/url_1 存放 ttserver 1號機(jī)的數(shù)據(jù),依此類推,分別在 11222、11223啟動 ttserver。
接著啟動 nginx:
ulimit -SHn 65535 /usr/local/nginx/sbin/nginx
接著在服務(wù)器上用下面的命令插入測試數(shù)據(jù):
curl -X PUT http://127.0.0.10:11221/aaaaaa -d "http://www.baidu.com" curl -X PUT http://127.0.0.10:11222/Aaaaaa -d "http://www.soso.com" curl -X PUT http://127.0.0.10:11223/1aaaaa -d "http://www.qq.com"
配置你機(jī)器的 hosts 指向 nginx 服務(wù)器:
127.0.0.10 url.cn
現(xiàn)在我們就可以打開瀏覽器,輸入 http://url.cn/aaaaaa 就可以跳轉(zhuǎn)到 baidu 上了,http://url.cn/Aaaaaa 就可以跳轉(zhuǎn)到 soso 了,http://url.cn/1aaaaa 就可以跳轉(zhuǎn)到 qq 上。至此配置完成,nginx只做轉(zhuǎn)發(fā)工作,應(yīng)付大規(guī)模的訪問應(yīng)該沒什么問題,這也正是 nginx 所擅長的。ttserver 數(shù)據(jù)的取值操作也是很快的,在后面可以多開幾臺 ttserver,分散大量訪問時的負(fù)載。
前臺程序根據(jù)用戶提交的 url 生成短的 url 后,根據(jù)前面的 nginx 分發(fā)規(guī)則寫到某一臺 ttserver 中,就可以了。nginx還支持一直 url hash 的均衡,但需要安裝一個第三方模塊ngx_http_upstream_hash_module,具體可以參考:http://hl5o.cn/System/plus/view.php?aid=6486
- 相關(guān)鏈接:
- 教程說明:
負(fù)載集群教程-微博縮短網(wǎng)址是如何實現(xiàn)的
。