1. Install FastDFS on the cloud server
1.2 Environment Requirements
-
Local VM/Cloud server: centos 7.x version
-
Download FastDFS and Nginx-related software packages and upload them to the cloud server. The download link is as follows:
- Nginx – 1.18.0. Tar. Gz
- Libfastcommon – 1.0.38. Tar. Gz
- Fastdfs – 5.11. Tar. Gz
- Fastdfs nginx – module 1.20. Tar. Gz
After downloading, see the picture below:
Upload the downloaded compressed file to the specified directory on the cloud server using Xftp 6. The directory I specified is /usr/local/src/software
1.3 Installing FastDFS on a Single node
Since I’m only using FastDFS as a file server, I don’t need a cluster of FastDFS for personal development, and I don’t have a multi-server environment! Find an article about FastDFS cluster environment setup if you need to!
1.3.1 Installing GCC and nginx dependency libraries
Because FastDFS itself is the taobao Daniel written in C language, so it relies on environment is compiled C GCC, install command is as follows:
yum -y install gcc
Copy the code
In addition to GCC, you will need to install the nginx-related dependency libraries, because nginx will be installed later:
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl--devel
yum install libevent
Copy the code
Note: If your nginx package is in zip format instead of tar.gz, you need to download the unzip command to unzip the zip file.
yum install -y unzip zip
Copy the code
1.3.2 installation libfastcommon
CD /usr/local/src/software is used to upload the software package
Decompress: tar -zxvf libfastcommon-1.0.38 Go to the decompressed directory.cdLibfastcommon-1.0.38 / Run the compile and install:./make. Sh./makeCopy the code
1.3.3 installation fastdfs
Decompress: tar -zxvf fastdfs-5.11.tar.gz Go to the decompressed directory.cdFastdfs -5.11/ Run the following command to compile and install:./make. Sh./makeCopy the code
If the installation is successful, you can see the script file provided in /etc/init.d/ :
fdfs_trackerd
是trackerThe startup scriptfdfs_storaged
是storageThe startup script
You can see the default configuration file template in the /etc/fdfs/directory:
Run the following command to run the /etc/fdfs/command: -rw-r--r-- 1 root root 1461 Dec 9 20:26 client.conf.sample -rw-r--r-- 1 root root 7927 Dec 9 20:26 storage.conf.sample -rw-r--r-- 1 root root 7389 Dec 9 20:26 tracker.conf.sampleCopy the code
tarcker.conf.sample
是trackerConfiguration file templatestorage.conf.sample
是storageConfiguration file templateclient.conf.sample
是The clientConfiguration file template
1.3.4 Configuring and Starting the Tracker service
Both FastDFS tracker and storage were installed during the previous installation, so we installed them the same way. The difference is that the two require different configuration files.
To start the tracker, modify the tarck.conf script you just saw and start the fdfs_trackerd script:
1) First copy the template file:
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
Copy the code
2) Modify the copied configuration file:
vim /etc/fdfs/tracker.conf
# The modified content is as follows:
base_path=/fastdfs_files/tracker # self-specified root directory for storing logs and data
Copy the code
The directory you specified does not exist by default, so after modifying the configuration file, we create a new directory:
mkdir -p /fastdfs_files/tracker
Copy the code
Note: If you use a local VM, please turn off the firewall. Remember to open the port corresponding to the security group on the cloud server! Here are the open ports for my cloud server. FastDFS requires ports 23000 and 22122 by default:
4) Start and stop:
- Start thetrackerServer:
/etc/init.d/fdfs_trackerd start
- stoptrackerServer:
/etc/init.d/fdfs_trackerd stop
However, during the installation, FDFS has been set as a system service. We can use the familiar service startup mode:
service fdfs_trackerd start # Start fdfs_trackerd and stop using stop
Copy the code
Check whether the FastDFS Tracker Server is started successfully:
ps -ef | grep fdfs_trackerd
Copy the code
If the VM is a local VM and you do not want to restart the service every time it is restarted, you can enable automatic startup for the VM:
chkconfig fdfs_trackerd on
Copy the code
1.3.5 Configuring and Starting the Storage Service
The steps are similar to 1.3.4:
1) First copy the template file:
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
Copy the code
2) Modify the copied configuration file:
vim /etc/fdfs/storage.conf
# The modified content is as follows:
base_path=/fastdfs_files/storage The root directory for storing data and log files
store_path0=/fastdfs_files/storage # the first storage directoryTracker_server = native IP: 22122# tracker Server IP address and port
Copy the code
The directory you specified does not exist by default, so after modifying the configuration file, we create a new directory:
mkdir -p /fastdfs_files/storage
Copy the code
3) Start and stop:
- Start thestorageServer:
/etc/init.d/fdfs_storaged start
- stopstorageServer:
/etc/init.d/fdfs_storaged stop
You are advised to use the following startup modes:
service fdfs_storaged start # start the fDFs_storaged service and stop using stop
Copy the code
You can use the ps – ef | grep FDFS view the process:
If the VM is a local VM and you do not want to restart the service every time it is restarted, you can enable automatic startup for the VM:
chkconfig fdfs_trackerd on
Copy the code
Remind again: disable the firewall on the local VM, and enable the security group port on the cloud server!
2. Access FastDFS using nginx
2.1 installation fastdfs – nginx – module
2.1.1 decompression
The tar - ZXVF fastdfs - nginx - module - 1.20. Tar. GzCopy the code
2.1.2 Modifying the Config File
1) Go to the SRC directory in the decompressed folder
cdFastdfs nginx - module - 1.20 / SRC /Copy the code
2) Edit config
vim config
Copy the code
Use the following bottom line command:
:%s+/usr/local/+/usr/+g
Copy the code
Replace all /usr/local with /usr, which is the correct directory:
2.1.3 Associating nginx with the FastDFS Profile
Copy the fastdfs-nginx-module-1.20 config file to /etc/fdfs and modify it:
cp /usr/local/ SRC/software/fastdfs - nginx - module - 1.20 / SRC/mod_fastdfs. Conf/etc/FDFS/vim/etc/FDFS mod_fastdfs. ConfCopy the code
Modify the following configuration:
connect_timeout=10 Client access file connection timeout duration (unit: seconds)Tracker_server = native IP: 22122# tracker service IP address and port
url_have_group_name=true Access links are prefixed with the group name
store_path0=/fastdfs_files/storage # File storage path
Copy the code
Copy some FastDFS configuration files to /etc/fdfs directory:
cd /usr/local/ SRC /software/fastdfs-5.11/conf/ cp http.conf mime.types /etc/fdfs.Copy the code
2.2 Installing the Nginx plug-in
Since the nginx dependency libraries are already installed in section 1.3.1 of this article, there is no need to install them here:
# install virtual nginx dependency library
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
Copy the code
2.2.1 Decompressing the installation package
The tar - ZXVF nginx - 1.18.0. Tar. GzCopy the code
2.2.2 Configuring the nginx installation package and specifying fastdfs-nginx-model
# unzip nginx-1.18.0 directory:
cdNginx 1.18.0 /Run the following command:
./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=/usr/local/ SRC/software/fastdfs - nginx - module - 1.20 / SRC /Copy the code
Note: in the execution. / configure nginx configuration parameters, you need to fastdfs nginx – moudle source as modules compiled in.
2.2.3 Compiling and Installing the SOFTWARE
make && make install
Copy the code
2.2.4 start nginx
Configure nginx integration fastdfs – the module module, we need to modify the nginx configuration file, in the/opt/nginx/config/nginx. Conf file:
vim /opt/nginx/conf/nginx.conf
Copy the code
Put files in the original server 80{… } part of the code is replaced with the following code:
server { listen 80; Server_name Your domain name;FastDFS FastDFS FastDFS FastDFS FastDFSlocation ~/group([0-9])/ { ngx_fastdfs_module; } location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}Copy the code
Start command:
nginx # start nginx
nginx -s stop # stop nginx
nginx -s reload Reload the configuration file
Copy the code
Through ps – ef | grep nginx see if nginx has launched successfully:
3. Integrate FastDFS with SpringBoot
3.1 Related Dependencies
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
</dependency>
Copy the code
3.2 Related Configuration Parameters
# fastDFS configuration
fdfs:
so-timeout: 1501 Read timeout
connect-timeout: 601 Connection timeout
thumb-image: # Thumbnail configuration
width: 60
height: 60
tracker-list: # tracker address: your virtual machine server address + port (default: 22122)
- Your server IP address :22122
Copy the code
3.3 Related Configuration Classes
/ * * *@Auther: csp1999
* @Date: 2020/12/10/11:29
* @Description: fastDFS client configuration class */
@Configuration
@Import(FdfsClientConfig.class)
// Resolve JMX double-registering beans
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastDfsClientConfig {}Copy the code
3.4 Tests are performed in test classes
/ * * *@Auther: csp1999
* @Date: 2020/12/10 / o *@Description: Test FastDFS file uploads */
@SpringBootTest
@RunWith(SpringRunner.class)
public class FastDFSTest {
// fastDFS storage client
@Autowired
private FastFileStorageClient storageClient;
// Used to get fastDFS thumbnails
@Autowired
private ThumbImageConfig thumbImageConfig;
@Test
public void testUpload(a) throws FileNotFoundException {
// Get the file to be uploaded
File file = new File("M:\\code\\IDEA\\leyou\\image\\lf.jpg");
/** * Upload and save images ** Parameters: * 1- stream of uploaded files * 2- File size * 3- file suffix * 4- can be ignored */
StorePath storePath = this.storageClient.uploadFile(
new FileInputStream(file), file.length(), "jpg".null);
// Path with grouping
System.out.println(storePath.getFullPath());
// No grouped path
System.out.println(storePath.getPath());
}
@Test
public void testUploadAndCreateThumb(a) throws FileNotFoundException {
// Get the file to be uploaded
File file = new File("M:\\code\\IDEA\\leyou\\image\\bz4.jpg");
/** * Upload and save images and generate thumbnails ** Parameters: * 1- uploaded file stream * 2- file size * 3- file suffix * 4- can be ignored */
StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
new FileInputStream(file), file.length(), "png".null);
// Path with grouping
System.out.println(storePath.getFullPath());
// No grouped path
System.out.println(storePath.getPath());
// Get the thumbnail pathString path = thumbImageConfig.getThumbImagePath(storePath.getPath()); System.out.println(path); }}Copy the code
TestUpload () method is tested first, and the output results are as follows:
And then we can through the server IP address/group1 / M00/00/00 / wKg4ZVsWl5eAdLNZAABAhya2V0c424. JPG, visit image:
Similarly, we tested the method of testUploadAndCreateThumb() with thumbnail, and the output result is as follows:
Group1 / M00/00/00 / rBHaFV_Rmq6AC7m3AAQvu4hOzN4945. PNG #. Take the path of the grouping M00/00/00 / rBHaFV_Rmq6AC7m3AAQvu4hOzN4945 PNG # don't take the path of the group M00/00/00/ rbhafv_RMq6AC7m3AaqVu4hozn4945_60x60.png # Thumbnail pathCopy the code
We first look at the effect of the not thumbnails: IP address/group1 / M00/00/00 / rBHaFV_Rmq6AC7m3AAQvu4hOzN4945 PNG
Take a look at the thumbnail image: IP address /group1/ m00/00/00 / rbhafv_RMq6ac7m3aaqvu4hozn4945_60x60.png
The image size is 60px wide and high as we configured in the configuration file! We can go to server /fastdfs_files/storage/data/ to view the uploaded image:
SpringBoot FastDFS integration complete!
Extensions: In addition to FastDFS distributed file uploads, osS-BASED file uploads are now common in development.
Springboot operation ali cloud OSS implementation file upload, download, delete