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);
    }

}

  https://github.com/Blank-mind/token-authentication-example


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM