成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

微博縮短網(wǎng)址用ttserver的實(shí)現(xiàn)方法

2010-12-03 09:56:56來源:javaeye作者:

  隨著類似Twitter的微型博客網(wǎng)站的出現(xiàn),由于字符數(shù)的限制,網(wǎng)址縮短服務(wù)日漸增多。加上網(wǎng)址縮短服務(wù)提供商提供網(wǎng)址追蹤等服務(wù),這一業(yè)務(wù)日漸興起。知名網(wǎng)址縮短服務(wù)商Bit.ly的主要業(yè)務(wù)便是為微博Twitter提供網(wǎng)址

  隨著類似Twitter的微型博客網(wǎng)站的出現(xiàn),由于字符數(shù)的限制,網(wǎng)址縮短服務(wù)日漸增多。加上網(wǎng)址縮短服務(wù)提供商提供網(wǎng)址追蹤等服務(wù),這一業(yè)務(wù)日漸興起。知名網(wǎng)址縮短服務(wù)商Bit.ly的主要業(yè)務(wù)便是為微博Twitter提供網(wǎng)址縮短服務(wù)。 比如sina微博的sinaurl.cn,騰訊微博的url.cn等。

  實(shí)現(xiàn)原理很簡單,主要是將用戶提交的 url 地址轉(zhuǎn)化成一個(gè)唯一的字串,這個(gè)字串就對(duì)應(yīng)著真實(shí)的 url,怎么樣實(shí)現(xiàn)這種轉(zhuǎn)換呢?

  url 的轉(zhuǎn)換摘自:http://www.cnblogs.com/sunli/archive/2010/03/25/1696183.html

  數(shù)據(jù)庫只有兩個(gè)字段seq(自增長數(shù)字)和url(數(shù)字的url地址,建立索引)。

  用戶輸入一個(gè)url地址,查詢表是否包含此url,如果存在,則返回seq的數(shù)字,如果不存在,則插入數(shù)據(jù)庫,得到一個(gè)新增加的自增seq數(shù)字,為了縮短數(shù)字占用的字符數(shù),我們可以把a(bǔ)bc等字母的大小寫用上。這樣10個(gè)數(shù)字,26個(gè)小寫字母,26個(gè)大小字母就組成了一個(gè)62進(jìn)制了。比如數(shù)字 10000000000(100億)轉(zhuǎn)換后就是aUKYOA,只有6位了,這樣就能縮短很多的網(wǎng)址了。

Php代碼
  1. <?php  
  2. //十進(jìn)制轉(zhuǎn)到其他制  
  3. function dec2any($num$base=62, $index=false)  
  4. {  
  5.   if (!$base)  
  6.   {  
  7.     $base = strlen($index);  
  8.   }  
  9.   elseif(!$index)   
  10.   {  
  11.     $index = substr("0123456789abcdefghijklmnopqrstuvwxyz  
  12.     ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base );  
  13.   }  
  14.   $out = "";  
  15.   for ($t = floor(log10($num) / log10($base )); $t >= 0; $t-- )   
  16.   {  
  17.     $a = floor$num / pow( $base$t ) );  
  18.     $out = $out . substr$index$a, 1 );  
  19.     $num = $num - ( $a * pow( $base$t ) );  
  20.   }  
  21.   return $out;  
  22. }  
  23.   
  24. function any2dec($num$base=62, $index=false)  
  25. {   
  26.   if (!$base)  
  27.   {  
  28.     $base = strlen$index );  
  29.   }  
  30.   elseif(!$index)  
  31.   {  
  32.     $index = substr("0123456789abcdefghijklmnopqrstuvwxyz  
  33.     ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base);  
  34.   }  
  35.   $out = 0;  
  36.   $len = strlen($num) - 1;  
  37.   for ($t = 0; $t <= $len$t++)  
  38.   {  
  39.     $out = $out + strpos($indexsubstr($num$t, 1 )) * pow($base$len - $t);  
  40.   }  
  41.   return $out;  
  42. }  
  43. ?>  

  得到縮短的網(wǎng)址以后,怎樣實(shí)現(xiàn)網(wǎng)址的轉(zhuǎn)發(fā)呢?可以利用 ttserver,將縮短網(wǎng)縮字串當(dāng)作key,真實(shí)的 url 地址當(dāng)作 value,存入ttserver中。ttserver本身就提供 http 訪問,只需要稍加修改就可以直接利用 ttserver 進(jìn)行縮短網(wǎng)址的轉(zhuǎn)發(fā):

  在 ttserver 源碼目錄下找到 ttserver.c 這個(gè)文件,這里我用的是 tokyotyrant-1.1.39 ,跳到第 2981 行,將下面的幾行改成圖中所示:

