sudo docker pull redis:6.2.2
手动创建redis.conf,并添加如下配置参数。
# 可远程连接
# bind 127.0.0.1
# 解除保护模式
protected-mode no
# 数据持久化
appendonly yes
# 设置密码
requirepass 123456
也可以在网上下载redis配置文件,注意需要修改里面的配置。
wget http://download.redis.io/redis-stable/redis.conf
授权配置文件
chmod 777 redis.conf
sudo docker run -itd \
--name myredis \
-p 6379:6379 \
-v /home/redis/redis.conf:/etc/redis/redis.conf \
-v /home/redis/data:/data \
redis:6.2.2 redis-server /etc/redis/redis.conf
# 进入redis
sudo docker exec -it myredis /bin/bash
# 使用redis
# 参数--raw,表示按原生数据显示,可以解决中文显示乱码,按原始格式打印
redis-cli --raw
# 输入密码
auth 123456
# (1) 键值对
# 创建键值对
set name mason
# 查看键值对
get name
# (2) 列表
# 插入列表
RPUSH user "河南大学"
# 查看列表中所有数据
LRANGE users 0 -1
# (3) hash
# 插入键值对,key:book, field: title, value: "河南大学发展"
HSET book title "河南大学发展"
# 查看键值对
HGET book title
# redis清空所有数据
flushall
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>myredis</artifactId>
<version>1.0.0</version>
<name>myredis</name>
<description>Myredis project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Init the entity -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.1.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
spring:
redis:
host: 192.168.108.100
port: 6379
password: 123456
ConfigRedis.java
package com.example.myredis.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class ConfigRedis {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate(){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 序列化key
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
// 序列化hash
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
// 连接redis数据库
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
RequestController.java
package com.example.myredis.controller;
import com.example.myredis.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Controller
@RequestMapping("/redis")
@ResponseBody
public class RequestController {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@GetMapping(value = "/key")
public String putKey(String key, String value){
this.redisTemplate.opsForValue().set(key,value,30, TimeUnit.SECONDS);
System.out.println(this.redisTemplate.opsForValue().get(key));
return "1";
}
@GetMapping(value = "/object")
public String putObject(){
User user = new User("河南大学", 10);
// 序列化对象
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
this.redisTemplate.opsForValue().set("user", user,30, TimeUnit.SECONDS);
System.out.println(this.redisTemplate.opsForValue().get("user"));
return "1";
}
@GetMapping(value = "/list")
public String putList(){
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
this.redisTemplate.opsForList().leftPush("users", new User("河南大学", 10));
System.out.println(this.redisTemplate.opsForList().range("users", 0, -1));
return "1";
}
@GetMapping(value = "/map")
public String putMap(){
Map<String, String> myMap = new HashMap<>();
myMap.put("name", "河南大学");
myMap.put("age", "20");
this.redisTemplate.opsForHash().putAll("map", myMap);
System.out.println(this.redisTemplate.opsForHash().get("map", "name"));
return "1";
}
}
User.java
package com.example.myredis.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User{
private String name;
private int age;
}
package com.example.myredis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyredisApplication {
public static void main(String[] args) {
SpringApplication.run(MyredisApplication.class, args);
}
}