溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

發(fā)布時(shí)間:2022-03-29 14:18:07 來源:億速云 閱讀:229 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)”,在日常操作中,相信很多人在如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

fastdfs服務(wù)搭建

先要安裝gcc編譯器:

yum -y install gcc-c++

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

安裝lib依賴包

tar -zxvf V1.0.43.tar.gz
cd libfastcommon-1.0.43
./make.sh
./make.sh install

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

安裝fastdfs服務(wù)

tar -zxvf V6.06.tar.gz
cd fastdfs-6.06
./make.sh
./make.sh install

配置Tracker服務(wù)

mkdir -p /data/fastdfs/tracker
cd /etc/fdfs
cp tracker.conf.sample  tracker.conf
vim tracker.conf

要修改的內(nèi)容為

#啟用配置文件(默認(rèn)啟用)
disabled=false 
#設(shè)置tracker的端口號(hào),通常采用22122這個(gè)默認(rèn)端口
port=22122 
#設(shè)置tracker的數(shù)據(jù)文件和日志目錄
base_path=/data/fastdfs/tracker
#設(shè)置http端口號(hào),默認(rèn)為8080
http.server_port=80

啟動(dòng)服務(wù)

#啟動(dòng)
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

查看是否啟動(dòng)

#查看啟動(dòng)端口
ss -ant | grep 22122

查看啟動(dòng)日志

#查看啟動(dòng)日志
tail -f /data/fastdfs/tracker/logs/trackerd.log

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)
加入開機(jī)啟動(dòng)

vim /etc/rc.d/rc.local

在加入啟動(dòng)命令

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)
如果重啟后發(fā)現(xiàn)未能自動(dòng)啟動(dòng)則通過命令

ll /etc/rc.d/rc.local

檢查一下rc.local是否具備可執(zhí)行權(quán)限,若是無可執(zhí)行權(quán)限則通過命令進(jìn)行授權(quán)

chmod +x /etc/rc.d/rc.local

配置Storage服務(wù)

mkdir -p /data/fastdfs/storage
mkdir -p /data/fastdfs/storage/file
cd /etc/fdfs
cp storage.conf.sample  storage.conf
vim storage.conf

要修改的內(nèi)容為

#啟用配置文件(默認(rèn)啟用)
disabled=false
#組名,根據(jù)實(shí)際情況修改 
group_name=group1 
#設(shè)置storage的端口號(hào),默認(rèn)是23000,同一個(gè)組的storage端口號(hào)必須一致
port=23000 
#設(shè)置storage數(shù)據(jù)文件和日志目錄
base_path=/data/fastdfs/storage 
#存儲(chǔ)路徑個(gè)數(shù),需要和store_path個(gè)數(shù)匹配
store_path_count=1 
#實(shí)際文件存儲(chǔ)路徑
store_path0=/data/fastdfs/storage/file
#tracker 服務(wù)器的 IP地址和端口號(hào),如果是單機(jī)搭建,IP不要寫127.0.0.1,否則啟動(dòng)不成功(此處的ip是我的CentOS虛擬機(jī)ip)
tracker_server=172.16.6.50:22122 
#設(shè)置 http 端口號(hào)
http.server_port=8888

啟動(dòng)服務(wù)

#啟動(dòng)
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start

查看啟動(dòng)端口

#查看啟動(dòng)端口
ss -ant | grep 23000

查看啟動(dòng)日志

#查看啟動(dòng)日志
tail -f /data/fastdfs/storage/logs/storaged.log

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

通過monitor查看storage是否綁定成功

[root@localhost /]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
[2021-09-23 12:59:26] DEBUG - base_path=/opt/fastdfs_storage, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

server_count=1, server_index=0

tracker server is 172.16.8.11:22122

group count: 1

Group 1:
group name = group1
disk total space = 6818 MB
disk free space = 2169 MB
trunk free space = 0 MB
……

加入開機(jī)啟動(dòng)

vim /etc/rc.d/rc.local

在該文件中,加入啟動(dòng)命令

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start

測(cè)試驗(yàn)證

[root@localhost ~]# ps -ef|grep fdfs
root     10335 17685  0 23:50 pts/3    00:00:00 grep --color=auto fdfs
root     13335     1  0 13:17 ?        00:00:07 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
root     15779     1  0 12:59 ?        00:00:04 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

