Kết nối Redis Replication
Database Redis Replication cung cấp hai endpoint, và việc bạn kết nối vào endpoint nào quyết định điều gì xảy ra với ứng dụng khi có failover.
| Endpoint | Port | Định dạng |
|---|---|---|
| Primary endpoint | 6379 | {vip}:6379, ví dụ 172.26.47.3:6379 |
| Advanced endpoint | 26379 | {ip_node1}:26379,{ip_node2}:26379,{ip_node3}:26379 |
Bạn đọc Primary endpoint ở tab Overview của database trên Console Portal.
Primary endpoint
Primary endpoint luôn trỏ tới node Redis Primary hiện tại, và không đổi khi có failover. Dùng cho các thao tác Redis thông thường như GET, SET, DEL, EXPIRE và Pub/Sub.
Phù hợp với:
- Kết nối ứng dụng thông thường.
- Các Redis client đơn giản.
- Hệ thống không hỗ trợ Sentinel.
Advanced endpoint
Advanced endpoint là endpoint của Redis Sentinel. Sentinel giám sát các node Redis, phát hiện lỗi, thực hiện failover tự động và cung cấp service discovery cho client. Khi failover xảy ra, Sentinel công bố Primary mới và client có hỗ trợ Sentinel tự kết nối lại, bạn không phải sửa cấu hình gì.
Phù hợp với:
- Môi trường High Availability.
- Ứng dụng phải tự kết nối lại sau failover.
- Redis client nâng cao có hỗ trợ Sentinel.
- Môi trường production cần nhận biết topology.
Nếu thư viện client của bạn hỗ trợ Sentinel, hãy kết nối qua Advanced endpoint thay vì Primary endpoint.
Nên dùng endpoint nào?
| Trường hợp | Endpoint khuyến nghị |
|---|---|
| Kết nối ứng dụng thông thường | Primary endpoint |
| Ứng dụng dùng thư viện client hỗ trợ Sentinel | Advanced endpoint |
| redis-cli | Primary endpoint |
Ví dụ kết nối
redis-cli
redis-cli không hỗ trợ Sentinel. Nó chỉ kết nối tới một host và port tại một thời điểm, không tự phát hiện được các node Sentinel, nên hãy kết nối qua Primary endpoint.
redis-cli -h {vip} -p 6379 -a {redis_password}
Python (redis-py)
pip install redis
from redis.sentinel import Sentinel
sentinel = Sentinel(
[
("{ip_node1}", 26379),
("{ip_node2}", 26379),
("{ip_node3}", 26379),
],
socket_timeout=2,
)
redis_master = sentinel.master_for(
service_name="mymaster",
password="{redis_password}",
decode_responses=True,
)
redis_master.set("test", "hello")
print(redis_master.get("test"))
Node.js (ioredis)
npm install ioredis
const Redis = require("ioredis");
const redis = new Redis({
sentinels: [
{ host: "ip_node1", port: 26379 },
{ host: "ip_node2", port: 26379 },
{ host: "ip_node3", port: 26379 },
],
name: "mymaster",
password: "redis_password",
});
async function test() {
await redis.set("test", "hello");
const value = await redis.get("test");
console.log(value);
}
test();
Spring Boot
Với ứng dụng Java Spring Boot, FPT khuyến nghị dùng Spring Data Redis kèm client Lettuce hoặc Jedis. Khai báo tên Sentinel master và danh sách node Sentinel trong application.yml hoặc application.properties.
spring:
data:
redis:
password: redis_password
sentinel:
master: mymaster
nodes:
- ip_node1:26379
- ip_node2:26379
- ip_node3:26379
spring.data.redis.password=redis_password
spring.data.redis.sentinel.master=mymaster
spring.data.redis.sentinel.nodes=ip_node1:26379,ip_node2:26379,ip_node3:26379
Các bản Spring Boot cũ dùng tiền tố spring.redis.* thay vì spring.data.redis.*. Hãy kiểm tra phiên bản của bạn dùng dạng nào.
Bước tiếp theo
- Kết nối tới database qua Floating IP để mở truy cập mạng trước khi kết nối
- Redis khi endpoint đã thông nhưng vẫn lỗi kết nối