Net分佈式系統之五:C#使用Redis集羣緩存

  本文介紹系統緩存組件,採用NOSQL之Redis做爲系統緩存層。node

1、背景c++

  系統考慮到高併發的使用場景。對於併發提交場景,經過上一章節介紹的RabbitMQ組件解決。對於系統高併發查詢,爲了提供性能減小數據庫壓力,咱們加入緩存機制,能夠不一樣層次加入緩存支持,本文主要介紹應用服務層和數據層之間加入緩存機制提高性能。業界緩存組件有Redis、Memcached、MemoryCache。本系統採用Redis緩存組件,有些系統將Redis看成MQ使用,此場景本系統用RabbitMQ,Redis主要用於系統緩存應用。redis


 

2、Redis簡介數據庫

  Redis是一個開源的Key-Value數據庫,使用C語言編寫、支持網絡、可基於內存亦可持久化的NOSQL數據庫,並提供多種語言的API,例如:Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby等語言驅動。自Redis3.0開始支持集羣方案。vim

   相關Redis Cluster 原理在此很少介紹,網絡上有不少資料。windows


 

3、Redis集羣應用centos

  (一)環境介紹設計模式

    本系統基於Linux之CentOS搭建Redis3.0集羣。將三個Instance部署於一臺虛擬機,應用部署於windows平臺。緩存

序號 服務IP 說明
1 192.168.1.110

Redis節點A端口:7000(M),7003(S)ruby

Redis節點B端口:7001(M),7004(S)

Redis節點C端口:7002(M),7005(S)

  

 

 

 

  

  (二)安裝Redis

  一、安裝相關依賴工具

[root@andoncentos 桌面]# yum -y install gcc openssl-devel libyaml-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel gcc-c++ automake autoconf

  二、安裝Redis 3.0.6

[root@andoncentos 桌面]# cd /usr/loacal
[root@andoncentos loacal]# wget http://download.redis.io/releases/redis-3.0.6.tar.gz
[root@andoncentos 桌面]# tar xvf redis-3.0.6.tar.gz
[root@andoncentos 桌面]# cd redis-3.0.6/
[root@andoncentos 桌面]# make MALLOC=libc
[root@andoncentos redis-3.0.6]# make install

  三、因爲咱們使用不一樣端口號區分,在兩個服務各自創建以端口命名的文件夾。配置7000節點服務,將redis-server和redis.conf複製到/etc/redis/7000

[root@andoncentos 桌面]# mkdir /etc/redis/7000
[root@andoncentos 桌面]# mkdir /etc/redis/7001
[root@andoncentos 桌面]# mkdir /etc/redis/7002
[root@andoncentos 桌面]# mkdir /etc/redis/7003
[root@andoncentos 桌面]# mkdir /etc/redis/7004
[root@andoncentos 桌面]# mkdir /etc/redis/7005
[root@andoncentos redis-3.0.6]# cp /usr/local/redis-3.0.6/src/redis-server /usr/local/redis-3.0.6/redis.conf /etc/redis/7000
[root@andoncentos redis-3.0.6]# vim /etc/redis/7000/redis.conf

  port 7000
  daemonize yes 
  pidfile /var/run/redis_7000.pid
  cluster-enabled yes
  cluster-config-file nodes.conf
  logfile "/var/log/redisd7000.log"
  dir /etc/redis/7000/
  cluster-node-timeout 5000
  appendonly yes

  四、修改redis服務的啓動腳本,修改內容,並複製相關其餘的節點配置

[root@andoncentos redis-3.0.6]# cp /usr/local/redis-3.0.6/utils/redis_init_script /etc/init.d/redis7000
[root@andoncentos redis-3.0.6]# vim /etc/init.d/redis7000

#!/bin/sh
# chkconfig 2345 90 10
# description:Redis is a persistent key-value database
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=7000
# EXEC=/usr/local/bin/redis-server
EXEC=/etc/redis/${REDISPORT}/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}/redis.conf"

[root@andoncentos redis-3.0.6]# cp /etc/init.d/redis7000 /etc/init.d/redis7001
[root@andoncentos redis-3.0.6]# cp /etc/init.d/redis7000 /etc/init.d/redis7003
[root@andoncentos redis-3.0.6]# cp /etc/init.d/redis7000 /etc/init.d/redis7004

  五、設置爲開機自啓動服務器

[root@andoncentos redis-3.0.6]# chkconfig redis7000 on
[root@andoncentos redis-3.0.6]# chkconfig redis7001 on
[root@andoncentos redis-3.0.6]# chkconfig redis7003 on
[root@andoncentos redis-3.0.6]# chkconfig redis7004 on

  六、重啓系統,並檢查redis7000,redis7001,redis7003,redis7004服務狀況

[root@andoncentos redis-3.0.6]# reboot
[root@andoncentos 桌面]# systemctl status redis7004.service

 

  (三)配置Redis集羣

  一、按照 ruby tree 工具,由於redis集羣須要ruby

[root@andoncentos redis-3.0.6]# yum -y install tcl ruby tree
[root@andoncentos 桌面]# gem install redis --version 3.0.6

Fetching: redis-3.0.6.gem (100%)
Successfully installed redis-3.0.6
Parsing documentation for redis-3.0.6
Installing ri documentation for redis-3.0.6
1 gem installed

  二、redis-trib.rb 配置集羣