用fdfs客戶端進(jìn)行測(cè)試

cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf

測(cè)試時(shí)需要設(shè)置客戶端的配置文件

vim /etc/fdfs/client.conf

打開文件后依次做以下修改:

#tracker服務(wù)器文件路徑
base_path=/data/fastdfs/tracker
#tracker服務(wù)器IP地址和端口號(hào)
tracker_server=172.16.7.50:22122
# tracker 服務(wù)器的 http 端口號(hào),必須和tracker的設(shè)置對(duì)應(yīng)起來
http.tracker_server_port=80

配置完成后就可以模擬文件上傳了,先給/data目錄下放一文件test.txt,然后通過執(zhí)行客戶端上傳命令嘗試上傳:

/usr/bin/fdfs_upload_file  /etc/fdfs/client.conf  /data/test.txt

單機(jī)redis服務(wù)搭建

1.獲取redis資源,并解壓

tar xzvf redis-4.0.8.tar.gz

2.安裝

cd redis-4.0.8
make
cd src
make install PREFIX=/usr/local/redis

3.移動(dòng)配置文件到安裝目錄下

cd ../
mkdir /usr/local/redis/etc
mv redis.conf /usr/local/redis/etc

 4.配置redis為后臺(tái)啟動(dòng)

vim /usr/local/redis/etc/redis.conf

注意,將daemonize no 改成daemonize yes,及注釋掉bind 127.0.0.1,可以遠(yuǎn)程訪問

5.將redis加入到開機(jī)啟動(dòng) 

vim /etc/rc.d/rc.local

在里面添加內(nèi)容:

/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

意思就是開機(jī)調(diào)用這段開啟redis的命令.

6.啟動(dòng)redis

/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf

7.將redis-cli,redis-server拷貝到bin下,讓redis-cli指令可以在任意目錄下直接使用

cp /usr/local/redis/bin/redis-server /usr/local/bin/
cp /usr/local/redis/bin/redis-cli /usr/local/bin/

8.設(shè)置redis密碼

a.運(yùn)行命令:

redis-cli

b.查看現(xiàn)有的redis密碼(可選操作,可以沒有)

運(yùn)行命令:

config get requirepass

如果沒有設(shè)置過密碼的話運(yùn)行結(jié)果會(huì)如下圖所示

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

c.設(shè)置redis密碼

運(yùn)行命令:

config set requirepass  *******

(******為你要設(shè)置的密碼),設(shè)置成功的話會(huì)返回‘OK’字樣

d.測(cè)試連接

重啟redis服務(wù)

//****為你設(shè)置的密碼
redis-cli -h 127.0.0.1 -p 6379 -a ****

也可以,輸入 redis-cli 進(jìn)入命令模式,使用 auth '*****' (****為你設(shè)置的密碼)登陸      

9.讓外網(wǎng)能夠訪問redis

a.配置防火墻:

#開放6379端口 
firewall-cmd --zone=public --add-port=6379/tcp --permanent
#重啟防火墻以使配置即時(shí)生效
systemctl restart firewalld
#查看系統(tǒng)所有開放的端口
firewall-cmd --zone=public --list-ports

b.若是仍不可遠(yuǎn)程訪問:

此時(shí) 雖然防火墻開放了6379端口,但是外網(wǎng)還是無法訪問的,因?yàn)閞edis監(jiān)聽的是127.0.0.1:6379,并不監(jiān)聽外網(wǎng)的請(qǐng)求。

  1. 把文件夾目錄里的redis.conf配置文件里的bind 127.0.0.1前面加#注釋掉

  2. 命令:redis-cli連接到redis后,通過 config get  daemonize和config get  protected-mode 是不是都為no,如果不是,就用config set 配置名 屬性 改為no。

其他redis相關(guān)常用命令  

卸載redis:

#停止redis
pkill redis   
#刪除安裝目錄
rm -rf /usr/local/redis 
#刪除所有redis相關(guān)命令腳本
rm -rf /usr/bin/redis-* 
#刪除redis解壓文件夾
rm -rf /root/download/redis-4.0.4

報(bào)異常,嘗試重啟

原因: Redis已經(jīng)啟動(dòng)

解決: 關(guān)掉Redis,重啟即可 

