์ด๋ฒ์๋ redis์ sentinel์ client์์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค.
redis standalone ์ฌ์ฉํ๊ธฐ
Copy cd standalone
docker-compose up -d
๋ด ์ปดํจํฐ์ 6379 ํฌํธ๋ก redis๊ฐ ์คํ๋์์ต๋๋ค.
์ด์ c# ์ฝ๋๋ฅผ ๋ง๋ค์ด ๋ณด์.
https://redis.io/docs/connect/clients/dotnet/ ์ฐธ๊ณ ํ๋ค.
Copy dotnet add package NRedisStack
Copy using Microsoft . AspNetCore . Mvc ;
using NRedisStack ;
using NRedisStack . RedisStackCommands ;
using StackExchange . Redis ;
namespace ThreadTest . Controllers ;
[ ApiController ]
[ Route ( "values" )]
public class ValuesController : ControllerBase
{
private readonly ILogger < ValuesController > _logger;
public ValuesController ( ILogger < ValuesController > logger)
{
_logger = logger;
}
[ HttpGet ]
public OkResult Get ()
{
ConnectionMultiplexer redis = ConnectionMultiplexer . Connect ( "localhost" );
IDatabase db = redis . GetDatabase ();
System . Console . WriteLine ( db . Database . ToString ());
db . StringSet ( "foo" , "bar" );
Console . WriteLine ( db . StringGet ( "foo" )); // prints bar
return Ok ();
}
}
.. .
curl http : //localhost:5007/value
! [ alt text ](images / sentinel - client / image . png )
bar๋ฅผ ์ฐ๋๊ฑธ ๋ณผ์์๋ค ๋ก์ปฌ์ ์๋ ๋ ๋์ค์ ์ ์ฅ์ด ๋๊ณ ๊ทธ ๊ฐ์ ๊ฐ์ ธ์จ๊ฒ์ด๋ค.
cli๋ก ํ์ธํด๋ณด์.
```sh
docker run - it -- rm bitnami / redis : 7.2 redis - cli - h host . docker . internal - p 6379
get foo
๊ฒฐ๊ณผ๊ฐ ์ ๋์จ๋ค.
Copy foreach ( EndPoint endpoint in redis . GetEndPoints ())
{
Console . WriteLine ( endpoint . ToString ());
}
์ด ์ฝ๋๋ฅผ ์ถ๊ฐํด์ ์คํํด๋ณด์.
Copy Unspecified/localhost:6379
์ด๋ผ๋ ๊ฒฐ๊ณผ๊ฐ ๋์จ๋ค.
redis replica(main-secondary) ์ฌ์ฉํ๊ธฐ
Copy cd replica-multi-node
docker-compose up -d
docker ps
๋ชจ๋ ๋
ธ๋๋ฅผ ,๋ก ๊ตฌ๋ถํด์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
Copy ConnectionMultiplexer redis = ConnectionMultiplexer . Connect ( "localhost:6379,localhost:6380,localhost:6381" );
if (redis == null ) return Ok ();
if ( ! redis . IsConnected ) return Ok ();
IDatabase db = redis . GetDatabase (); //database 0 ์ ์ฌ์ฉํ๋ค.
Console . WriteLine ( db . Database . ToString ());
db . StringSet ( "foo1" , "bar1" );
Console . WriteLine ( db . StringGet ( "foo" )); // prints bar
์๋์ผ๋ก master๋ฅผ ์์๋ด์ ๊ทธ๊ณณ์๋ค ๋ฐ์ดํฐ๋ฅผ ์
๋ ฅ์์ผ์ค๋ค. ์ฝ๊ธฐ๋ ์๋์ผ๋ก secondary์์ ์ฝ์ด์จ๋ค.
database๋ 0๋ฒ์ด๊ณ ๊ฐ์ bar์ด๊ณ endpoint๋ localhost:6379์ด๋ค.
replica์์ ํ์ธํด๋ณด์.
Copy docker run -it --rm bitnami/redis:7.2 redis-cli -h host.docker.internal -p 6380
replica์์๋ ๊ฐ์ด ์ ์
๋ ฅ๋์๋ค.
db๋ฅผ ์ ํํ์ง
๋ ๋์ค ์๋ฒ๋ ์ฌ๋ฌ๊ฐ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๊ฐ์ง ์ ์๋ค.
DB ๋ฒํธ 0๋ถํฐ 15๊น์ง 16๊ฐ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๊ฐ๋๋ค.
Copy CONFIG GET databases
INFO keyspace
๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ ํํ๋ ๋ฐฉ๋ฒ
code์์ db๋ฅผ ์ ํํ๋ ๋ฐฉ๋ฒ
Copy IDatabase db = redis . GetDatabase (); // 0๋ฒ์ ๋ฆฌํด
// 1๋ฒ์ ์ฌ์ฉํ๋ ค๋ฉด
IDatabase db = redis . GetDatabase ( 1 ); //database 1 ์ ์ฌ์ฉํ๋ค.
redis sentinel ์ฌ์ฉํ๊ธฐ
Copy cd sentinel
docker-compose up -d
sentinel์ service name์ด๋ผ๋ ๊ฒ์ด ์๋ค ํ์ธํด๋ณด์.
Copy docker run -it --rm bitnami/redis-sentinel:7.2 redis-cli -h host.docker.internal -p 26379
info sentinel
> master0:name = mymaster, status = ok, address = 172.30.0.2:6379, slaves = 4, sentinels = 3
mymaster๊ฐ ์ด๋ฆ์ด๋ค.
connection string์ ์๋น์ค ์ด๋ฆ์ ์ง์ ํ๋ฉด sentinel ๋ชจ๋๊ฐ ์คํ
Copy var sentinelConfig = new ConfigurationOptions
{
AllowAdmin = false ,
AbortOnConnectFail = false ,
CommandMap = CommandMap . Sentinel ,
EndPoints =
{
"host.docker.internal:26379" ,
"host.docker.internal:26380" ,
"host.docker.internal:26381"
} ,
};
var masterConfig = new ConfigurationOptions
{
CommandMap = CommandMap . Default ,
ServiceName = "mymaster" ,
};
var redis = ConnectionMultiplexer . SentinelConnect (sentinelConfig , Console . Out );
var conn = redis . GetSentinelMasterConnection (masterConfig , Console . Out );
var db = conn . GetDatabase ( 0 );
db . StringSet ( "foo2" , "bar2" );
Console . WriteLine ( db . StringGet ( "foo" )); // prints bar
redis . Close ();
๋์ํ๋ค.
redis cluster ์ฌ์ฉํ๊ธฐ
Copy string connectString = "host.docker.internal:6371,host.docker.internal:6372,host.docker.internal:6373,host.docker.internal:6374,host.docker.internal:6375,host.docker.internal:6379";
var options = ConfigurationOptions . Parse (connectString);
var redisCluster = ConnectionMultiplexer . Connect (options);
IDatabase db = redisCluster . GetDatabase ();
db . StringSet ( "foo3" , "bar3" );
Console . WriteLine ( db . StringGet ( "foo3" )); // prints bar3
redisCluster . Close ();
์ด๋ ๊ฒ ํ๋ฉด ๋์ํ๋ค.
todo
์ผ๋ฐ์ ์ผ๋ก ์ด๋ ๊ฒ ์ฝ๋ฉํ๋๋ฐ ์ด๋ฌ๋ฉด connection์ ๋งค๋ฒ ๋งบ๋๋ค. ๋ฌธ์ ๊ฐ ๋๋ค.
๊ผญ Async๋ฅผ ์ฌ์ฉํ๊ธฐ ๋ฐ๋๋ค.
์ฐธ๊ณ
connection string์ ์ธ์ ์๋ ์ถ๊ฐ ์ต์
๋ค
https://stackexchange.github.io/StackExchange.Redis/Configuration#configuration-options
Last updated 9 months ago