1.思路
1. 一般使用fastdfs分布式文件系统来处理(不支持window),因为部署的环境是window,所以这里对fastdfs不过多的阐述。
2.实现集群环境图片上传,我是通过nginx+ftp实现的。ftp服务器上传图片到固定位置。nginx负责回显数据。----下面采用的是这种方案。
3. 两张表,第一个表是服务器配置表(记录服务器的ip和端口) ,第二个表示记录服务器下对应的图片信息。假设有3个服务器。
使用这种方案,理论情况下,图片会分割成3份,每台服务器的图片一样多,实际上不可能一样多,这里就需要在数据库做一个标识,后台再查下数据库的图片数量,通过一定的算法,从而保证图片几乎一样多。
2.ftp代码
https://blog.csdn.net/qq_16855077/article/details/83862161
3.nginx
3.1nginx下载
官网地址:https://nginx.org/en/download.html
因部署系统为window,所以我下载的是包含windows的zip
把下载好的zip解压
我看网上很多的人都是直接双击nginx,但是出现的相关都是一闪而过,用过tomcat的朋友,都知道一闪而过实际上报错,我最初也是以为报错,但是实际运行时没有问题的。
建议通过cmd命令行的方式访问
开始命令:start nginx
停止命令:nginx -s stop
重新加载配置 nginx -s reload 一般改动nginx配置后不用充钱nginx,重新加载配置就可以
网站访问地址:http://localhost
nginx安装成功效果图如下:
3.2 通过nginx会显图片
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8089;
server_name localhost;
#charset utf-8;
#access_log logs/host.access.log main;
#添加博客的代码
location ~ .*\.(gif|jpg|jpeg|png)$ {
expires 24h;
root D:\apache-tomcat-8.5.32/;#指定图片存放路径
access_log D:\apache-tomcat-8.5.32;#图片路径
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path D:\apache-tomcat-8.5.32/;#图片路径
proxy_redirect off;
proxy_set_header Host 127.0.0.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 1280k;
proxy_connect_timeout 900;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 40k;
proxy_buffers 40 320k;
proxy_busy_buffers_size 640k;
proxy_temp_file_write_size 640k;
if ( !-e $request_filename)
{
proxy_pass http://192.168.1.155:8089; #代理访问地址
}
}
location / {
root html;
index index.html index.htm;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
window版本:
上面这两个地址发到linux需要屏蔽掉,不然会修改图片的所属组
linux版本:
存放路径
重新加载nginx配置,访问http://localhost:8089/1.jpg
这样就可以实现tomcat集群图片上传勒,不管请求通过是有A、B、C服务器发过来的,假设ftp服务器在A,我们只需要把图片保存到A服务器上就行,在A服务器上搭建nginx和ftp服务器,如果担心A服务器挂掉,导致图片服务器不可用的朋友,可以研究下ftp服务器如何搭建。