NET Distributed System five: C # uses Redis cluster cache

Source: Internet
Author: User
Tags rabbitmq redis cluster install redis



This article describes the system cache component, which uses NoSQL Redis as the system cache layer.



I. BACKGROUND



The system takes into account high concurrent usage scenarios. For concurrent submission scenarios, this is addressed through the RABBITMQ component described in the previous section. For the system high concurrency query, in order to provide performance to reduce the database pressure, we join the cache mechanism, can join the cache support at different levels, this article mainly introduces the application service layer and the data layer to add caching mechanism to improve performance. Industry cache components are Redis, Memcached, MemoryCache. The system uses the Redis cache component, some systems use Redis as MQ, this scenario is mainly used in Rabbitmq,redis system cache application.






Ii. introduction of Redis



Redis is an open source Key-value database, written in C, supported by a network, a NoSQL database that can be persisted based on memory, and provides APIs in a variety of languages, such as: Java,c/c++,c#,php,javascript,perl, Object-c,python,ruby and other language drivers. The cluster scenario has been supported since Redis3.0.



The relevant Redis Cluster principle is not introduced here, there is a lot of information on the network.






Third, Redis cluster application



  (a) Introduction to the environment



This system builds Redis3.0 cluster based on the Linux CentOS. Deploy three instance to a single virtual machine and apply to the Windows platform.


Serial number Service IP Description
1 192.168.1.110

Redis Node A port: 7000 (M), 7003 (S)

Redis Node B port: 7001 (M), 7004 (S)

Redis node C port: 7002 (M), 7005 (S)

















  (ii) Installation of Redis



1. Install Dependent tools


[Email protected] Desktop]# yum-y install gcc openssl-devel libyaml-devel libffi-devel readline-devel zlib-devel gdbm-devel N Curses-devel gcc-c++ Automake autoconf


2. Installing Redis 3.0.6


[[Email protected] Desktop]# cd/usr/loacal

[Email protected] loacal]# wget http://download.redis.io/releases/redis-3.0.6.tar.gz

[Email protected] Desktop]# tar xvf redis-3.0.6.tar.gz 

CD redis-3.0.6/

Make MALLOC=LIBC

[[email protected] redis-3.0.6]# make install

 


3, because we use different port numbers to differentiate, in two services each establish a port-named folder. Configure the 7000 node service to copy Redis-server and redis.conf to/etc/redis/7000


[[Email protected] Desktop]# mkdir/etc/redis/7000

[[Email protected] Desktop]# mkdir/etc/redis/7001

[[Email protected] Desktop]# mkdir/etc/redis/7002

[[Email protected] Desktop]# mkdir/etc/redis/7003

[[Email protected] Desktop]# mkdir/etc/redis/7004

[[Email protected] Desktop]# mkdir/etc/redis/7005

[Email protected] 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

[Email protected] 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



4. Modify the startup script of the Redis service, modify the content, and copy the related other node configuration


[[email protected] redis-3.0.6]# cp /usr/local/redis-3.0.6/utils/redis_init_script /etc/init.d/redis7000
[[email protected] 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"


[[email protected] redis-3.0.6]# cp /etc/init.d/redis7000 /etc/init.d/redis7001
[[email protected] redis-3.0.6]# cp /etc/init.d/redis7000 /etc/init.d/redis7003
[[email protected] redis-3.0.6]# cp /etc/init.d/redis7000 /etc/init.d/redis7004


5. Set to boot server


[[email protected] redis-3.0.6]# chkconfig redis7000 on
[[email protected] redis-3.0.6]# chkconfig redis7001 on
[[email protected] redis-3.0.6]# chkconfig redis7003 on
[[email protected] redis-3.0.6]# chkconfig redis7004 on


6, restart the system, and check the redis7000,redis7001,redis7003,redis7004 service situation


[email protected] redis-3.0.6]# reboot
[Email protected] Desktop]# systemctl status Redis7004.service





  (iii) Configuring a Redis cluster



1. Follow the ruby Tree tool because the Redis cluster requires ruby


[Email protected] redis-3.0.6]# yum-y install TCL Ruby Tree
[[Email protected] Desktop]# 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



2. REDIS-TRIB.RB Configuration Cluster


[[email protected] Desktop] # /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.


3. Check cluster status


[Email protected] Desktop]#/usr/local/redis-3.0.6/src/redis-trib.rb check  192.168.1.110:7000


4, if the error, by the following command line repair


[[Email protected] Desktop]#/usr/local/redis-3.0.6/src/redis-trib.rb fix 192.168.1.110:7000


5, firewall open port, and restart the firewall


[[email protected] desktop] # firewall-cmd --zone = public --add-port = 7000-7005 / tcp --permanent
success
[[email protected] desktop] # firewall-cmd --reload
success


6, check the cluster situation


[[email protected] desktop] # 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





Iv. Description of Use



(i) test the cache with the command line









  (ii) test caching through C # code



1. Cache data to a clustered Redis service by using the Stackexchang.redis component.


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


2. Get data through 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 : "";


3. Redis Cache condition












V. Summary



The Redis cluster supports HA by default, but for a single instance failure, the user needs to determine the mechanism of the processing itself, and the follow-up is pending, but Redis Codis provides convenient support.








Author: Andon
Source: Http://www.cnblogs.com/Andon_liu
About the focus on Microsoft Platform Project architecture, management. Familiar with design patterns, domain drivers, architecture design, agile development and project management. is mainly engaged in ASP, Wcf/web API, SOA, MSSQL, Redis aspects of project development, architecture, management work. If you have questions or suggestions, please learn to discuss together!
This article is copyrighted by the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link.
If you have questions, you can email: [Email protected] contact me, thank you.





NET Distributed System five: C # uses Redis cluster cache


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.