1. 程式人生 > >redis安裝配置測試

redis安裝配置測試

一、redis是什麼?

 1、redis簡介

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster

  redis是一個開源的(BSD協議),記憶體資料結構儲存應用。可以被用來作為資料庫,快取,訊息代理,它支援字串,雜湊,列表,集合以及有序集合等五種資料結構對於範圍查詢,點陣圖對映,日誌,地理空間索引的位置查詢和流式資料結構。redis有內部功能包含複製,LUA指令碼,LRU驅逐,事務和不同級別磁碟儲存,並且提供了高可用性通過 Redis Sentinel,提供自動分割槽通過Redis Cluster。

2、redis與memached的區別

      2.1、redis擁有持久化功能而meamched沒有,所有redis可以在儲存(快取,儲存),而meamched只能用作快取。

      2.2、redis儲存的資料結構很豐富有五種,分別是字串,集合,列表,雜湊,有序集合。而meamched只支援字串。

   核心的區別在於這兩點,詳細的資訊可以參考:https://www.cnblogs.com/tuyile006/p/6382062.html

二、redis的安裝

     1、下載redis 

#進入存放目錄
cd /usr/local/src

#下載
wget http://download.redis.io/releases/redis-5.0.2.tar.gz

#建立解壓目錄,並在該目錄下進行操作
mkdir /usr/local/redis

cd /usr/local/redis

#解壓
tar -xvf ../src/redis-5.0.2.tar.gz

#解壓的時候出現,liunx系統的時間和redis的confige時間有衝突
#編譯
make

#最好進行測試檢視是否編譯正確
make  test


#中間出現(可能出現: need tcl  >8.4這種情況, yum install tcl)
yum install tcl

#安裝
make  PREFIX=/usr/local/redis install


2、 redis 安裝後的目錄

     redis-benchmark  效能測試工具

     redis-check-aof  日誌檔案檢測工(比如斷電造成日誌損壞,可以檢測並修復)

    redis-check-dump  快照檔案檢測工具,效果類上

    redis-cli  客戶端

    redis-sentinel -> redis-server    Sentinel(哨崗、哨兵)是redis的高可用性解決方案

    redis-server 服務

三、redis的測試

  1、redis的服務端和客戶端連線測試

       複製redis.conf到目錄下,啟動服務使用該配置

#啟動 redis-server
./bin/redis-server ./redis.conf 
#啟動客戶端,測試是否可以連線服務,並做一些測試

[[email protected] redis]# ./bin/redis-cli 
#連線成功
127.0.0.1:6379> set name "xieqx"
OK
127.0.0.1:6379> get name
"xieqx"
127.0.0.1:6379> 

讓redis以後臺程序的形式執行

編輯conf配置檔案,修改如下內容;

daemonize yes

 2、使用java連線redis服務端(使用springBoot+redis)

    2.1、mven新增依賴

     <!--  redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

    2.2、配置

#redis配置
# REDIS (RedisProperties)
# Redis資料庫索引(預設為0)
spring.redis.database=0  
# Redis伺服器地址
spring.redis.host=192.168.47.137
# Redis伺服器連線埠
spring.redis.port=6379  
# Redis伺服器連線密碼(預設為空)
spring.redis.password=
# 連線池最大連線數(使用負值表示沒有限制)
#spring.redis.pool.max-active=8
# 連線池最大阻塞等待時間(使用負值表示沒有限制)
#spring.redis.pool.max-wait=-1
# 連線池中的最大空閒連線
#spring.redis.pool.max-idle=8
# 連線池中的最小空閒連線
#spring.redis.pool.min-idle=0
# 連線超時時間(毫秒)
#spring.redis.time

   2.3、測試demo

public class RedisDemoTest {

    @Autowired
    private RedisTemplate redisTemplate;


    //對於字串的操作
    @Test
    public void testAdd(){
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();

        //寫入快取
        valueOperations.set("name","無敵破壞王");

        //獲取欄位值
        String value = valueOperations.get("name");

        log.info("name : {}",value);


        redisTemplate.delete("name");

        value = valueOperations.get("name");

        log.info("name : {}",value);

    }
}