1.下载Redis3.2的镜像
docker pull redis:3.2
2.运行容器
docker run -p 6379:6379 -v $PWD/data:/data -d redis:3.2 redis-server --appendonly yes
带密码启动:
docker run -p 6379:6379 -v $PWD/data:/data -d redis:3.2 redis-server --appendonly yes --requirepass "mypassword"
可以自定义外置配置文件
最后追加redis-server /usr/local/etc/redis/redis.conf
命令说明:
-p 6379:6379 : 将容器的6379端口映射到主机的6379端口
-v $PWD/data:/data : 将主机中当前目录下的data挂载到容器的/data
redis-server --appendonly yes : 在容器执行redis-server启动命令,并打开redis持久化配置
3.连接查看容器
runoob@runoob:~/redis$ docker exec -it 容器id redis-cli
如果有密码docker exec -it 容器id redis-cli -a mypassword
172.17.0.1:6379> info # Server redis_version:3.2.0 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:f449541256e7d446 redis_mode:standalone os:Linux 4.2.0-16-generic x86_64 arch_bits:64 multiplexing_api:epoll
4.springboot 集成redis,pom添加依耐
<!-- redis依赖包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
5.配置application.yml
spring:
#redis配置
redis:
#Redis服务器地址
host: 192.168.1.193
#Redis服务器连接端口
port: 6379
#Redis数据库索引(默认为0)
database: 0
jedis:
pool:
#连接池最大连接数(使用负值表示没有限制)
max-active: 50
#连接池中的最大空闲连接
max-idle: 20
#连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: 3000
#连接池中的最小空闲连接
min-idle: 2
#连接超时时间(毫秒)
timeout: 5000
#Redis密码
password: mypassword
6.编写redis工具类
package com.fengshun.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; /** * redis操作工具类.</br> * (基于RedisTemplate) * @author xcbeyond * 2018年7月19日下午2:56:24 */ @Component public class RedisUtils { @Autowired private RedisTemplate<String, String> redisTemplate; /** * 读取缓存 * * @param key * @return */ public String get(final String key) { return redisTemplate.opsForValue().get(key); } /** * 写入缓存 */ public boolean set(final String key, String value) { boolean result = false; try { redisTemplate.opsForValue().set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 更新缓存 */ public boolean getAndSet(final String key, String value) { boolean result = false; try { redisTemplate.opsForValue().getAndSet(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 删除缓存 */ public boolean delete(final String key) { boolean result = false; try { redisTemplate.delete(key); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } }
7.测试
package com.fengshun.config; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * * @author xcbeyond * 2018年7月19日下午3:08:04 */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class RedisTest { @Resource private RedisUtils redisUtils; /** * 插入缓存数据 */ @Test public void set() { redisUtils.set("redis_key", "redis_vale"); } /** * 读取缓存数据 */ @Test public void get() { String value = redisUtils.get("redis_key"); System.out.println(value); } }
docker 安装Redis 以及 springboot整合redis
原文:https://www.cnblogs.com/418836844qqcom/p/11737781.html