Practice: Docker builds FastDFS file system and integrates SpringBoot
preface
At that time, cloud storage was far from being as widely used as it is now, and the bottom line was cost and security issues. I remember that the company I worked for at that time was engaged in website development, and in the early stage, we used GridFS to store files with self-built mongodb.
Later it was fastDFS, and today we’re going to talk about fastDFS.
Introduction to the
FastDFS is an open source lightweight distributed file system developed in C language. It manages files. Its main functions include file storage, file synchronization, and file access (file upload/download).
“Features are as follows:“
- Applicable type: 4KB to 500MB
- File distribution: Small files are merged and stored without fragmentation
- Backup mechanism: Intra-group backup
- Communication protocol interfaces: Api and HTTP
- Development language: C language
- Users and communities: Mainly domestic users
The core concept
The FastDFS server has two core concepts: tracker and storage.
The former functions as load balancing and scheduling, while the latter provides capacity storage and backup.
A clear understanding of these two concepts will help with the following server setup.
Docker builds the server
Docker installation and entry is not much to explain, before also written a lot, not seen readers can from the history of the page search review.
This machine starts Docker service in Mac environment for installation, and other environments are based on the Docker environment with similar minor differences.
1. Run commands to search for mirrors
docker search fastdfs
Copy the code
2. Download the image shown in the figure
docker pull delron/fastdfs
Copy the code
I have downloaded it, so I need to update it
If you have not downloaded the image, just wait for the download to finish
View the image after downloading:
docker images
Copy the code
3. Start the Tracker container service
docker run -d --name tracker -p 22122:22122 -v ~/docker_work/tracker:/var/fdfs delron/fastdfs tracker
Copy the code
The container’s default port is 22122.
These directories are generated under the mount host directory after startup
4. Start the storage server
Before starting the storage server, we need to query the IP of the trace server, which will be used when starting the container.
The following command displays all container IP addresses:
docker inspect -f='{{.Name}} {{.NetworkSettings.IPAddress}} {{.HostConfig.PortBindings}}' $(docker ps -aq)
Copy the code
172.17.0.2. Take a little notebook and write it down
Of course, you can also directly use the host Ip 192.168.3.112
I’m just going to use the host IP,
Docker run -d --name storage -p 8888:8888 -p 23000:23000 -e TRACKER_SERVER=192.168.3.112:22122 -V ~/docker_work/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storageCopy the code
8888 is the Nginx access port of the image, and 23000 is the storage service port.
If you need to modify the corresponding port and configuration, their own Baidu good, here is not more introduction.
Ping the port to see if the service is available:
5. Verify the image server service
After the server is set up, we go into the container and use the command to verify that we can upload.
Start by putting an image into the mount directory
cp Downloads/ewm.jpg ~/docker_work/storage
Copy the code
Then go to the container, and the first discovery is in the nginx directory. Switch to /var/fdfs
Run the following commands to upload files:
docker exec -it storage bash
ls
cd /var/fdfs
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf ewm.jp
Copy the code
Relevant screenshots are as follows:
The successful execution of the command will return the file path suffix, which we can paste to the browser to access.
Now that the image server is done, let’s start integrating programs to play with it.
6. Create a SpringBoot program and integrate fastdFS dependencies
The encapsulation here relies on finding an open source wheel
Address: https://github.com/tobato/FastDFS_Client
Pom file
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.1 - RELEASE</version>
</dependency>
Copy the code
The following is the project integration directory structure and PostMan test call. Instead of Posting each class, the code can be found on Github.
“Postman calls“
“access“
conclusion
This article uses the MAC environment as an example to demonstrate how to set up a picture server and test, and use the client SpringBoot project to encapsulate and call the picture server API. Next time we will talk about MongoDb GridFS and see which one is more beautiful.
This article Java integration source code: github.com/pengziliu/G…