千万级图片资源存储、裁剪方案

目标

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

方案

利用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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

//安装 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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

//安装 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、存储测试

1

2

3

4

5

//上传

/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 下

安装

1

2

3

4

/opt/server/php/bin/phpize

./configure --with-php-config=/opt/server/php/bin/php-config

make

make install

配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

[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配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

# 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

上传&获取测试

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<html>

<body>

 

<form action="upload.php?action=upload" method="post" enctype="multipart/form-data">

<label for="file">Filename:</label>

<input type="file" name="file" id="file" /><input type="submit" name="submit" value="Submit" />

</form>

 

</body>

</html>

 

<?php

 

    if('upload' == $_GET['action'])

    {

        //调用上传

        move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);

        $re = upload($_FILES["file"]["name"]);

        var_dump($re);

    }

 

    //文件上传

    function upload($fileName, $fileExtName = null, $metaList = array(), $groupName = null)

    {

        $tracker = fastdfs_tracker_get_connection();

        $storage = fastdfs_tracker_query_storage_store();

 

        return  fastdfs_storage_upload_by_filename($fileName, $fileExtName, $metaList, $groupName, $tracker, $storage);

    }

 

?>

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

安装

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

//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,控制图片处理生成需求图片。 下载地址

安装

1

2

3

./configure --prefix=/opt/server/lighttpd

make

make install

配置

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

1

2

3

4

5

6

7

# 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)

安装

1

2

3

4

5

6

7

8

9

10

11

12

13

14

//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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

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);

}

启动

1

/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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

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、切割好的图片立即被缓存,提高图片访问速度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值