微博縮短網(wǎng)址的實(shí)現(xiàn)

  保存退出,編譯安裝 ttserver,網(wǎng)上有很多安裝教程,可以參考。

  啟動(dòng) ttserver,并向里面寫入一條 key 為 aaaaaa,value為 http://www.baidu.com 的值。

代碼
  1. curl -X PUT http://127.0.0.10:11221/aaaaaa -d "http://www.baidu.com"  

  主要目的是用 http 訪問 ttserver 時(shí)直接取得到真實(shí)的 url 并做轉(zhuǎn)發(fā)。這樣做很方便,但不安全,ttserver 的 http 還支持刪除、修改、插入數(shù)據(jù)(當(dāng)然也可以修改 ttserver 的 http 訪問入口,屏蔽掉這幾種操作)。負(fù)載均衡方面,可以通過添加多條 A 記錄隨機(jī)轉(zhuǎn)發(fā)到不同的 ttserver 機(jī)器上,但這樣每臺(tái)機(jī)器上存放的數(shù)據(jù)必須相同,網(wǎng)上也有說過ttserver  存過千萬左右的數(shù)據(jù)以后不太穩(wěn)定。

  利用 nginx 就能很好解決直接用 ttserver 的問題,用 nginx 過濾掉 http 訪問 ttserver 的刪除、修改、插入的操作,并為多臺(tái) ttserver 提供反向代理的功能。如下圖所示:

微博縮短網(wǎng)址的實(shí)現(xiàn)

  安裝 nginx,我這里采用的是 nginx-0.8.36.tar.gz。安裝 nginx 請(qǐng)參考:http://blog.s135.com/nginx_php_v6

  打開 nginx.conf 配置文件:

代碼
  1. #user  nobody;  
  2.   
  3. #啟動(dòng) 8 個(gè) nginx 進(jìn)程  
  4. worker_processes  8;  
  5.   
  6. #error_log  logs/error.log;  
  7. #error_log  logs/error.log  notice;  
  8. #error_log  logs/error.log  info;  
  9.   
  10. #pid        logs/nginx.pid;  
  11.   
  12. events {  
  13.     # 用 epoll,最大連接數(shù)  
  14.     use epoll;  
  15.     worker_connections 65535;  
  16. }  
  17.   
  18. http {  
  19.     include       mime.types;  
  20.     default_type  application/octet-stream;  
  21.   
  22.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  23.     #                  '$status $body_bytes_sent "$http_referer" '  
  24.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  25.   
  26.     #access_log  logs/access.log  main;  
  27.   
  28.     sendfile        on;  
  29.     #tcp_nopush     on;  
  30.   
  31.     # 由于只做轉(zhuǎn)發(fā),將超時(shí)時(shí)間設(shè)為 0  
  32.     keepalive_timeout  0;  
  33.   
  34.     #gzip  on;  
  35.     # 反向代理 ttserver 1 號(hào)機(jī),這里我放在一臺(tái)機(jī)器上開了三個(gè)不同端口  
  36.     upstream backend_1 {  
  37.         server 127.0.0.10:11221 weight=5 max_fails=3 fail_timeout=1s;  
  38.     }  
  39.     # 反向代理 ttserver 2 號(hào)機(jī)  
  40.     upstream backend_2 {  
  41.         server 127.0.0.10:11221 weight=5 max_fails=3 fail_timeout=1s;  
  42.     }  
  43.     # 反向代理 ttserver 3 號(hào)機(jī)  
  44.     upstream backend_3 {  
  45.         server 127.0.0.10:11221 weight=5 max_fails=3 fail_timeout=1s;  
  46.     }  
  47.   
  48.     server {  
  49.         listen       80;  
  50.         server_name  url.cn;  
  51.   
  52.         #charset koi8-r;  
  53.   
  54.         #access_log  logs/host.access.log  main;  
  55.         #當(dāng)路徑包含/count的時(shí)候,則代理到ttserver后端進(jìn)行請(qǐng)求數(shù)據(jù)。  
  56.     #請(qǐng)注意,這里屏蔽了PUT,DELETE,POST方法,只是使用了GET,主要目的是為了安全性,  
  57.     #因?yàn)?span id="0yi22am" class="func">DELETE,POST,PUT是可以修改數(shù)據(jù)的  
  58.     location ~* /count(.*) {  
  59.         if ($request_method = PUT ) {  
  60.             return 403;  
  61.         }  
  62.         if ($request_method = DELETE ) {  
  63.             return 403;  
  64.         }  
  65.         if ($request_method = POST ) {  
  66.             return 403;  
  67.         }  
  68.         proxy_method GET;  
  69.     }  
  70.         #將以 a-z 為第一個(gè)字符的 url 代理到 ttserver 1 號(hào)機(jī)  
  71.     location ~* "^/([a-z]{1})([a-zA-Z0-9]{5})" {  
  72.         proxy_pass http://backend_1;  
  73.     }  
  74.         #將以 A-Z 為第一個(gè)字符的 url 代理到 ttserver 2 號(hào)機(jī)  
  75.     location ~* "^/([A-Z]{1})([a-zA-Z0-9]{5})" {  
  76.         proxy_pass http://backend_2;  
  77.     }  
  78.         #將以 0-9 為第一個(gè)字符的 url 代理到 ttserver 3 號(hào)機(jī)  
  79.     location ~* "^/([0-9]{1})([a-zA-Z0-9]{5})" {  
  80.         proxy_pass http://backend_3;  
  81.     }  
  82.   
  83.         #error_page  404              /404.html;  
  84.   
  85.         # redirect server error pages to the static page /50x.html  
  86.         #  
  87.         error_page   500 502 503 504  /50x.html;  
  88.         location = /50x.html {  
  89.             root   html;  
  90.         }  
  91.     }  
  92. }  

  保存 nginx.conf 退出,現(xiàn)在就可以啟動(dòng) ttserver了,我這里做演示,為了方便就在一臺(tái)機(jī)器的三個(gè)端口啟動(dòng)了三個(gè) ttserver。如圖:

點(diǎn)擊查看大圖

  這里用 /ttserver/url_1 存放 ttserver 1號(hào)機(jī)的數(shù)據(jù),依此類推,分別在 11222、11223啟動(dòng) ttserver。

  接著啟動(dòng) nginx:

代碼
  1. ulimit -SHn 65535  
  2. /usr/local/nginx/sbin/nginx  

  接著在服務(wù)器上用下面的命令插入測試數(shù)據(jù):

代碼
  1. curl -X PUT http://127.0.0.10:11221/aaaaaa -d "http://www.baidu.com"  
  2. curl -X PUT http://127.0.0.10:11222/Aaaaaa -d "http://www.soso.com"  
  3. curl -X PUT http://127.0.0.10:11223/1aaaaa -d "http://www.qq.com"  

   配置你機(jī)器的 hosts 指向 nginx 服務(wù)器:

代碼
  1. 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ù)的取值操作也是很快的,在后面可以多開幾臺(tái) ttserver,分散大量訪問時(shí)的負(fù)載。

  前臺(tái)程序根據(jù)用戶提交的 url 生成短的 url 后,根據(jù)前面的 nginx 分發(fā)規(guī)則寫到某一臺(tái) ttserver 中,就可以了。nginx還支持一直 url hash 的均衡,但需要安裝一個(gè)第三方模塊ngx_http_upstream_hash_module,具體可以參考:http://blog.sina.com.cn/s/blog_5426e0180100dwsp.html

by xhttp.cn http://www.xhttp.cn/2010/07/22
原文:http://www.javaeye.com/topic/718695
 

贊助商鏈接:

主站蜘蛛池模板: 莱西市| 蒙自县| 长白| 抚宁县| 枣强县| 镇巴县| 永兴县| 崇明县| 大足县| 独山县| 台南县| 彭泽县| 江达县| 察哈| 东乡族自治县| 长岭县| 达尔| 永川市| 宜阳县| 温州市| 清苑县| 太康县| 黑河市| 富平县| 周口市| 海口市| 突泉县| 建水县| 习水县| 师宗县| 疏勒县| 夹江县| 夏河县| 昌邑市| 德庆县| 射阳县| 马公市| 蛟河市| 望谟县| 临邑县| 兴隆县|