libevent搭建伺服器
⑴ 如何基於fastdfs搭建縮略圖伺服器
目前FastDFS支持類Unix系統 ,在Linux和FreeBSD下測試過,本文使用的是Ubuntu10.4,建議安裝之前先簡單了解一下FastDFS的原理 ,這對後繼的配置部分會有很好的理解。
一、准備工作-安裝libevent
FastDFS 內部綁定了 libevent 作為 http 伺服器 ,在V2.X版本必須安裝 libevent ,本文安裝的是V2.2版本,因此必須安裝libevent。(官方也推薦使用 http 方式下載 文件 )
如果已經安裝了 libevent,請確認安裝路徑是 /usr , 因為 FastDFS 在編譯源程序時,需要到此目錄下查找一些依賴文件,否則編譯 FastDFS 會出錯 。如果不是,建議首先卸載 libevent ,然後安裝到 /usr 下。Ubuntu10.4默認是安裝了libevent,可以到軟體中心卸載掉然後按照下面介紹的方式安裝。
安裝步驟:
解壓 libevent ,然後進入解壓後的目錄分別執行:
Shell代碼
./configure --prefix=/usr
make clean;
make
make install
安裝libevent完成。
二、安裝 FastDFS
1、下載 FastDFS 源程序
2、在 FastDFS_v4.05.tar.gz 所在文件夾下執行:
Shell代碼
sudo tar vxzf FastDFS_v4.05.tar.gz /home/soar/FastDFS
註: /home/soar/FastDFS 也可以是其他目錄,記住這是解壓目錄,以後用 %FastDFS% 表示)
3、由於要使用內置的 http 服務,因此編輯 %FastDFS%/make.sh 文件,找到
#WITH_HTTPD=1
修改成
WITH_HTTPD=1
以支持 http
4、進入 %FastDFS% 目錄,執行:
Shell代碼
sudo ./make.sh
5、在 %FastDFS% 目錄下,執行:
Shell代碼
sudo ./make.sh install
如果運行如上命令後,在命令行信息最後看到類似
Shell代碼
#ln -fs /usr/local/lib/libfastcommon.so.1 /usr/local/lib/libfastcommon.so
#ln -fs /usr/local/lib/libfdfsclient.so.1 /usr/local/lib/libfdfsclient.so
sh ./fdfs_link_library.sh
恭喜你,已經安裝成功了!
FastDFS的配置文件在%FastDFS%/conf目錄下,其中包括
Client.conf 客戶端上傳配置文件
Storage.conf 文件存儲伺服器配置文件
Tracker.conf 負責均衡調度伺服器配置文件
http.conf http伺服器配置文件
配置文件需要根據伺服器環境的不同,進行不同的配置,具體可以參考FastDFS官方的安裝配置文檔http://code.google.com/p/fastdfs/wiki/Setup,以及FastDFS原理介紹文檔http://code.google.com/p/fastdfs/wiki/Overview
在本例中,Tracker Server,Storage Server都安裝在一台伺服器上,IP:10.0.2.15
1、配置及啟動Tracker Server
A、修改%FastDFS%/conf/tracker.conf文件,修改如下
#可以自己指定目錄位置,但目錄必須存在,用於存儲日誌及storage server等信息,否則tracker server無法啟動
Txt代碼
base_path=/home/yuqing/fastdfs -> base_path=/home/soar/fastdfs_tracker
#改成你想要的http埠,將來http下載文件的埠就是他了
Txt代碼
http.server_port=8080 -> http.server_port=8090
#http支持
Txt代碼
##include http.conf -> #include http.conf
#默認4GB,如果空間不足,建議調小,否則會報no free space的異常,無法正常啟動
Txt代碼
reserved_storage_space = 4GB -> reserved_storage_space = 1GB
#tracker server對storage server供服務的埠,使用默認的即可,也可以自定義
Txt代碼
port=22122
B、將http.conf文件拷貝到/etc/fdfs目錄下,執行
Shell代碼
sudo cp %FastDFS%/conf/http.conf /etc/fdfs/
註:為了支持http,必須將這個文件拷貝到此目錄,否則無法啟動,報param http.XXX not exist or is empty類似的錯誤
C、進入/usr/local/bin/目錄,啟動tracker伺服器,執行
Shell代碼
sudo fdfs_trackerd %FastDFS%/conf/tracker.conf
D、進入/home/soar/fastdfs_tracker/logs/trackerd.log查看tracker的啟動日誌,如果看到類似
Shell代碼
[2010-11-04 16:21:25] INFO - FastDFS v2.02, base_path=/home/soar/fastdfs_tracker, connect_timeout=30s, network_timeout=60s, port=22122, bind_addr=, max_connections=256, work_threads=4, store_lookup=2, store_group=, store_server=0, store_path=0, reserved_storage_space=4MB, download_server=0, allow_ip_count=-1, sync_log_buff_interval=10s, check_active_interval=120s, thread_stack_size=64 KB, storage_ip_changed_auto_adjust=1, storage_sync_file_max_delay=86400s, storage_sync_file_max_time=300s
[2010-11-04 16:21:25] INFO - HTTP supported: server_port=8090, default_content_type=application/octet-stream, anti_steal_token=0, token_ttl=0s, anti_steal_secret_key length=0, token_check_fail content_type=, token_check_fail buff length=0, check_active_interval=30, check_active_type=tcp, check_active_uri=/status.html
恭喜你,tracker server已經啟動成功!
2、配置及啟動Storage Server
A、修改%FastDFS%/conf/storage.conf文件,修改如下:
#可以自定義,但必須存在此目錄,用於存儲storage相關的log、group內的相關信息
Txt代碼
base_path=/home/yuqing/fastdfs -> /home/soar/fastdfs_storge
#文件的存儲位置,在一台storage server上可以指定多個存儲位置
Txt代碼
store_path0=/home/yuqing/fastdfs -> store_path0=/home/soar/fastdfs_storge
#必須指定
Txt代碼
group_name=group1
#修改成tracker server的IP和埠信息
Txt代碼
tracker_server=192.168.209.121:22122 -> tracker_server=10.0.2.15:22122
#http支持
Txt代碼
##include http.conf ->#include http.conf
B、進入/usr/local/bin/目錄,啟動storage伺服器,執行
Shell代碼
sudo fdfs_storaged %FastDFS%/conf/storage.conf
C、進入/home/soar/fastdfs_tracker/logs/storage.log查看storage伺服器啟動日誌,如果看到類似
Shell代碼
[2010-11-04 16:37:16] INFO - FastDFS v2.02, base_path=/home/soar/fastdfs_storge, store_path_count=1, subdir_count_per_path=256, group_name=group1, connect_timeout=30s, network_timeout=60s, port=23000, bind_addr=, client_bind=1, max_connections=256, work_threads=4, disk_rw_separated=1, disk_reader_threads=1, disk_writer_threads=1, buff_size=256KB, heart_beat_interval=30s, stat_report_interval=60s, tracker_server_count=1, sync_wait_msec=200ms, sync_interval=0ms, sync_start_time=00:00, sync_end_time=23:59, write_mark_file_freq=500, allow_ip_count=-1, file_distribute_path_mode=0, file_distribute_rotate_count=100, fsync_after_written_bytes=0, sync_log_buff_interval=10s, sync_binlog_buff_interval=60s, sync_stat_file_interval=300s, thread_stack_size=512 KB, upload_priority=10, if_alias_prefix=, check_file_plicate=0, FDHT group count=0, FDHT server count=0, FDHT key_namespace=, FDHT keep_alive=0, HTTP server port=8888, domain name=
[2010-11-04 16:37:16] INFO - HTTP supported: server_port=8888, http_trunk_size=262144, default_content_type=application/octet-stream, anti_steal_token=0, token_ttl=0s, anti_steal_secret_key length=0, token_check_fail content_type=, token_check_fail buff length=0
[2010-11-04 16:37:16] INFO - file: storage_param_getter.c, line: 48, storage_ip_changed_auto_adjust=1
[2010-11-04 16:37:19] INFO - file: tracker_client_thread.c, line: 274, successfully connect to tracker server 10.0.2.15:22122, as a tracker client, my ip is 10.0.2.15
恭喜你,storage server已經啟動成功!
經過前面的安裝、配置、啟動,馬上就可以看到FastDFS的效果了,我們先上傳一個文件,然後再通過http下載。
一、上傳文件
FastDFS安裝包中,自帶了客戶端程序,通過程序可以進行文件上傳。在使用這個客戶端程序之前,首先需要配置client.conf,然後再進行文件上傳及下載。
1、修改%FastDFS%/conf/client.conf文件,修改如下:
#可自定義,但此目錄必須存在,用於存放文件上傳log
Txt代碼
base_path=/home/yuqing/fastdfs-> base_path=/home/soar/fastdfs_tracker
Txt代碼
tracker_server=192.168.209.121:22122 -> tracker_server=10.0.2.15:22122
Txt代碼
http.tracker_server_port=8080 ->http.tracker_server_port=8090
#支持http
Txt代碼
##include http.conf ->#include http.conf
2、進入/usr/local/bin/目錄,上傳文件,執行
Txt代碼
sudo fdfs_test %FastDFS%/conf/client.conf upload a.txt
註:a.txt可以在/usr/local/bin/目錄下自己創建一個
如果命令行反饋類似如下:
則說明上傳文件成功。
關於fastdfs集群和備份配置
1、修改tracker.conf文件中store_lookup=2 平衡負載
2、修改tracker.conf文件中store_server=0 採用輪詢方式。
如果希望只使用一台文件存儲伺服器,其他二台做備份,則修改文件tracker_server為某台主機的IP,只啟動文件存儲伺服器的tracker,其他二台備份伺服器不需要啟動tracker。
如果希望三台伺服器集群使用,互相備份,比如:使用A機器存儲文件,這時候A機器會將文件同步到B機器和C機器,同理使用B機器存儲文件,則會同時將文件同步到A機器和C機器。這時候需要將所有的機器IP和埠號
都配置在storage.conf中,如下所示:
tracker_server=10.207.16.127:22122
tracker_server=10.207.16.129:22122
1.安裝centos VMware9下面安裝centos2.在centos下面設置共享文件夾為你本地的論壇的代碼,然後設置網路為橋接:直接連接到物理網路,賦值網路連接狀態3.進入forum_svr.py目錄下運行python forum_svr.py,當然是啟動不了服務的4.安裝easy_install(想辦法)5.安裝pymogo tornado memcache 等一系列的模塊。6.當然還是啟動不了的7.還需要啟動memcached ,所以就要安裝memcached,Memcached是由Danga Interactive開發的,高性能的,分布式的內存對象緩存系統,用於在動態應用中減少資料庫負載,提升訪問速度。所以你就要安裝libevent8.libevent 最新的穩定版:wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
# rpm -q libevent (首先檢查系統是否安裝了Libevent)
# yum -y install libevent* (我使用的Yum安裝)或者可以使用編譯安裝
# tar zxvf libevent-1.4.14b-stable.tar.gz
# cd libevent-1.4.14b-stable
# ./configure --prefix=/usr/local/libevent/ # make
# make install
9.memcached 最新的穩定版:wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz # tar -zxvf memcached-1.4.5.tar.gz
# cd memcached-1.4.5 # ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/ # make
# make install
啟動Memcached
# cd /usr/local/memcached/bin/ # ./memcached -u root -d
將Memcached加入系統自動啟動
# vim /etc/rc.local
# /usr/local/memcached/bin/memcached -u root -d
查看Memcached
# ps -ef |grep memcached
10.然後你就要找到虛擬機的IP地址,可以在虛擬機下找運行ifconfig 例如:inet addr:192.168.0.109
11.在course_form_edit.html和course_forum_viem.html下修改swf:地址:(我的)當然是http://192.168.0.109:8086/了。(共六處)12.現在從虛擬機下啟動forum_svr.py就能啟動了。13.可能在windows下你訪問時可能還是會報伺服器拒絕的錯誤,說明虛擬機的鏈接地址還是請求不到虛擬機下的數據,可以在虛擬機下運行
iptables -I INPUT -i eth0 -p tcp --dport 8086 -j ACCEPT(為windows下實體機開辟一個8086埠),虛擬機防火牆不讓請求數據。
OK,現在windows下就可以請求到數據了。方便本地的調試了。
⑶ 使用docker搭建STUN/TURN伺服器
前言 STUN,首先在RFC3489中定義,作為一個完整的NAT穿透解決方案,英文全稱是Simple Traversal of UDP Through NATs,即簡單的用UDP穿透NAT。 TURN,首先在RFC5766中定義,英文全稱是Traversal Using Relays around NAT:Relay Extensions to Session Traversal Utilities for NAT,即使用中繼穿透NAT:STUN的擴展 簡單的說,TURN與STURN的共同點都是通過修改應用層中的私網地址達到NAT穿透的效果,異同點是TURN是通過兩方通訊的「中間人」方式實現穿透。 ICE的全稱Interactive Connectivity Establ.shment(互動式連接建立),由IETF的MMUSIC工作組開發出來的,它所提供的是一種框架,使各種NAT穿透技術可以實現統一。 STUN和TURN伺服器和ICE可以參考閱讀: P2P技術詳解(三):P2P技術之STUN、TURN、ICE詳解 本文介紹如何通過DOCKER搭建STUN和TURN伺服器,步驟如下 1:創建Dockerfile,內容如下: FROM ubuntu:14.04 MAINTAINER Patxi Gortázar <[email protected]> RUN apt-get update && apt-get install -y \ curl \ libevent-core-2.0-5 \ libevent-extra-2.0-5 \ libevent-openssl-2.0-5 \ libevent-pthreads-2.0-5 \ libhiredis0.10 \ libmysqlclient18 \ libpq5 \ telnet \ wget RUN wget http://turnserver.open-sys.org/downloads/v4.4.2.2/turnserver-4.4.2.2-debian-wheezy-ubuntu-mint-x86-64bits.tar.gz \ && tar xzf turnserver-4.4.2.2-debian-wheezy-ubuntu-mint-x86-64bits.tar.gz \ && dpkg -i coturn_4.4.2.2-1_amd64.deb COPY ./turnserver.sh /turnserver.sh ENV TURN_USERNAME test ENV TURN_PASSWORD test ENV REALM kurento.org ENV NAT true EXPOSE 3478 3478/udp ENTRYPOINT ["/turnserver.sh"] 2:創建turnserver.sh,內容如下 #!/bin/bash set-e if[$NAT="true"-a-z"$EXTERNAL_IP"];then #Try to get public IP PUBLIC_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)||echo"No public ip found on http://169.254.169.254/latest/meta-data/public-ipv4" if[-z"$PUBLIC_IP"];then PUBLIC_IP=$(curl http://icanhazip.com)||exit1 fi #Try to get private IP PRIVATE_IP=$(ifconfig|awk'/inet addr/{print substr($2,6)}'|grep -v 127.0.0.1)||exit1 exportEXTERNAL_IP="$PUBLIC_IP/$PRIVATE_IP" echo"Starting turn server with external IP:$EXTERNAL_IP" fi echo'min-port=49152'>/etc/turnserver.conf echo'max-port=65535'>>/etc/turnserver.conf echo'fingerprint'>>/etc/turnserver.conf echo'lt-cred-mech'>>/etc/turnserver.conf echo"realm=$REALM">>/etc/turnserver.conf echo'log-file stdout'>>/etc/turnserver.conf echo"user=$TURN_USERNAME:$TURN_PASSWORD">>/etc/turnserver.conf [$NAT="true"]&&echo"external-ip=$EXTERNAL_IP">>/etc/turnserver.conf exec/usr/bin/turnserver"$@" 3:使用docker build 創建鏡像,執行結果如下 [root@www]# docker build -t teststurn_1 . Sending build context to Docker daemon 4.096kB Step 1/11 : FROM ubuntu:14.04 ---> 6e4f1fe62ff1 Step 2/11 : MAINTAINER Patxi Gortázar <[email protected]> ---> Using cache ---> 4460f9f84053 Step 3/11 : RUN apt-get update && apt-get install -y curl libevent-core-2.0-5 libevent-extra-2.0-5 libevent-openssl-2.0-5 libevent-pthreads-2.0-5 libhiredis0.10 libmysqlclient18 libpq5 telnet wget ---> Using cache ---> 05ed9ced48a5 Step 4/11 : RUN wget http://turnserver.open-sys.org/downloads/v4.4.2.2/turnserver-4.4.2.2-debian-wheezy-ubuntu-mint-x86-64bits.tar.gz && tar xzf turnserver-4.4.2.2-debian-wheezy-ubuntu-mint-x86-64bits.tar.gz && dpkg -i coturn_4.4.2.2-1_amd64.deb ---> Using cache ---> d82ed28fdac9 Step 5/11 : COPY ./turnserver.sh /turnserver.sh ---> Using cache ---> 1d37a488282c Step 6/11 : ENV TURN_USERNAME test ---> Running in bfd88f08db42 Removing intermediate container bfd88f08db42 ---> cf8af0504b95 Step 7/11 : ENV TURN_PASSWORD test ---> Running in b8ef33b7c213 Removing intermediate container b8ef33b7c213 ---> 32a832f23169 Step 8/11 : ENV REALM kurento.org ---> Running in bbe129edf5b3 Removing intermediate container bbe129edf5b3 ---> 21fdfe34689b Step 9/11 : ENV NAT true ---> Running in 5bdfe8555d5e Removing intermediate container 5bdfe8555d5e ---> dc7fc896841c Step 10/11 : EXPOSE 3478 3478/udp ---> Running in 67aaa1966f68 Removing intermediate container 67aaa1966f68 ---> a12646ed45ff Step 11/11 : ENTRYPOINT ["/turnserver.sh"] ---> Running in b8fc2ff09265 Removing intermediate container b8fc2ff09265 ---> f5e5acad0f81 Successfully built f5e5acad0f81 Successfully tagged teststurn_1:latest 執行完後可以看到自己創建的鏡像名稱為teststurn_1 4:啟動docker的鏡像,並開啟埠3478 docker run -d -p 3478:3478 -p 3478:3478/udp teststurn_1 啟動後需要等待一兩分鍾才能測試順暢 5:測試伺服器效果 打開 https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/ 並輸入自己的本機IP和埠,分別測試兩種協議服務是否生效