JAVA

5분 안에 구축하는 Redis Predixy

코린이s 2021. 6. 1. 16:16
728x90

Predixy 란?

- 프록시 중 하나

- redis cluster에서 key 가 분산돼서 저장될 때, 클라이언트가 키가 어디 있는지 찾을 필요 없이 Predixy 가 전부 해줌.

- master, slave 읽기 처리 비율 조정 가능

저자가 redis cluster 를 위한 기능이 탑재되어 있는 프록시를 찾아봤을 때(2021.06.01 기준) predixy 뿐이었으므로 해당 프록시와 연동해본다!

아래와 같은 구조가 베스트 이지만..

테스트를 위한 구성으로 아래와 같이 하나의 서버에서 포트별로 다르게 프로세스를 띄워 구성한다.

1. 설치

$ git clone https://github.com/joyieldInc/predixy.git
$ cd predixy
$ make
$ cp src/predixy /usr/local/bin
-- 설명 문구가 나오면 설치 성공!
$ predixy -h
Usage:
   predixy <conf-file> [options]
   predixy -h or --help
   predixy -v or --version

Options:
   --Name=name        set current service name
   --Bind=addr        set bind address, eg:127.0.0.1:7617, /tmp/predixy
   --WorkerThreads=N  set worker threads
   --LocalDC=dc       set local dc

 

2. 설정

$ cd conf/

conf 폴더 안에 기본 설정 파일들이 만들어져 있기에 각자 원하는 설정으로 변경하면 된다.

저자와 동일하게 하려면 아래와 같다.

# Predixy.conf

################################### GENERAL ####################################
## Predixy configuration file example

## Specify a name for this predixy service
## redis command INFO can get this
Name PredixyExample

## Specify listen address, support IPV4, IPV6, Unix socket
## Examples:
# Bind 127.0.0.1:7617
# Bind 0.0.0.0:7617
# Bind /tmp/predixy

## Default is 0.0.0.0:7617
 Bind 0.0.0.0:7617

## Worker threads
WorkerThreads 4

## Memory limit, 0 means unlimited

## Examples:
# MaxMemory 100M
# MaxMemory 1G
 MaxMemory 0

## MaxMemory can change online by CONFIG SET MaxMemory xxx
## Default is 0
# MaxMemory 0

## Close the connection after a client is idle for N seconds (0 to disable)
## ClientTimeout can change online by CONFIG SET ClientTimeout N
## Default is 0
ClientTimeout 300


## IO buffer size
## Default is 4096
 BufSize 4096

################################### LOG ########################################
## Log file path
## Unspecify will log to stdout
## Default is Unspecified
 Log ./predixy.log

## LogRotate support

## 1d rotate log every day
## nh rotate log every n hours   1 <= n <= 24
## nm rotate log every n minutes 1 <= n <= 1440
## nG rotate log evenry nG bytes
## nM rotate log evenry nM bytes
## time rotate and size rotate can combine eg 1h 2G, means 1h or 2G roate a time

## Examples:
# LogRotate 1d 2G
 LogRotate 1d

## Default is disable LogRotate


## In multi-threads, worker thread log need lock,
## AllowMissLog can reduce lock time for improve performance
## AllowMissLog can change online by CONFIG SET AllowMissLog true|false
## Default is true
# AllowMissLog false

## LogLevelSample, output a log every N
## all level sample can change online by CONFIG SET LogXXXSample N
LogVerbSample 0
LogDebugSample 0
LogInfoSample 10000
LogNoticeSample 1
LogWarnSample 1
LogErrorSample 1


################################### AUTHORITY ##################################
Include auth.conf

################################### SERVERS ####################################
 Include cluster.conf
# Include sentinel.conf
#Include try.conf


################################### DATACENTER #################################
## LocalDC specify current machine dc
# LocalDC bj

## see dc.conf
# Include dc.conf


################################### COMMAND ####################################
## Custom command define, see command.conf
#Include command.conf

################################### LATENCY ####################################
## Latency monitor define, see latency.conf
Include latency.conf

 

# auth.conf

Authority {
    Auth {
        Mode write
    }
    Auth "#a complex password#" {
        Mode admin
    }
}

 

# cluster.conf

