一.FastDFS
1.什么是FastDFS
FastDFS:Fast Distributed File System,快速的分布式文件系统,是一款用c语言开发的开源的分布式文件系统.
2.FastDFS的特点
- 为互联网量身定制
- 冗余备份
- 负载均衡
- 线性扩容
- 高可用
- 高性能
3.FastDFS使用场景
- 高性能的文件服务器集群
- 提供文件上传服务
- 提供文件下载服务
5.存储文件file_id的组成格式(通过Nginx代理实现)
格式:组名/虚拟的磁盘路径/文件的两级目录/文件名
例子:/group1/M00/00/00/wKg6gGNwcFqAYweVADpk741bgPA332.JPG
注释:Linux中的nginx配置文件nginx.conf如下,通过nginx实现代理过程
#user nobody;
worker_processes 1;
user root;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/nginx/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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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;
#}
}
server {
listen 80;
server_name 192.168.58.128;
#charset koi8-r;
#access_log logs/host.access.log main;
location /group1/M00/ {
root /home/fastdfs/storage/data;
ngx_fastdfs_module;
}
}
# 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;
# }
#}
}
nginx所在目录
二.Dubbo的简易介绍
1.Dubbo是什么?
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东西,说白了就是个远程服务调用的分布式框架(告别Web Service模式中的WSdl,以服务者与消费者的方式在dubbo上注册)。
其核心部分包含:
- 远程通讯:
提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
- 集群容错:
提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
- 自动发现
基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
二.Dubbo能做什么?
1.透明化的远程方法调用
就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
2.软负载均衡及容错机制
可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
3. 服务自动注册与发现
不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
Dubbo采用全spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
三.FastDFS文件系统实现,和环境搭建
注意:我这里的fastdfs文件系统,zookeeper是基于docker容器实现的,需安装docker管理容器,然后在容器下拉取对应的环境镜像安装即可。
1.安装FastDFS的步骤
1.1安装FastDFS的依赖的工具包
1.1.1.libevent
- yum -y install libevent
1.2.libfastcommonV1.0.7(貌似是不需要这一步也可以)
下载
上传linux服务器放入/tmp下
解压 tar -xvf libfastcommon-1.0.7.tar.gz
编译
- yum -y install zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl unzip net-tools wget
- cd libfastcommon-1.0.7
- ./make.sh
- ./make.sh install
- cd /usr/lib64
- cp libfastcommon.so …/lib
- cd …/lib
- ll libfastcommon.so
1.2安装FastDFS
-
下载
-
上传linux服务器放入/tmp下
-
解压:
- cd /tmp/
- tar -xvf fastdfs-5.05.tar.gz
-
编译
- cd fastdfs-5.05
- ./make.sh
-
安装
- ./make.sh install
-
验证
-
cd /usr/bin/
-
ll fdfs*(查看含有fdfs关键字的文件)
-rwxr-xr-x. 1 root root 321896 12月 1 10:34 fdfs_appender_test -rwxr-xr-x. 1 root root 321672 12月 1 10:34 fdfs_appender_test1 -rwxr-xr-x. 1 root root 308552 12月 1 10:34 fdfs_append_file -rwxr-xr-x. 1 root root 307984 12月 1 10:34 fdfs_crc32 -rwxr-xr-x. 1 root root 308584 12月 1 10:34 fdfs_delete_file -rwxr-xr-x. 1 root root 309344 12月 1 10:34 fdfs_download_file -rwxr-xr-x. 1 root root 308928 12月 1 10:34 fdfs_file_info -rwxr-xr-x. 1 root root 322752 12月 1 10:34 fdfs_monitor -rwxr-xr-x. 1 root root 1125664 12月 1 10:34 fdfs_storaged -rwxr-xr-x. 1 root root 331840 12月 1 10:34 fdfs_test -rwxr-xr-x. 1 root root 326960 12月 1 10:34 fdfs_test1 -rwxr-xr-x. 1 root root 465464 12月 1 10:34 fdfs_trackerd -rwxr-xr-x. 1 root root 309536 12月 1 10:34 fdfs_upload_appender -rwxr-xr-x. 1 root root 310560 12月 1 10:34 fdfs_upload_file
-
-
拷贝源码包的配置文件到/etc/fdfs下
- cd /tmp/fastdfs-5.05/conf
- cp * /etc/fdfs/
- cd /etc/fdfs/
- ll
- 配置Tracker服务
- 创建文件夹 mkdir -p /home/fastdfs/tracker
- 修改配置文件/etc/fdfs/tracker.conf 22行修改:base_path=/home/fastdfs/tracker
- 启动Tracker
- /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
- 查看tracker进程:ps aux|grep tracker
2.安装storage
- 创建文件夹:mkdir -p /home/fastdfs/storage
- 配置storage服务/etc/fdfs/storage.conf
- 41行:base_path=/home/fastdfs/storage
- 109行:store_path0=/home/fastdfs/storage
- 118行:tracker_server=192.168.204.128:22122
- 启动storage:
- /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
- 查看进程: ps aux|grep storage
3.测试服务
- 创建文件夹: mkdir -p /home/fastdfs/client
- 修改配置文件/etc/fdfs/client.conf
- 10行: base_path=/home/fastdfs/client
- 14行: tracker_server=192.168.58.128:22122
我自己fdfs下的client.fdfs配置如下
# 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= /home/fastdfs/client
# 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.58.128:22122
#standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info
# if use connection pool
# default value is false
# since V4.05
use_connection_pool = false
# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600
# if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false
# if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V4.05
use_storage_id = false
# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V4.05
storage_ids_filename = storage_ids.conf
#HTTP settings
http.tracker_server_port=80
#use "#include" directive to include HTTP other settiongs
##include http.conf
- 进入文件夹:
- cd /etc/fdfs/
- ls
- 使用命令上传文件到文件服务器
- /usr/bin/fdfs_test /etc/fdfs/client.conf upload anti-steal.jpg
4.搭建FastDFS的Nginx插件的http模块提供http服务-----nginx
-
下载
-
上传到linux服务器/tmp
-
解压: tar -xvf fastdfs-nginx-module_v1.16.tar.gz
-
修改配置文件: /tmp/fastdfs-nginx-module/src/config 将local去掉
-
对Nginx重新编译
-
mkdir /var/temp/nginx -p
-
cd nginx-1.8.0
-
./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi --add-module=/tmp/fastdfs-nginx-module/src
-
cd /usr/lib64
-
cp libfdfsclient.so …/lib
-
对niginx进行编译
- cd /tmp/nginx-1.8.0
- make
- make install
-
cp /tmp/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
-
修改/etc/fdfs/mod_fastdfs.conf
- 40行: tracker_server=192.168.204.128:22122
- 53行: url_have_group_name = true
- 62行: store_path0=/home/fastdfs/storage
-
配置nginx
-
/usr/local/nginx/conf/nginx.conf
- 添加一个server
-
-
server {
listen 80;
server_name 192.168.58.128;
#charset koi8-r;
#access_log logs/host.access.log main;
location /group1/M00/ {
#不加这一行,则访问文件时访问不到,会报错
root /home/fastdfs/storage/data;
ngx_fastdfs_module;
}
}
-
启动nginx
- /usr/local/nginx/sbin/nginx
- ps aux|grep nginx
-
浏览器输入网址:
在这里插入图片描述
http://192.168.58.128/group1/M00/00/00/wKg6gGNwcEiAYhGrAHTAkzQ5Mvg931.JPG
四.springboot+dubbo+FastDFS代码实现
我的项目结构,仅罗列provider服务提供者和consumer服务消费者(domain,service,mapper均为maven工程,可在provider,consumer中导入即可)
1.providor服务提供者
简易结构:
1.1.maven依赖文件
<?xml version="