redis-cli shutdown 
redis-server &

然后你就能看到Redis愉快的運(yùn)行了.

客戶端檢測(cè)連接是否正常

使用redis-cli客戶端檢測(cè)連接是否正常

$redis-cli 
127.0.0.1:6379> keys * 
(empty list or set) 
127.0.0.1:6379> set key "hello world" 
OK 
127.0.0.1:6379> get key 
"hello world"

Redis客戶端常見操作

Redis是key-value數(shù)據(jù)庫,支持五種數(shù)據(jù)類型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。

當(dāng)value是string類型,命令包括set get setnx incr del 等。

> set server:name "fido"  // 設(shè)置鍵值
OK
> get server:name  // 獲取鍵值
"fido"
> setnx connections 10   // set if not exists
OK
> incr connections   // 原子性增加values值
(integer) 11
> incr connections
(integer) 12
> del connections  // 刪除key
(integer) 1
> incr connections
(integer) 1

當(dāng)value是list類型,命令包括rpush lpush llen lrange lpop rpop del 等。

> rpush friends "Alice"   // 在末尾追加
(integer) 1
> rpush friends "Bob"
(integer) 2
> lpush friends "Sam"    // 插入到開頭
(integer) 3 
> lrange friends 0 -1     // 返回列表的子集,類似切片操作
1) "Sam"
2) "Alice"
3) "Bob"
> lrange friends 0 1
1) "Sam"
2) "Alice"
> lrange friends 1 2
1) "Alice"
2) "Bob" 
> llen friends   // 返回列表長(zhǎng)度
(integer) 3
> lpop friends   // 刪除并返回列表第一個(gè)元素
"Sam"
> rpop friends   // 刪除并返回列表最后一個(gè)元素
"Bob"
> lrange friends 0 -1
1) "Alice" 
> del friends    // 刪除key
(integer) 1     // 1表示成功,0表示失敗

當(dāng)value是set類型,命令包括sadd srem sismember smembers sunion del等。

> sadd superpowers "flight"     // 添加元素
(integer) 1
> sadd superpowers "x-ray vision"
(integer) 1
> sadd superpowers "reflexes"
(integer) 1
> srem superpowers "reflexes"   // 刪除元素1
 
> sismember superpowers "flight"   // 測(cè)試元素是否在集合中
(integer) 1
> sismember superpowers "reflexes"
(integer) 0
> smembers superpowers    // 返回集合中所有元素
1) "x-ray vision"
2) "flight" 
> sadd birdpowers "pecking"
(integer) 1
> sadd birdpowers "flight"
(integer) 1
> sunion superpowers birdpowers    // 合并多個(gè)set,返回合并后的元素列表
1) "x-ray vision"
2) "flight"
3) "pecking" 
> del superpowers   // 刪除key
(integer) 1

當(dāng)value是zset類型,命令包括 zadd zrange del等,注意給value一個(gè)編號(hào)用于排序。

> zadd hacker 1940 "Alan Kay"     // 給value指定一個(gè)編號(hào),比如以年份1940作為編號(hào)
(integer) 1
> zadd hacker 1906 "Grace Hopper"
(integer) 1
> zadd hacker 1953 "Richard Stallman"
(integer) 1
> zadd hacker 1965 "Yukihiro Matsumoto"
(integer) 1
> zadd hacker 1916 "Claude Shannon"
(integer) 1
> zadd hacker 1969 "Linux Torvalds"
(integer) 1
> zadd hacker 1957 "Sophie Wilson"
(integer) 1
> zadd hacker 1912 "Alan Turing"
(integer) 1
 
> zrange hacker 2 4    // 切片返回有序集合中元素
1) "Claude Shannon"
2) "Alan Kay"
3) "Richard Stallman"
 
> del hacker    // 刪除key
(integer) 1

當(dāng)value是hash類型,hash類型可以理解為字典,需要給value指定一個(gè)field用于映射,命令包括hset hmset hget hgetall hdel hincrby del 等。

> hset user:1000 name "John Smith"   // 給value指定一個(gè)field,比如name
(integer) 1
> hset user:1000 email "john.smith@example.com"
(integer) 1
> hset user:1000 password "s3cret"
(integer) 1
> hgetall user:1000   // 獲得hash表中所有成員,包括field和value
1) "name"
2) "John Smith"
3) "email"
4) "john.smith@example.com"
5) "password"
6) "s3cret"
 
