docker 安装Redis 以及 springboot整合redis
1.下载Redis3.2的镜像
docker pull redis:3.2
2.创建配置文件
# 如果不想开启RDB,就是配置成 save ""
#900秒内变更1次才触发bgsave
save 900 1 save 300 10 save 60 10000
#rdb保存的文件名
dbfilename dump.rdb
#就是存放我们RDB备份文件的目录 dir /data #yes:如果save过程出错了则停止Redis写操作 #no:没所谓save是否出错 stop-writes-on-bgsave-error yes #开启RDB压缩 rdbcompression yes #进行CRC64算法校验,有10%的性能损耗 rdbchecksum yes
3.运行容器
docker run -p 6379:6379 --name redis6379 -v /root/redis6379/data:/data -v /root/redis6379/redis.conf:/etc/redis/redis.conf -d redis:3.2 redis-server /etc/redis/redis.conf --appendonly yes
命令说明:
-p 6379:6379 : 将容器的6379端口映射到主机的6379端口
-v $PWD/data:/data : 将主机中当前目录下的data挂载到容器的/data
redis-server --appendonly yes : 在容器执行redis-server启动命令,并打开redis持久化配置
4.连接查看容器
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
5.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>
6.配置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
7.编写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;
    }
}
8.测试
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);
    }
}
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号