“This is the third day of my participation in the Gwen Challenge.
Download and install
IO/Download Version redis-5.0.5 is used in this article.
1. Upload it to the server and decompress it
#Pre installation
yum install -y gcc-c++
#Unpack theTar -zxvf redis-5.0.5.tar.gz CD redis-5.0.5#To viewll -rw-rw-r--. 1 root root 106874 May 16 2019 00-RELEASENOTES -rw-rw-r--. 1 root root 53 May 16 2019 BUGS -rw-rw-r--. 1 root root 2381 May 16 2019 CONTRIBUTING -rw-rw-r--. 1 root root 1487 May 16 2019 COPYING drwxrwxr-x. 6 root root 192 Apr 30 06:58 deps -rw-rw-r--. 1 root root 11 May 16 2019 INSTALL -rw-rw-r--. 1 root root 151 May 16 2019 Makefile -rw-rw-r--. 1 root root 6888 May 16 2019 MANIFESTO -rw-rw-r--. 1 root root 20555 May 16 2019 README.md -rw-rw-r--. 1 root root 61797 May 16 2019 redis.conf -rwxrwxr-x. 1 root root 275 May 16 2019 runtest -rwxrwxr-x. 1 root root 280 May 16 2019 runtest-cluster -rwxrwxr-x. 1 root root 341 May 16 2019 runtest-moduleapi -rwxrwxr-x. 1 root root 281 May 16 2019 runtest-sentinel -rw-rw-r--. 1 root root 9710 May 16 2019 sentinel.conf drwxrwxr-x. 3 root root 8192 Apr 30 06:59 src drwxrwxr-x. 11 root root 182 May 16 2019 tests drwxrwxr-x. 8 root root 4096 May 16 2019 utils
#compile
make
#The installationmake install ll /usr/local/bin -rwxr-xr-x. 1 root root 4366568 Apr 30 06:59 redis-benchmark -rwxr-xr-x. 1 root root 8111808 Apr 30 06:59 redis-check-aof -rwxr-xr-x. 1 root root 8111808 Apr 30 06:59 redis-check-rdb -rwxr-xr-x. 1 root root 4806792 Apr 30 06:59 redis-cli lrwxrwxrwx. 1 root root 12 Apr 30 06:59 redis-sentinel -> redis-server -rwxr-xr-x. 1 root root 8111808 Apr 30 06:59 redis-serverCopy the code
It is recommended to use a newer version of redis. Some older versions of Redis may fail to compile during installation, and other libraries need to be installed.
2. Go to the utils folder in the redis directory and copy the startup script redis_init_script to /etc/init.d
cd utils ll -rw-rw-r--. 1 root root 593 May 16 2019 build-static-symbols.tcl -rw-rw-r--. 1 root root 1303 May 16 2019 cluster_fail_time.tcl -rw-rw-r--. 1 root root 1098 May 16 2019 corrupt_rdb.c drwxrwxr-x. 2 root root 60 May 16 2019 create-cluster -rwxrwxr-x. 1 root root 2149 May 16 2019 generate-command-help.rb drwxrwxr-x. 3 root root 31 May 16 2019 graphs drwxrwxr-x. 2 root root 39 May 16 2019 hashtable drwxrwxr-x. 2 root root 70 May 16 2019 hyperloglog -rwxrwxr-x. 1 root root 9567 May 16 2019 install_server.sh drwxrwxr-x. 2 root root 63 May 16 2019 lru -rw-rw-r--. 1 root root 1277 May 16 2019 redis-copy.rb -rwxrwxr-x. 1 root root 1352 May 16 2019 redis_init_script -rwxrwxr-x. 1 root root 1047 May 16 2019 redis_init_script.tpl -rw-rw-r--. 1 root root 1762 May 16 2019 redis-sha1.rb drwxrwxr-x. 2 root root 135 May 16 2019 releasetools -rwxrwxr-x. 1 root root 3787 May 16 2019 speed-regression.tcl -rwxrwxr-x. 1 root root 693 May 16 2019 whatisdoing.sh cp redis_init_script /etc/init.d/Copy the code
3. Save the configuration file separately
Mkdir /usr/local/redis -p mkdir /usr/local/redis/db -p cp /opt/soft/redis-5.0.5/redis. Conf /usr/local/redis/Copy the code
4. Modify the configuration file
vim /usr/local/redis/redis.conf
Copy the code
The following are some of my modifications, which can be modified according to the actual situation
Bind 0.0.0.0 requirepass redis1234 dir /usr/local/redis/db daemonize yesCopy the code
5. Modify the redis_init_script startup script
vim /etc/init.d/redis_init_script #! /bin/sh # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. ### BEGIN INIT INFO # Provides: redis_6379 # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Redis data structure server # Description: See https://redis.io ### END INIT INFO #chkconfig: 22345 10 90 #description: Start and Stop redis REDISPORT=6379 EXEC=/usr/local/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}. Pid # Configuration file location CONF = "/ usr/local/redis/redis CONF" case "$1" in the start) if [-f $PIDFILE] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esacCopy the code
Modify the permission of the startup script and enable the automatic startup
chmod 777 redis_init_script
chkconfig redis_init_script on
Copy the code
Since then, redis installation and boot since the start of the configuration is completed, the next integration of SpringBoot actual combat.
Integrated SpringBoot
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.3. RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<! -- Demo introduction -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Copy the code
Start the class
@SpringBootApplication
public class RedisApplication {
public static void main(String[] args) { SpringApplication.run(RedisApplication.class, args); }}Copy the code
The configuration file
spring:
redis:
host: 192.16879.120.
port: 6379
password: redis1234
database: 0
Copy the code
Utility classes for redis operations
@Component
public class RedisUtil {
private final StringRedisTemplate redisTemplate;
public RedisUtil(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
// Key, a simple key-value operation
/** * TTL key, in seconds, returns the TTL (time to live) of a given key. * *@param key
* @return* /
public long ttl(String key) {
return redisTemplate.getExpire(key);
}
Expire sets the expiration time, in seconds **@param key
* @return* /
public void expire(String key, long timeout) {
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
}
/** * INCR key **@param key
* @return* /
public long incr(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
/** * find all KEYS that match the given pattern */
public Set<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
/** * delete a key **@param key
*/
public void del(String key) {
redisTemplate.delete(key);
}
// String (String)
/** * SET key value, SET a key-value (string value associated with key) **@param key
* @param value
*/
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
/** * SET key value EX seconds to SET the key-value and timeout duration **@param key
* @param value
* @paramTimeout (in seconds) */
public void set(String key, String value, long timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
/** * GET key, return the string value associated with key. * *@param key
* @return value
*/
public String get(String key) {
return (String) redisTemplate.opsForValue().get(key);
}
/** * Mget **@param keys
* @return* /
public List<String> mget(List<String> keys) {
return redisTemplate.opsForValue().multiGet(keys);
}
/** *; /** *@param keys
* @return* /
public List<Object> batchGet(List<String> keys) {
// nginx -> keepalive
// redis -> pipeline
List<Object> result = redisTemplate.executePipelined(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
StringRedisConnection src = (StringRedisConnection) connection;
for (String k : keys) {
src.get(k);
}
return null; }});return result;
}
// Hash table
/** * HSET key field value = value **@param key
* @param field
* @param value
*/
public void hset(String key, String field, Object value) {
redisTemplate.opsForHash().put(key, field, value);
}
/** * HGET key field returns the value of field ** in the hash table key@param key
* @param field
* @return* /
public String hget(String key, String field) {
return (String) redisTemplate.opsForHash().get(key, field);
}
/** * HDEL key field [field...] Delete one or more specified fields in the hash table key. Non-existing fields are ignored. * *@param key
* @param fields
*/
public void hdel(String key, Object... fields) {
redisTemplate.opsForHash().delete(key, fields);
}
/** * HGETALL key returns all fields and values in the hash table key. * *@param key
* @return* /
public Map<Object, Object> hgetall(String key) {
return redisTemplate.opsForHash().entries(key);
}
// List ()
/** * LPUSH key value, insert a value into the list key header **@param key
* @param value
* @returnThe length of the list after executing the LPUSH command. * /
public long lpush(String key, String value) {
return redisTemplate.opsForList().leftPush(key, value);
}
/** * LPOP key removes and returns the header element of the list key. * *@param key
* @returnThe header element of the list key. * /
public String lpop(String key) {
return (String) redisTemplate.opsForList().leftPop(key);
}
/** * RPUSH key value insert a value at the end of the list key (rightmost). * *@param key
* @param value
* @returnThe length of the list after executing the LPUSH command. * /
public long rpush(String key, String value) {
returnredisTemplate.opsForList().rightPush(key, value); }}Copy the code
Write control classes and test Redis
@RestController
@RequestMapping("test")
public class TestRedisController {
@Autowired
private RedisUtil redisUtil;
@GetMapping("add")
public void add(@RequestParam("key") String key, @RequestParam("value") String value) {
redisUtil.set(key, value);
}
@GetMapping("list")
public Object listAllKeys(a){
Set<String> keys = redisUtil.keys("*");
returnkeys; }}Copy the code
From there, the integration is complete. More operations in RedisUtil will not be written out.