> hmset user:1001 name "Mary Jones" password "hidden" email
"mjones@example.com"   // 設(shè)置多個(gè)field和value
OK
> hget user:1001 name   // 根據(jù)field獲取value
"Mary Jones"
  
> hset user:1000 visits 10    // field可以映射到數(shù)字值
(integer) 1
> hincrby user:1000 visits 1    // 原子性增加value的值,增加1
(integer) 11
> hincrby user:1000 visits 10    // 增加10
(integer) 21
> hdel user:1000 visits      // 刪除field及其value
(integer) 1
> hincrby user:1000 visits 1
(integer) 1
 
> del user:1000     // 刪除key
(integer) 1

設(shè)置和查看key的生命周期,key過期會(huì)被自動(dòng)刪除,命令包括expire ttl 等。

> set resource:lock "Redis Demo"
OK
> expire resource:lock 120   // 設(shè)置生命周期為120s
(integer) 1
> ttl resource:lock   // 查看當(dāng)前生命周期還剩多少時(shí)間
(integer) 109
> ttl resource:lock   // 120s后查看,返回-2表示已過期或不存在
(integer) -2
 
> set resource:lock "Redis Demo 2"
OK
> ttl resource:lock    // 返回-1表示永不過期
(integer) -1

springboot實(shí)現(xiàn)h5與fastdfs之間的斷點(diǎn)續(xù)傳,大文件上傳,秒傳文件和批量上傳 

對(duì)比,只是單純返回一個(gè)String類型?

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

改為upload_list8888,頁面調(diào)用是失敗的

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

改回upload_list

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

ThreadLocal與redis秒配,解決了redis線程池在被共享訪問時(shí)帶來的線程安全問題

打個(gè)比方,現(xiàn)在公司所有人都要填寫一個(gè)表格,但是只有一支筆,這個(gè)時(shí)候就只能上個(gè)人用完了之后,下個(gè)人才可以使用,為了保證"筆"這個(gè)資源的可用性,只需要保證在接下來每個(gè)人的獲取順序就可以了,這就是 lock 的作用,當(dāng)這支筆被別人用的時(shí)候,我就加 lock ,你來了那就進(jìn)入隊(duì)列排隊(duì)等待獲取資源(非公平方式那就另外說了),這支筆用完之后就釋放 lock ,然后按照順序給下個(gè)人使用。

但是完全可以一個(gè)人一支筆對(duì)不對(duì),這樣的話,你填寫你的表格,我填寫我的表格,咱倆誰都不耽擱誰。這就是 ThreadLocal 在做的事情,因?yàn)槊總€(gè) Thread 都有一個(gè)副本,就不存在資源競(jìng)爭(zhēng),所以也就不需要加鎖,這不就是拿空間去換了時(shí)間嘛!

了解決redis線程池對(duì)象(筆),不能被多線程(多個(gè)人)共享訪問的問題,通過 threadLocal.set() 方法,將redis線程池對(duì)象實(shí)例保存在每個(gè)線程,自己所擁有的 threadLocalMap中(生成多個(gè)副本)。

這樣的話,每個(gè)線程都使用自己的redis線程池對(duì)象實(shí)例,彼此不會(huì)影響,從而達(dá)到了隔離的作用,這樣就解決了redis線程池對(duì)象在被共享訪問時(shí)帶來的線程安全問題。

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;


public class RedisUtil {

    private RedisUtil() {
    }
    private static Logger _logger = LoggerFactory.getLogger(RedisUtil.class);
    ;

    protected static final  ThreadLocal<Jedis> threadLocalJedis = new ThreadLocal<Jedis>();

    //Redis服務(wù)器IP
    private static String ADDR_ARRAY = ReadProper.getResourceValue("spring.redis.host");

    //Redis的端口號(hào)
    private static int PORT = Integer.parseInt(ReadProper.getResourceValue("spring.redis.port"));

    //訪問密碼
    private static String AUTH = ReadProper.getResourceValue("spring.redis.password");