[root@andoncentos 桌面]# /usr/local/redis-3.0.6/src/redis-trib.rb create --replicas 1 192.168.1.110:7000 192.168.1.110:7001 192.168.1.110:7002 192.168.1.110:7003 192.168.1.110:7004 192.168.1.110:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.110:7000
192.168.1.110:7001
192.168.1.110:7002
Adding replica 192.168.1.110:7003 to 192.168.1.110:7000
Adding replica 192.168.1.110:7004 to 192.168.1.110:7001
Adding replica 192.168.1.110:7005 to 192.168.1.110:7002
M: b164701893bfbdc078e2f7e3b16f1216c1bf65ff 192.168.1.110:7000
   slots:0-5460 (5461 slots) master
M: 4c2d36c55cff692a7bbeccb663197b555747d15d 192.168.1.110:7001
   slots:5461-10922 (5462 slots) master
M: b147e4dfcd63c5ce059540db55a9d7cb9fa093eb 192.168.1.110:7002
   slots:10923-16383 (5461 slots) master
S: 757381aa5cc5c8ba70f3798f6de6cb7b2e97f924 192.168.1.110:7003
   replicates b164701893bfbdc078e2f7e3b16f1216c1bf65ff
S: fecc8edf32fc72cd4a5d8ae5306fe4083abfe8e9 192.168.1.110:7004
   replicates 4c2d36c55cff692a7bbeccb663197b555747d15d
S: 98bd8e1aff631a3bee7f92a39764decea16ee955 192.168.1.110:7005
   replicates b147e4dfcd63c5ce059540db55a9d7cb9fa093eb
Can I set the above configuration? (type 'yes' to accept): yes           
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join....
>>> Performing Cluster Check (using node 192.168.1.110:7000)
M: b164701893bfbdc078e2f7e3b16f1216c1bf65ff 192.168.1.110:7000
   slots:0-5460 (5461 slots) master
M: 4c2d36c55cff692a7bbeccb663197b555747d15d 192.168.1.110:7001
   slots:5461-10922 (5462 slots) master
M: b147e4dfcd63c5ce059540db55a9d7cb9fa093eb 192.168.1.110:7002
   slots:10923-16383 (5461 slots) master
M: 757381aa5cc5c8ba70f3798f6de6cb7b2e97f924 192.168.1.110:7003
   slots: (0 slots) master
   replicates b164701893bfbdc078e2f7e3b16f1216c1bf65ff
M: fecc8edf32fc72cd4a5d8ae5306fe4083abfe8e9 192.168.1.110:7004
   slots: (0 slots) master
   replicates 4c2d36c55cff692a7bbeccb663197b555747d15d
M: 98bd8e1aff631a3bee7f92a39764decea16ee955 192.168.1.110:7005
   slots: (0 slots) master
   replicates b147e4dfcd63c5ce059540db55a9d7cb9fa093eb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

  三、檢查集羣狀態

[root@andoncentos 桌面]# /usr/local/redis-3.0.6/src/redis-trib.rb check  192.168.1.110:7000

  四、若出錯的話,經過以下命令行修復

[root@andoncentos 桌面]# /usr/local/redis-3.0.6/src/redis-trib.rb fix 192.168.1.110:7000

  五、防火牆開放端口,並重啓防火牆

[root@andoncentos 桌面]# firewall-cmd --zone=public --add-port=7000-7005/tcp --permanent
success
[root@andoncentos 桌面]# firewall-cmd --reload
success

  六、檢查集羣狀況

[root@andoncentos 桌面]# redis-cli -c -p 7000
127.0.0.1:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:2492
cluster_stats_messages_received:2492

 

4、使用說明

   (一)使用命令行測試緩存

  

 

  (二)經過C#代碼測試緩存

  一、經過使用stackexchang.redis組件,將數據緩存到集羣redis服務。

var cfg = RedisCachingSectionHandler.GetConfig();
var serializer = new NewtonsoftSerializer();
var redis = new StackExchangeRedisCacheClient(serializer, cfg);

var cls = new Cls(){ ID = 1, Name = txt };
string key = "tkey" + new Random().Next(1000, 9999).ToString();
redis.Add<Cls>(key, cls);

  二、經過key獲取數據

var cfg = RedisCachingSectionHandler.GetConfig();
var serializer = new NewtonsoftSerializer();
var redis = new StackExchangeRedisCacheClient(serializer, cfg);
var entity =  redis.Get<Cls>(key);
 ViewBag.KV = entity != null ? entity.Name : "";

  三、redis緩存狀況

 

 


 

5、總結

   redis cluster 默認支持HA,可是對於單個Instance故障,使用者須要自行判斷處理的機制,後續有待於研究,可是redis codis 這方面提供了方便的支持。

 

 

做者:劉蔡濤
出處: http://www.cnblogs.com/Andon_liu 關於做者:專一於微軟平臺項目架構、管理。熟悉設計模式、領域驅動、架構設計、敏捷開發和項目管理。現主要從事ASP.NET MVC、WCF/Web API、SOA、MSSQL、redis方面的項目開發、架構、管理工做。 若有問題或建議,請一塊兒學習討論! 本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。 若有問題,能夠郵件:568773262@qq.com 聯繫我,謝謝。
相關文章
相關標籤/搜索