In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to build fastdfs service and stand-alone redis service". In daily operation, I believe many people have doubts about how to build fastdfs service and stand-alone redis service. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to build fastdfs service and stand-alone redis service". Next, please follow the editor to study!
First install the gcc compiler:
Yum-y install gcc-c++
Install the lib dependency package tar-zxvf V1.0.43.tar.gzcd libfastcommon-1.0.43./make.sh./make.sh install
Ln-s / usr/lib64/libfastcommon.so / usr/local/lib/libfastcommon.soln-s / usr/lib64/libfastcommon.so / usr/lib/libfastcommon.soln-s / usr/lib64/libfdfsclient.so / usr/local/lib/libfdfsclient.soln-s / usr/lib64/libfdfsclient.so / usr/lib/libfdfsclient.so install fastdfs service tar-zxvf V6.06.tar.gzcd fastdfs-6.06./make.sh./make.sh install configure Tracker service mkdir-p / data/fastdfs / trackercd / etc/fdfscp tracker.conf.sample tracker.confvim tracker.conf
The content to be modified is
# enable configuration file (default enabled) disabled=false # set the port number of tracker, usually use 22122 as the default port port=22122 # set tracker data file and log directory base_path=/data/fastdfs/tracker# set http port number, default is 8080http.server_port=80
Start the service
# launch / usr/bin/fdfs_trackerd / etc/fdfs/tracker.conf start
Check to see if it starts
# View launch port ss-ant | grep 22122
View the startup log
# View startup log tail-f / data/fastdfs/tracker/logs/trackerd.log
Join the boot boot
Vim / etc/rc.d/rc.local
After adding the startup command
If you find that it fails to start automatically after reboot, use the command
Ll / etc/rc.d/rc.local
Check whether rc.local has executable permission, and authorize it through command if there is no executable permission.
Chmod + x / etc/rc.d/rc.local configure Storage service mkdir-p / data/fastdfs/storagemkdir-p / data/fastdfs/storage/filecd / etc/fdfscp storage.conf.sample storage.confvim storage.conf
The content to be modified is
# enable configuration file (default enabled) disabled=false# group name, modify group_name=group1 # to set the port number of storage according to the actual situation, default is 23000, storage port number of the same group must be the same port=23000 # set the number of storage data files and log directories base_path=/data/fastdfs/storage # storage paths The number of store_path needs to match the IP address and port number of the store_path_count=1 # actual file storage path store_path0=/data/fastdfs/storage/file#tracker server. If it is built on a stand-alone machine, do not write 127.0.0.1 for IP, otherwise the startup is unsuccessful (the ip here is my CentOS virtual machine ip) tracker_server=172.16.6.50:22122 # set the http port number http.server_port=8888
Start the service
# launch / usr/bin/fdfs_storaged / etc/fdfs/storage.conf start
View startup port
# View launch port ss-ant | grep 23000
View the startup log
# View startup log tail-f / data/fastdfs/storage/logs/storaged.log
Check whether storage is bound successfully through monitor
[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: 0server_count=1 Server_index=0tracker server is 172.16.8.11:22122group count: 1Group 1:group name = group1disk total space = 6818 MBdisk free space = 2169 MBtrunk free space = 0 MB...
Join the boot boot
Vim / etc/rc.d/rc.local
In the file, add the startup command
/ usr/bin/fdfs_storaged / etc/fdfs/storage.conf start Test Verification [root@localhost ~] # ps-ef | grep fdfsroot 10335 17685 0 23:50 pts/3 00:00:00 grep-- color=auto fdfsroot 13335 10 13:17? 00:00:07 / usr/bin/fdfs_storaged / etc/fdfs/storage.conf startroot 15779 10 12:59? 00:00:04 / usr/bin/fdfs_trackerd / etc/fdfs/tracker.conf
Testing with fdfs client
Cp / etc/fdfs/client.conf.sample / etc/fdfs/client.conf
The configuration file of the client needs to be set up when testing
Vim / etc/fdfs/client.conf
After opening the file, make the following changes in turn:
# tracker server file path base_path=/data/fastdfs/tracker#tracker server IP address and port number http port number of tracker_server=172.16.7.50:22122# tracker server must correspond to the setting of tracker http.tracker_server_port=80
After the configuration is completed, you can simulate file upload. First, send a file test.txt to the / data directory, and then try to upload it by executing the client upload command:
/ usr/bin/fdfs_upload_file / etc/fdfs/client.conf / data/test.txt1. Get the redis resources and extract the tar xzvf redis-4.0.8.tar.gz2. Install cd redis-4.0.8makecd srcmake install PREFIX=/usr/local/redis3. Move the configuration file to cd.. / mkdir / usr/local/redis/etcmv redis.conf / usr/local/redis/etc4 in the installation directory. Configure redis to start vim / usr/local/redis/etc/redis.conf in the background
Note that changing daemonize no to daemonize yes and commenting out bind 127.0.0.1 can be accessed remotely.
5. Add redis to boot vim / etc/rc.d/rc.local
Add content to it:
/ usr/local/redis/bin/redis-server / usr/local/redis/etc/redis.conf
It means to start up and call the command to open redis.
6. Start redis/usr/local/redis/bin/redis-server / usr/local/redis/etc/redis.conf7. Copy redis-cli,redis-server to bin so that the redis-cli instruction can directly use cp / usr/local/redis/bin/redis-server / usr/local/bin/cp / usr/local/redis/bin/redis-cli / usr/local/bin/8 in any directory. Set the redis password a. Run the command: redis-clib. View existing redis passwords (optional, may not be available)
Run the command:
Config get requirepass
If the password has not been set, the running result will look like the following figure
c. Set redis password
Run the command:
Config set requirepass *
(* the password you want to set). If the setting is successful, the word 'OK'' will be returned.
d. Test connection
Restart the redis service
/ / * the password set for you redis-cli-h 127.0.0.1-p 6379-a *
Alternatively, enter redis-cli to enter command mode and log in using auth'* (the password set for you).
9. Enable the external network to access redisa. Configure the firewall: # Open port 6379 firewall-cmd-- zone=public-- add-port=6379/tcp-- permanent# restart the firewall to make the configuration effective immediately systemctl restart firewalld# view all open ports of the system firewall-cmd-- zone=public-- list-portsb. If it is still not remotely accessible:
At this time, although the firewall has opened port 6379, the external network is still inaccessible, because redis listens to 127.0.0.1 6379 and does not listen for requests from the external network.
Comment out bind 127.0.0.1 before the redis.conf configuration file in the folder directory.
Command: after redis-cli connects to redis, whether both config get daemonize and config get protected-mode are no, if not, use config set to configure the name attribute to no.
Other redis-related common commands uninstall redis:# stop redispkill redis # Delete installation directory rm-rf / usr/local/redis # Delete all redis-related command scripts rm-rf / usr/bin/redis-* # Delete redis decompression folder rm-rf / root/download/redis-4.0.4 reported an exception, try to restart
Reason: Redis has been started
Solution: turn off Redis and restart it
Redis-cli shutdown redis-server &
Then you can see Redis running happily.
The client detects whether the connection is normal.
Use the redis-cli client to detect whether the connection is normal
$redis-cli 127.0.0.1 empty list or set 6379 > keys * (empty list or set) 127.0.0.1 empty list or set 6379 > set key "hello world" OK 127.0.0.1 empty list or set 6379 > get key "hello world" Redis client common operations
Redis is a key-value database that supports five data types: string (string), hash (hash), list (list), set (collection) and zset (sorted set: ordered collection).
When value is of type string, the commands include set get setnx incr del and so on. > set server:name "fido" / / set key value OK > get server:name / / get key value "fido" > setnx connections 10 / / set if not existsOK > incr connections / / Atomic increase value (integer) 11 > incr connections (integer) 12 > del connections / / delete key (integer) 1 > incr connections (integer) 1 when value is list, commands include rpush lpush llen lrange lpop rpop del, etc. > rpush friends "Alice" / / append (integer) 1 > rpush friends "Bob" (integer) 2 > lpush friends "Sam" / / insert to the beginning (integer) 3 > lrange friends 0-1 / / returns a subset of the list Similar slicing operation 1) "Sam" 2) "Alice" 3) "Bob" > lrange friends 0 11) "Sam" 2) "Alice" > lrange friends 1 21) "Alice" 2) "Bob" > llen friends / / return list length (integer) 3 > lpop friends / delete and return the first element of the list "Sam" > rpop friends / / delete and return the last element of the list "Bob" > lrange friends 0-11) "Alice" > del friends / / Delete key (integer) 1 / / 1 indicates success 0 indicates failure when value is of type set, commands include sadd srem sismember smembers sunion del, etc. > sadd superpowers "flight" / / add element (integer) 1 > sadd superpowers "x-ray vision" (integer) 1 > sadd superpowers "reflexes" (integer) 1 > srem superpowers "reflexes" / / delete element 1 > sismember superpowers "flight" / / Test whether the element is in the collection (integer) 1 > sismember superpowers "reflexes" (integer) 0 > smembers superpowers / / returns all elements in the collection 1) "x-ray vision" 2) "flight" "> sadd birdpowers" pecking "(integer) 1 > sadd birdpowers" flight "(integer) 1 > sunion superpowers birdpowers / / merge multiple set Return the merged element list 1) "x-ray vision" 2) "flight" 3) "pecking" > del superpowers / / delete key (integer) 1 when value is of zset type, the command includes zadd zrange del, etc., pay attention to give value a number for sorting. > zadd hacker 1940 "Alan Kay" / / assign a number to value For example, take year 1940 as the number (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 / / return elements in the ordered set 1) "Claude Shannon" 2 ) "Alan Kay" 3) "Richard Stallman" > del hacker / / Delete key (integer) 1 when value is hash type The hash type can be understood as a dictionary, and you need to specify a field to value for mapping, and commands include hset hmset hget hgetall hdel hincrby del, and so on. > hset user:1000 name "John Smith" / / specify a field to value, such as name (integer) 1 > hset user:1000 email "john.smith@example.com" (integer) 1 > hset user:1000 password "s3cret" (integer) 1 > hgetall user:1000 / / get all the members in the hash table Including field and value1) "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" / / set multiple field and valueOK > hget user:1001 name / / obtain value "Mary Jones" > hset user:1000 visits 10 / field according to field can be mapped to numbers The value (integer) 1 > hincrby user:1000 visits 1 / / atomicity increases the value of value Add 1 (integer) 11 > hincrby user:1000 visits 10 / / add 10 (integer) 21 > hdel user:1000 visits / / delete field and its value (integer) 1 > hincrby user:1000 visits 1 (integer) 1 > del user:1000 / / delete key (integer) 1 settings and view the life cycle of key, key expiration will be automatically deleted, commands include expire ttl, etc. > set resource:lock "Redis Demo" OK > expire resource:lock 120 / / set the life cycle to 120s (integer) 1 > ttl resource:lock / / check how much time is left in the current life cycle (integer) 109 > ttl resource:lock / / 120s and then check. Return-2 means expired or does not exist (integer)-2 > set resource:lock "Redis Demo 2" OK > ttl resource:lock / / return-1 means never expire (integer)-1 comparison Simply return a String type?
Change it to upload_list8888, and the page call failed.
Change back to upload_list
The second match of ThreadLocal and redis solves the thread safety problem caused by the shared access of the redise thread pool.
For example, now everyone in the company has to fill out a form, but there is only one pen. At this time, only after the individual has finished using it, the next person can use it. In order to ensure the availability of the "pen" resource, you only need to ensure the order in which everyone gets it. This is the function of lock. When this pen is used by someone else, I add lock. When you get here, queue up for resources (unfair way to say otherwise), release lock after using this pen, and then give it to the next person in order.
But it is perfectly possible for each person to have a pen. In that case, you fill out your form and I will fill out my form. Neither of us will delay the other. This is what ThreadLocal is doing, because each Thread has a copy, there is no resource competition, so there is no need to lock, this is to trade space for time!
To solve the problem that the redise thread pool object (pen) cannot be shared by multiple threads (multiple people), through the threadLocal.set () method, the instance of the redise thread pool object is saved in the threadLocalMap owned by each thread (multiple copies are generated).
In this way, each thread uses its own redisthread pool object instance and does not affect each other, thus isolating each other, thus solving the thread safety problem caused by the shared access of the redise thread pool object.
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 threadLocalJedis = new ThreadLocal () / / Redis server IP private static String ADDR_ARRAY = ReadProper.getResourceValue ("spring.redis.host"); / / Redis port number private static int PORT = Integer.parseInt (ReadProper.getResourceValue ("spring.redis.port")); / / access password private static String AUTH = ReadProper.getResourceValue ("spring.redis.password"); / / maximum number of available connection instances. Default is 8. / / if the value is-1, there is no limit; if maxActive jedis instances have been assigned to pool, the status of pool is exhausted (depleted). Private static int MAX_ACTIVE =-1; / / controls the maximum number of jedis instances of a pool with a state of idle (idle). The default value is also 8. Private static int MAX_IDLE = 16; / / the maximum time to wait for available connections (in milliseconds). The default value is-1, which means that you will never time out. If the waiting time is exceeded, JedisConnectionException; private static int MAX_WAIT = 1000 * 5; / / timeout private static int TIMEOUT = 1000 * 5; / / whether to perform validate operation in advance when borrow a jedis instance; if true, the resulting jedis instances are available Private static boolean TEST_ON_BORROW = true; private static JedisPool jedisPool; / / default database is 0 / * redis expiration time, in seconds * / public final static int EXRP_HOUR = 60 * 60; / / one hour public final static int EXRP_DAY = 60 * 60 * 24; / / one day public final static int EXRP_MONTH = 60 * 60 * 24 * 30 / / one month / * initialize the Redis connection pool. Be sure to initialize the Redis connection pool before use. Generally, initialize it at project startup * / 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 server exception", e);} return jp;} public static void close (Jedis jedis) {if (threadLocalJedis.get () = = null & & jedis! = null) {jedis.close () }} / / * * / / * initialize synchronously in a multithreaded environment / / * / private static synchronized void poolInit () {/ / if (jedisPool = = null) {/ / initialPool () / /} / /} / * to obtain a Jedis instance, be sure to initialize * * @ return Jedis * / public static Jedis getJedis () {boolean success = false; Jedis jedis = null;// if (jedisPool = = null) {/ / poolInit (); / /} int iTun0 While (! success) {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 () + ": the" + I + "acquisition succeeded # @ made use of the local cache redis") Return jedis;}} else {throw new RuntimeException ("redis connection pool initialization failed");}} catch (Exception e) {System.out.println (Thread.currentThread (). GetName () + ":" + I + "acquisition failed!!") Success= false; e.printStackTrace (); _ logger.error ("redis server exception", e);} if (redis server exception null) {success=true } if (I > = 10&&i=20&&i=30&&i=40) {System.out.println ("redis is completely out of touch with ~ (> _)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.