Don saita Redis Replication kuma High Availability a cikin NodeJS, bi waɗannan matakan:
Shigar Redis
Da farko, kuna buƙatar shigarwa Redis akan sabar ku. Kuna iya saukar da shi daga gidan yanar gizon hukuma ko amfani da mai sarrafa fakiti kamar dacewa ko sha.
Sanya Redis don Replication
Bude Redis fayil ɗin sanyi(redis.conf) kuma yi canje-canje masu zuwa:
# Enable replication
replicaof <master_ip> <master_port>
Sauya <master_ip>
kuma <master_port>
tare da adireshin IP da tashar jiragen ruwa na Redis uwar garken uwar garken.
Tart Redis Replicas
Fara Redis lokuta da yawa akan sabar ko tashar jiragen ruwa daban-daban, waɗanda zasu yi aiki azaman kwafin maigidan. Yi amfani da fayil ɗin sanyi iri ɗaya Redis don kowane misali.
Yi amfani Redis Client da NodeJS
A cikin aikace-aikacen NodeJS ɗinku, yi amfani da Redis ɗakin karatu na abokin ciniki kamar "ioredis" don haɗawa da Redis misalin. Abokin ciniki zai yi amfani da buƙatun gazawar ta atomatik zuwa uwar garken da ta dace.
Misalin haɗawa da Redis "ioredis" a cikin NodeJS:
const Redis = require('ioredis');
const redis = new Redis({
sentinels: [{ host: 'sentinel_ip', port: sentinel_port }],
name: 'mymaster',
role: 'slave',
});
Sauya 'sentinel_ip'
kuma sentinel_port
tare da adireshin IP da tashar jiragen ruwa na Redis Sentinel uwar garken, wanda ke sa ido kan maigidan kuma yana sarrafa gazawar.
Saka idanu Redis Sentinel
Redis Sentinel ke da alhakin sa ido akan Redis abubuwan da kuma magance gazawar. Shigar kuma saita Redis Sentinel akan uwar garken daban, kuma ƙara cikakkun bayanansa a cikin aikace-aikacen NodeJS.
Misalin haɗawa da Redis Sentinel "ioredis" a cikin NodeJS:
const Redis = require('ioredis');
const sentinel = new Redis({
sentinels: [
{ host: 'sentinel1_ip', port: sentinel1_port },
{ host: 'sentinel2_ip', port: sentinel2_port },
// Add more Sentinel servers if needed
],
name: 'mymaster',
});
const redis = new Redis({
sentinels: [
{ host: 'sentinel1_ip', port: sentinel1_port },
{ host: 'sentinel2_ip', port: sentinel2_port },
// Add more Sentinel servers if needed
],
name: 'mymaster',
});
Sauya 'sentinel1_ip'
, sentinel1_port
, 'sentinel2_ip'
, sentinel2_port
, da sauransu, tare da adiresoshin IP da tashoshin jiragen ruwa na Redis Sentinel sabobin.
Gwajin gazawar kuma High Availability
Don gwadawa Redis replication da high availability, zaku iya kwaikwayi rashin nasarar uwar garken. Redis Sentinel yakamata ya inganta ɗaya daga cikin kwafin ga sabon maigida ta atomatik kuma ya sarrafa gazawar ba tare da wata matsala ba.
Ta bin waɗannan matakan, zaku iya cimma Redis Replication kuma High Availability a cikin aikace-aikacen ku na NodeJS, tabbatar da sake sake bayanan bayanai da ci gaba da aiki ko da a cikin yanayin gazawar uwar garken.