首页 > Linux > 搭建千万级图片资源存储服务

搭建千万级图片资源存储服务

2012年10月23日 发表评论 阅读评论

目标

搭建千万级图片资源存储服务,方便快捷切割所需高宽以及精度图片。

方案

利用fastdfs做文件分布式存储;

lighttpd结合imagemagic扩展开发,支持通过HTTP请求传入高宽以及精度截取需要的图片;

外加varnish文件缓存。

即,

如原图为:http://xxx/group1/M00/00/00/1.jpg

通过链接 http://xxx/group1/M00/00/00/1_500_400_30.jpg

即可获取高为400,宽为400,精度为30%的图片;

用户请求时,临时生成,且立即存入缓存和物理存储,以后直接从缓存中获取。

1、两台服务器做存储分发(tracker 192.168.20.28,192.168.20.27)
//安装 libevent

./configure –prefix=/usr
make
make install

//安装 fastdfs

//修改 make.sh 支持内置httpd

#WITH_HTTPD=1 => WITH_HTTPD=1

//安装目录

TARGET_PREFIX=/opt/server/fastdfs
TARGET_CONF_PATH=$TARGET_PREFIX/etc

./make.sh
./make.sh install

//配置 fastdfs tracker

//使用内置server

http.disabled=false

//端口

http.server_port=8080

//启动

/opt/server/fastdfs/bin/fdfs_trackerd /opt/server/fastdfs/etc/tracker.conf

2、两台服务器做物理存储(storage 192.168.20.24,192.168.20.25)
//安装 libevent

./configure –prefix=/usr
make
make install

//安装 fastdfs

//修改 make.sh 支持内置httpd

#WITH_HTTPD=1 => WITH_HTTPD=1

//安装目录

TARGET_PREFIX=/opt/server/fastdfs
TARGET_CONF_PATH=$TARGET_PREFIX/etc

./make.sh
./make.sh install

//配置 fastdfs storage

//使用内置server

http.disabled=false

//端口

http.server_port=8099

//设置 tracker

tracker_server=192.168.20.28:22122
tracker_server=192.168.20.27:22122

//启动

/opt/server/fastdfs/bin/fdfs_storaged /opt/server/fastdfs/etc/storage.conf

3、存储测试
//上传
/opt/server/fastdfs/bin/fdfs_test /opt/server/fastdfs/etc/client.conf upload 3.jpg

//获取
wget http://192.168.20.28:8080/group1/M00/00/00/wKgUGE_yR6bou3MSAACABLw5CM4429_big.jpg

4、PHP扩展 (需先安装fastdfs服务)(安装在web服务器192.168.20.231,192.168.20.232)
编译源码 /php_client 下

安装

/opt/server/php/bin/phpize
./configure –with-php-config=/opt/server/php/bin/php-config
make
make install
配置

[fastdfs]

extension = fastdfs_client.so

; the base path
fastdfs_client.base_path = /tmp

; connect timeout in seconds
; default value is 30s
fastdfs_client.connect_timeout = 30

; network timeout in seconds
; default value is 30s
fastdfs_client.network_timeout = 60

; standard log level as syslog, case insensitive, value list:
;;; emerg for emergency
;;; alert
;;; crit for critical
;;; error
;;; warn for warning
;;; notice
;;; info
;;; debug
fastdfs_client.log_level = info

; set the log filename, such as /usr/local/fastdfs/logs/fastdfs_client.log
; empty for output to stderr
fastdfs_client.log_filename = /opt/server/fastdfs/logs/fastdfs_client.log

; secret key to generate anti-steal token
; this parameter must be set when http.anti_steal.check_token set to true
; the length of the secret key should not exceed 128 bytes
fastdfs_client.http.anti_steal_secret_key =

; FastDFS cluster count, default value is 1
fastdfs_client.tracker_group_count = 1

; config file of FastDFS cluster ;, based 0
; must include absolute path, such as fastdfs_client.tracker_group0
; the config file is same as conf/client.conf
fastdfs_client.tracker_group0 = /opt/server/fastdfs/etc/client.conf
其中/opt/server/fastdfs/etc/client.conf配置

# connect timeout in seconds
# default value is 30s
connect_timeout=30

# network timeout in seconds
# default value is 30s
network_timeout=60

# the base path to store log files
base_path=/opt/server/fastdfs

# tracker_server can ocur more than once, and tracker_server format is
# “host:port”, host can be hostname or ip address
tracker_server=192.168.20.28:22122
tracker_server=192.168.20.27:22122