ClusterServerPool {
    MasterReadPriority 60
    StaticSlaveReadPriority 50
    DynamicSlaveReadPriority 50
    RefreshInterval 1
    ServerTimeout 1
    ServerFailureLimit 10
    ServerRetryTimeout 1
    KeepAlive 120
    Servers {
        + 127.0.0.1:6300
        + 127.0.0.1:6301
        + 127.0.0.1:6302
        + 127.0.0.1:6400
        + 127.0.0.1:6401
        + 127.0.0.1:6402
    }
}

3. 실행

$ predixy conf/predixy.conf

4. 테스트

아래와 같이 cluster 관련 정보가 정상적으로 나온다면 연동 성공!

$ redis-cli -p 7617 info
# Proxy
Version:1.0.5
Name:PredixyExample
Bind:0.0.0.0:7617
RedisMode:proxy
SingleThread:false
WorkerThreads:4
Uptime:1621676923
UptimeSince:2021-05-22 18:48:43

# SystemResource
UsedMemory:104112
MaxMemory:0
MaxRSS:1807745024
UsedCpuSys:0.020
UsedCpuUser:0.012

# Stats
Accept:1
ClientConnections:1
TotalRequests:48
TotalResponses:47
TotalRecvClientBytes:14
TotalSendServerBytes:1004
TotalRecvServerBytes:15969
TotalSendClientBytes:0

# Servers
Server:127.0.0.1:6300
Role:master
Group:1421ba67b8753514d8b4468bfba6691492600b15
DC:
CurrentIsFail:0
Connections:3
Connect:3
Requests:12
Responses:12
SendBytes:264
RecvBytes:4554

Server:127.0.0.1:6301
Role:master
Group:a9a9cf5addf2b403f9d7a19f2a7436a69b9ee29a
DC:
CurrentIsFail:0
Connections:2
Connect:2
Requests:7
Responses:7
SendBytes:148
RecvBytes:2283

Server:127.0.0.1:6302
Role:master
Group:b6d509a5de3a12df80922d5cc9508a0e9031f4c9
DC:
CurrentIsFail:0
Connections:2
Connect:2
Requests:7
Responses:7
SendBytes:148
RecvBytes:2283

Server:127.0.0.1:6400
Role:slave
Group:1421ba67b8753514d8b4468bfba6691492600b15
DC:
CurrentIsFail:0
Connections:2
Connect:2
Requests:7
Responses:7
SendBytes:148
RecvBytes:2283

Server:127.0.0.1:6401
Role:slave
Group:a9a9cf5addf2b403f9d7a19f2a7436a69b9ee29a
DC:
CurrentIsFail:0
Connections:1
Connect:1
Requests:3
Responses:3
SendBytes:60
RecvBytes:765

Server:127.0.0.1:6402
Role:slave
Group:b6d509a5de3a12df80922d5cc9508a0e9031f4c9
DC:
CurrentIsFail:0
Connections:3
Connect:3
Requests:11
Responses:11
SendBytes:236
RecvBytes:3801


# LatencyMonitor
LatencyMonitorName:all

LatencyMonitorName:get

LatencyMonitorName:set

LatencyMonitorName:blist

5. 클라이언트 연동(client : 애플리케이션)

아래 소스 대로 구현하고 redis ip, port 만 predixy의 ip, port로 변경하면 된다. (위 설정대로 구현 시 port는 7617)

아래 소스 관련해서는 이전 포스팅 [5분 안에 구축하는 레디스(redis)참고하면 된다!

:: https://github.com/works-code/redis

 

works-code/redis

어노테이션을 활용한 캐싱 처리 스타터 코드로, 기본 레디스 연동만 할 시 Junit 테스트 코드 부분만 확인 - works-code/redis

github.com

 

정상 동작하는지 API를 호출해본다!

predixy Port로 접속하여 레디스 서버에 정상적으로 저장됐는지 확인한다!

$ redis-cli -p 7617 get redis:top-chart:local:A
"{\"jobName\":\"A\",\"type\":\"A\",\"cacheDate\":\"2021-06-01 04:43:15\"}"

한번 더 호출 시 캐싱된 시간으로 나오며, 정상적으로 캐싱된 것을 볼 수 있다.

끝!

728x90