在Spring Boot項目中使用Redis集羣

Redis安裝

Mac 系統安裝Redis

brew方式安裝

在命令汗執行命令node

brew install redis

安裝完成以後的提示信息redis

==> Downloading https://homebrew.bintray.com/bottles/redis-5.0.2.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring redis-5.0.2.mojave.bottle.tar.gz
==> Caveats
To have launchd start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  redis-server /usr/local/etc/redis.conf
==> Summary
  /usr/local/Cellar/redis/5.0.2: 13 files, 3.1MB

啓動redis服務測試

brew services start redis

啓動後提示信息spa

==> Successfully started `redis` (label: homebrew.mxcl.redis)

下面咱們來使用redis內置的客戶端來訪問redisrest

$ src/redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"

下面咱們就能夠操做了redis了。code

若是要中止redis服務,能夠執行命令server

brew services stop redis

重啓redis服務,能夠執行命令進程

brew services restart redis

若是須要卸載redis,能夠執行homebrew

brew uninstall redis

查看HomeBrew安裝的所有服務能夠使用命令get

brew services list

配置文件在/usr/local/etc目錄下

源代碼編譯安裝

1. 下載

$ wget http://download.redis.io/releases/redis-5.0.3.tar.gz

2. 解壓

$ tar xzf redis-5.0.3.tar.gz
$ cd redis-5.0.3
$ make

3. 跳轉到redis目錄編譯

$ cd redis-5.0.3 && make

若是須要跑redis的測試,能夠執行

$ make test

到如今咱們就編譯好了redis了。接下來,咱們把編譯的文件放到redis-5.0.3/bin目錄下,把配置文件移動到redis-5.0.3/config目錄下

$ mkdir bin && cd src && mv redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server redis-trib.rb ../bin/
$ cd ../ && mkdir config && mv *.conf config/

接下來啓動redis服務

$ bin/redis-server

如今咱們能夠啓動redis的客戶端來檢查是不是肯定服務正常。

$ bin/redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"

集羣配置

咱們這裏配置官方推薦的三主三從模式的集羣配置。這裏使用的是採用源代碼編譯方式安裝的版本。

首先,咱們修改配置文件reids.conf,主要修改如下屬性:

port 6380                           // 節點端口
daemonize yes                       // 配置redis做爲守護進程運行,默認狀況下,redis不是做爲守護進程運行的
cluster-enabled yes                 // 是否開啓集羣
cluster-config-file nodes-6380.conf // 該節點的對應的節點配置文件
cluster-node-timeout 15000          // 集羣超時時間
pidfile /var/run/redis_6380.pid     // 當Redis以守護進程方式運行時,Redis默認會把pid寫入/var/run/redis.pid文件,能夠經過pidfile指定

咱們將文件另存爲redis-6380.conf,並將該文件複製5份,分別修改文件中的6380爲6381,6382,6383,6384,6385。

這樣咱們就有以下六個配置文件。

redis-6380.conf
redis-6381.conf
redis-6382.conf
redis-6383.conf
redis-6384.conf
redis-6385.conf

其中redis-6380.conf,redis-6381.conf,redis-6382.conf文件爲主節點的配置文件,redis-6383.conf,redis-6384.conf,redis-6385.conf爲從節點的配置文件。

下面咱們執行如下命令

$ mkdir logs
$ nohup bin/redis-server ./config/redis-6380.conf >> logs/redis-6380.log 2>&1 &
$ nohup bin/redis-server ./config/redis-6381.conf >> logs/redis-6381.log 2>&1 &
$ nohup bin/redis-server ./config/redis-6382.conf >> logs/redis-6382.log 2>&1 &
$ nohup bin/redis-server ./config/redis-6383.conf >> logs/redis-6383.log 2>&1 &
$ nohup bin/redis-server ./config/redis-6384.conf >> logs/redis-6384.log 2>&1 &
$ nohup bin/redis-server ./config/redis-6385.conf >> logs/redis-6385.log 2>&1 &

以後運行命令

./bin/redis-cli --cluster create 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 --cluster-replicas 1

當提示

Can I set the above configuration?(type 'yes' to accept):

時輸入 yes 。至此集羣就搭建完畢了。

Spring Boot項目中配置redis集羣

將Spring boot項目中的redis配置修改成:

redis:
    database: 0
    cluster:
        nodes: 127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384,127.0.0.1:6385
        max-redirects: 2

到這裏就能夠在項目中訪問Redis集羣了

相關文章
相關標籤/搜索