log_level=info

#HTTP settings
http.tracker_server_port=8080

#use “#include” directive to include HTTP other settiongs
#include http.conf
上传&获取测试

5、图片处理imagemagic(192.168.20.28,192.168.20.27)
安装

//libpng-1.2.10.tar.bz2

./configure –prefix=/usr
make
make install
ldconfig

//jpegsrc.v7.tar.gz

./configure –prefix=/usr
make
make install
ldconfig

//freetype-2.4.6.tar.bz2

./configure –prefix=/usr
make
make install
ldconfig

//imagemagic

./configure –prefix=/opt/server/imagemagick
make
make instll

测试

convert logo: logo.gif
6、图片处理调度WEB服务(192.168.20.28,192.168.20.27)

定制化开发的lighttpd,调用特定URL,控制图片处理生成需求图片。 下载地址

安装

./configure –prefix=/opt/server/lighttpd
make
make install
配置

vi /opt/server/lighttpd/conf/lighttpd.conf

# FastDFS Configuration

fastdfs.conf = “/opt/server/fastdfs/etc/client.conf”
fastdfs.convert-enable = “enable”
fastdfs.convert-filesize = ( “*”, “_100_100″ )
fastdfs.convert-filetypes = ( “.gif”, “.jpg”, “.jpeg”, “.png” )
server.port = 81
启动

1
/opt/server/lighttpd/sbin/lighttpd -f /opt/server/lighttpd/conf/lighttpd.conf
裁剪图片测试

浏览器访问:

1

http://192.168.20.28/group1/M00/00/00/wKgUGE_yrsPEiKq-AARlTan74rg752_500_500_30.jpg

其中500_500 长宽,30为精度

7、图片获取varnish缓存 (192.168.20.28,192.168.20.27)

安装

//pcre-8.30

./configure –prefix=/usr
make
make install

ldconfig

//varnish-3.0.2

export PKG_CONFIG_PATH=/usr/lib/pkgconfig
./configure –prefix=/opt/server/varnish
make
make install
配置

指向后端图片处理lighttpd.

backend rs
{
.host = “192.168.20.28″;
.port = “81″;
}

acl purge
{
“localhost”;
“127.0.0.1″;
“192.168.20.0″/24;
}

sub vcl_recv
{

if (req.http.host ~”^rs.mo.dev$”)
{
set req.backend = rs;
}

if (req.request == “PURGE”)
{
if (!client.ip~purge)
{
error 405 “Not allowed.”;
return (lookup);
}
}

}

sub vcl_hit
{
if (req.request == “PURGE”)
{
set obj.ttl = 0s;
error 200 “Purged.”;
}
}

sub vcl_miss
{
if (req.request == “PURGE”)
{
error 404 “Not in cache.”;
}

}

sub vcl_fetch
{
set req.grace = 30s;
if (req.request == “GET” && req.url ~ “\.(css|js)$”)
{
set beresp.ttl = 3600s;
}
else
{
set beresp.ttl = 30d;
}
return (deliver);
}
sub vcl_deliver
{
return (deliver);
}
启动

/opt/server/varnish/sbin/varnishd -f /opt/server/varnish/etc/varnish/my.vcl -n /opt/server/varnish/var/cache -a 0.0.0.0:80 -T 0.0.0.0:3500 -u www -s malloc,10G -w 2,500,300
8、最前端配置资源获取服务器nginx (安装在web服务器192.168.20.231,192.168.20.232)

upstream rs

{

server 192.168.20.28:80;
server 192.168.20.27:80;

}

server {

listen 80;

server_name rs.mo.dev;

location / {

proxy_pass http://rs/;

proxy_hide_header Content-Type;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

log_format rs.mo.dev ‘$remote_addr – $remote_user [$time_local] “$request” ‘

‘$status $body_bytes_sent “$http_referer” ‘

‘”$http_user_agent” $http_x_forwarded_for’;

access_log /opt/server/nginx/logs/rs.log rs.mo.dev;

}
通过浏览器访问

1

http://rs.mo.dev/group1/M00/00/00/wKgUGE_yrsPEiKq-AARlTan74rg752_500_500_30.jpg

9、总结

通过6台服务器,配置的图片服务,具有以下特点:

1、具备基本的图片以及其他资源文件上传下载

2、避免单点故障,不会因为某台tracker或者storage挂掉,而服务终止

3、根据传入参数,实时切割所需图片,参数支持长、宽、以及精度

4、切割好的图片立即被缓存,提高图片访问速度

分类: Linux 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.