    //可用連接實(shí)例的最大數(shù)目&#xff0c;默認(rèn)值為8&#xff1b;
    //如果賦值為-1&#xff0c;則表示不限制&#xff1b;如果pool已經(jīng)分配了maxActive個(gè)jedis實(shí)例&#xff0c;則此時(shí)pool的狀態(tài)為exhausted(耗盡)。
    private static int MAX_ACTIVE = -1;

    //控制一個(gè)pool最多有多少個(gè)狀態(tài)為idle(空閑的)的jedis實(shí)例&#xff0c;默認(rèn)值也是8。
    private static int MAX_IDLE = 16;

    //等待可用連接的最大時(shí)間&#xff0c;單位毫秒&#xff0c;默認(rèn)值為-1&#xff0c;表示永不超時(shí)。如果超過等待時(shí)間&#xff0c;則直接拋出JedisConnectionException&#xff1b;
    private static int MAX_WAIT = 1000 * 5;

    //超時(shí)時(shí)間
    private static int TIMEOUT = 1000 * 5;

    //在borrow一個(gè)jedis實(shí)例時(shí)&#xff0c;是否提前進(jìn)行validate操作&#xff1b;如果為true&#xff0c;則得到的jedis實(shí)例均是可用的&#xff1b;
    private static boolean TEST_ON_BORROW = true;

    private static JedisPool jedisPool ;

    //默認(rèn)的數(shù)據(jù)庫為0



    /**
     * redis過期時(shí)間,以秒為單位
     */
    public final static int EXRP_HOUR = 60 * 60;          //一小時(shí)
    public final static int EXRP_DAY = 60 * 60 * 24;        //一天
    public final static int EXRP_MONTH = 60 * 60 * 24 * 30;   //一個(gè)月





    /**
     * 初始化Redis連接池,注意一定要在使用前初始化一次,一般在項(xiàng)目啟動(dòng)時(shí)初始化就行了
     */
    public static JedisPool  initialPool() {
        JedisPool jp=null;
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(MAX_ACTIVE);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setTestOnBorrow(TEST_ON_BORROW);
            config.setTestOnCreate(true);
            config.setTestWhileIdle(true);
            config.setTestOnReturn(true);
            config.setNumTestsPerEvictionRun(-1);
            jp = new JedisPool(config, ADDR_ARRAY, PORT, TIMEOUT, AUTH);
            jedisPool=jp;
            threadLocalJedis.set(getJedis());
        } catch (Exception e) {
            e.printStackTrace();
            _logger.error("redis服務(wù)器異常",e);

        }

        return  jp;
    }



    public static void close(Jedis jedis) {
        if (threadLocalJedis.get() == null && jedis != null){
            jedis.close();
        }
    }






