服务发现指南¶
仓库中包含一套基于 Redis 的服务发现与 HTTP 调用工具。
核心组件¶
| 组件 | 描述 |
|---|---|
ServiceRegistry |
服务注册与心跳 |
DiscoveryClient |
带缓存的服务发现与负载均衡 |
DiscoveryHttpClient |
结合服务发现的 HTTP 重试与节点切换 |
ServiceRegistry¶
服务注册中心,负责管理服务的注册与心跳。
DiscoveryClient¶
带缓存的服务发现客户端:
DiscoveryHttpClient¶
结合服务发现的 HTTP 客户端,支持自动重试与节点切换:
from by_framework.util.discovery_http_client import DiscoveryHttpClient
# 构造函数接收一个已有的 DiscoveryClient 实例,不是 redis_client / service_name;
# service_name 是每次请求单独传入的(一个 DiscoveryHttpClient 可以调用多个服务)
http_client = DiscoveryHttpClient(discovery_client)
response = await http_client.get("api-service", "/api/v1/users")
response = await http_client.post("api-service", "/api/v1/users", json={"name": "test"})
import com.iwhaleai.byai.framework.util.http.DiscoveryHttpClient;
// 构造函数接收 DiscoveryClient + 底层 ByHttpClient(可传 null 使用默认实现)+ RetryConfig;
// serviceName 同样是每次请求单独传入的
DiscoveryHttpClient httpClient = new DiscoveryHttpClient(
discoveryClient, null, RetryConfig.builder().build()
);
HttpResponse response = httpClient.get("api-service", "/api/v1/users", null, null).get();
HttpResponse response = httpClient.post(
"api-service", "/api/v1/users", null, Map.of("name", "test"), null
).get();
import { DiscoveryHttpClient, DiscoveryClient } from '@byclaw/by-framework';
const discoveryClient = new DiscoveryClient(redis);
const httpClient = new DiscoveryHttpClient({ discoveryClient });
const response = await httpClient.get("api-service", "/api/v1/users");
const response = await httpClient.post("api-service", "/api/v1/users", {
json: { name: "test" },
});