//    /**
//     * 在多線程環(huán)境同步初始化
//     */
//    private static synchronized void poolInit() {
//        if (jedisPool == null) {
//            initialPool();
//        }
//    }


    /**
     * 獲取Jedis實(shí)例,一定先初始化
     *
     * @return Jedis
     */
    public static Jedis getJedis() {
        boolean success = false;
        Jedis jedis = null;
//        if (jedisPool == null) {
//            poolInit();
//        }
        int i=0;
        while (!success) {
            i++;
            try {
                if (jedisPool != null) {
                    jedis=threadLocalJedis.get();
                    if (jedis==null){
                        jedis = jedisPool.getResource();
                    }else {
                       if(! jedis.isConnected()&&!jedis.getClient().isBroken()){
                           threadLocalJedis.set(null);
                           jedis = jedisPool.getResource();
                       }
                        //System.out.println(Thread.currentThread().getName()+":第"+i+"次獲取成功#@利用了本地緩存redis");
                        return jedis;
                    }

                }else {
                    throw new RuntimeException("redis連接池初始化失敗");
                }
            } catch (Exception e) {

               System.out.println(Thread.currentThread().getName()+":第"+i+"次獲取失敗!!!");
                success = false;
                e.printStackTrace();
                _logger.error("redis服務(wù)器異常",e);
            }
            if (jedis!=null){
                success=true;
            }

            if (i>=10&&i<20){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

            if (i>=20&&i<30){
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

            if (i>=30&&i<40){
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

            if (i>=40){
                System.out.println("redis徹底連不上了~~~~(>_<)~~~~");
                return null;
            }

        }
        if (threadLocalJedis.get()==null){threadLocalJedis.set(jedis);}
        //System.out.println(Thread.currentThread().getName()+":第"+i+"次獲取成功@");
        return jedis;
    }




    /**
     * 設(shè)置 String
     *
     * @param key
     * @param value
     */
    public static void setString(String key, String value) {
        Jedis jo = null;
        try {

            value = StrUtil.isBlank(value) ? "" : value;
            jo = getJedis();
            jo.set(key, value);
        } catch (Exception e) {
            threadLocalJedis.set(null);
            _logger.error("redis服務(wù)器異常",e);
            throw new  RuntimeException("redis服務(wù)器異常");
        } finally {
            if (jo != null) {
                close(jo);
            }

        }
    }

    /**
     * 設(shè)置 過期時(shí)間
     *
     * @param key
     * @param seconds 以秒為單位
     * @param value
     */
    public static void setString(String key, int seconds, String value) {
        Jedis jo = null;
        try {
            value = StrUtil.isBlank(value) ? "" : value;
            jo = getJedis();
            jo.setex(key, seconds, value);
        } catch (Exception e) {
            threadLocalJedis.set(null);
            e.printStackTrace();
            _logger.error("redis服務(wù)器異常",e);
            throw new  RuntimeException("redis服務(wù)器異常");
        } finally {
            if (jo != null) {
                close(jo);

            }
        }


    }

    /**
     * 獲取String值
     *
     * @param key
     * @return value
     */
    public static String getString(String key) {
        Jedis jo = null;

        try {
            jo = getJedis();

            if (jo == null || !jo.exists(key)) {
                return null;
            }
            return jo.get(key);
        } catch (Exception e) {
            threadLocalJedis.set(null);
            e.printStackTrace();
            _logger.error("redis服務(wù)器異常",e);
            throw new  RuntimeException("redis操作錯(cuò)誤");
        } finally {
            if (jo != null) {
                close(jo);
            }
        }


    }

    public static long incrBy(String key, long integer) {
        Jedis jo = null;
        try {
            jo = getJedis();

            return jo.incrBy(key, integer);
        } catch (Exception e) {
            threadLocalJedis.set(null);
            e.printStackTrace();
            _logger.error("redis服務(wù)器異常",e);
            throw new  RuntimeException("redis操作錯(cuò)誤");
        } finally {
            if (jo != null) {
                close(jo);
            }
        }

    }

    public static long decrBy(String key, long integer) {
        Jedis jo = null;
        try {
            jo = getJedis();
            return jo.decrBy(key, integer);
        } catch (Exception e) {
            threadLocalJedis.set(null);
            e.printStackTrace();
            _logger.error("redis服務(wù)器異常",e);
            throw new  RuntimeException("redis操作錯(cuò)誤");
        } finally {
            if (jo != null) {
                close(jo);
            }
        }

    }

    //刪除多個(gè)key
    public static  long  delKeys(String [] keys){
        Jedis jo = null;
        try {
            jo = getJedis();
            return jo.del(keys);
        } catch (Exception e) {
            threadLocalJedis.set(null);
            e.printStackTrace();
            _logger.error("redis服務(wù)器異常",e);
            throw new  RuntimeException("redis操作錯(cuò)誤");
        } finally {
            if (jo != null) {
                close(jo);
            }
        }

    }

    //刪除單個(gè)key
    public static  long  delKey(String  key){
        Jedis jo = null;
        try {
            jo = getJedis();
            return jo.del(key);
        } catch (Exception e) {
            threadLocalJedis.set(null);
            e.printStackTrace();
            _logger.error("redis服務(wù)器異常",e);
            throw new  RuntimeException("redis操作錯(cuò)誤");
        } finally {
            if (jo != null) {
                close(jo);
            }
        }

    }

    //添加到隊(duì)列尾
    public static  long  rpush(String  key,String node){
        Jedis jo = null;
        try {
            jo = getJedis();
            return jo.rpush(key,node);
        } catch (Exception e) {
            threadLocalJedis.set(null);
            e.printStackTrace();
            _logger.error("redis服務(wù)器異常",e);
            throw new  RuntimeException("redis操作錯(cuò)誤");
        } finally {
            if (jo != null) {
                close(jo);
            }
        }

    }


    //刪除list元素
    public static  long  delListNode(String  key,int count,String value){
        Jedis jo = null;
        try {
            jo = getJedis();
            return jo.lrem(key,count,value);
        } catch (Exception e) {
            threadLocalJedis.set(null);
            e.printStackTrace();
            _logger.error("redis服務(wù)器異常",e);
            throw new  RuntimeException("redis操作錯(cuò)誤");
        } finally {
            if (jo != null) {
                close(jo);
            }
        }

    }




    //獲取所有l(wèi)ist

    public static List getListAll(String key){
        Jedis jo = null;
        List list=null;
        try {
            jo = getJedis();
            list=    jo.lrange(key,0,-1);
        } catch (Exception e) {
            threadLocalJedis.set(null);
            e.printStackTrace();
            _logger.error("redis服務(wù)器異常",e);
            throw new  RuntimeException("redis操作錯(cuò)誤");
        } finally {
            if (jo != null) {
                close(jo);
            }
        }
        return  list;
    }

    //清理緩存redis
    public  static  void cleanLoacl(Jedis jo){
        threadLocalJedis.set(null);
        close(jo);
    }

static {
  initialPool();
}

}

使用webuploader組件實(shí)現(xiàn)大文件分片上傳&#xff0c;斷點(diǎn)續(xù)傳

webuploader&#xff1a;是一個(gè)以HTML5為主&#xff0c; Flash為輔的文件上傳組件&#xff0c;采用大文件分片/并發(fā)上傳的方式&#xff0c;極大地提高了文件上傳的效率&#xff0c;同時(shí)兼容多種瀏覽器版本&#xff1b;

前端

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

引入百度Webuploader組件&#xff0c;需要注意標(biāo)簽的id/nama屬性&#xff0c;這些將在后面的JavaScript中使用到進(jìn)行文件切分、驗(yàn)證。

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

以上js組件&#xff0c;將完成文件上傳、MD5驗(yàn)證、刪除、切片、上傳進(jìn)度條顯示、暫停、繼續(xù)上傳及上傳成功/失敗時(shí)候的回調(diào)。

后端

前端&#xff0c;給后端提供封裝的chunk,及request

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

后端&#xff0c;主要是判斷文件是否有分片&#xff0c;如果沒有&#xff0c;則直接存放到目的目錄&#xff1b;

如果存在分片&#xff0c;則創(chuàng)建臨時(shí)目錄&#xff0c;存放分片信息&#xff1b;

之后判斷當(dāng)前分片所屬的文件的所有分片是否已經(jīng)傳輸完畢&#xff0c;如果當(dāng)前分片數(shù)==所屬文件總分片數(shù)&#xff0c;則開始合并文件并轉(zhuǎn)移完整文件到目的目錄&#xff0c;并且刪除臨時(shí)目錄

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

檢測(cè)完文件&#xff0c;以后&#xff0c;開始上傳操作

//上傳操作
path = appendFileStorageClient.uploadAppenderFile(UpLoadConstant.DEFAULT_GROUP, file.getInputStream(),file.getSize(), FileUtil.extName((String) paramMap.get("name")));
//更新操作
appendFileStorageClient.modifyFile(UpLoadConstant.DEFAULT_GROUP, noGroupPath, file.getInputStream(),file.getSize(),historyUpload);

hutool工具的巧妙運(yùn)用

可參考hutool資料http://www.mianshigee.com/tutorial/hutool/26e24c7a37d93249.md

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.0.6</version>
</dependency>

cn.hutool.core.collection.CollUtil判斷非空

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

cn.hutool.json.JSONObject與JSONUtil

if (CollUtil.isNotEmpty(fileList)){
            for (String e:fileList){
                JSONObject jsonObject= JSONUtil.parseObj(e);
                jsonObjects.add(jsonObject);

            }
        }

cn.hutool.core.convert.Convert類型轉(zhuǎn)換

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

cn.hutool.core.util.RandomUtil生成隨機(jī)字符串及StrUtil判斷字符為空

如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)

解析文件的擴(kuò)展名&#xff0c;來獲該文件的類型

import cn.hutool.core.io.FileUtil;
FileUtil.extName((String) paramMap.get("name")));

到此,關(guān)于“如何搭建fastdfs服務(wù)及單機(jī)redis服務(wù)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI