SyZero.Feign
1.2.0
See the version list below for details.
dotnet add package SyZero.Feign --version 1.2.0
NuGet\Install-Package SyZero.Feign -Version 1.2.0
<PackageReference Include="SyZero.Feign" Version="1.2.0" />
<PackageVersion Include="SyZero.Feign" Version="1.2.0" />
<PackageReference Include="SyZero.Feign" />
paket add SyZero.Feign --version 1.2.0
#r "nuget: SyZero.Feign, 1.2.0"
#:package SyZero.Feign@1.2.0
#addin nuget:?package=SyZero.Feign&version=1.2.0
#tool nuget:?package=SyZero.Feign&version=1.2.0
SyZero.Feign
SyZero 框架的声明式 HTTP 客户端模块,基于 Refit 实现类似 Spring Cloud OpenFeign 的功能。自 v9.1.0 起,SyZero.Feign 是 IRemoteServiceProvider 抽象的一个具体实现(Provider)—— 同一份 RemoteServiceOptions 配置可被未来的其它 Provider(如 WebApiClient 等)复用。
📦 安装
dotnet add package SyZero.Feign
✨ 特性
- 🚀 声明式调用 - 使用接口定义 HTTP 客户端
- 💾 服务发现 - 集成服务注册与发现
- 🔒 负载均衡 - 内置负载均衡支持
- 🔄 gRPC 支持 - 同时支持 HTTP 和 gRPC 调用
- 🧩 协议级 Provider 扩展 - 可通过
IRemoteServiceProxyFactory注入自定义协议工厂
🚀 快速开始
1. 配置 appsettings.json
{
"Feign": {
"Service": [
{
"ServiceName": "SyZero.Example2",
"AssemblyName": "SyZero.Example2.IApplication",
"Protocol": "Http",
"Timeout": 30,
"Retry": 3
}
],
"Global": {
"Protocol": "Http",
"Timeout": 30,
"Retry": 3,
"EnableSsl": false
}
}
}
⚠️
v9.1.0统一发布时纳入的破坏性变更:原FeignOptions/FeignService/FeignProtocol/IFeignProxyFactory等类型被下沉到SyZero.Core并改名为RemoteServiceOptions/RemoteServiceItem/RemoteServiceProtocol/IRemoteServiceProxyFactory;服务条目字段DllName改名为AssemblyName。AddSyZeroFeign(...)API 形态保持不变(参数类型由FeignOptions改为RemoteServiceOptions)。
2. 注册服务
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 添加SyZero
builder.AddSyZero();
// 注册服务方式1 - 从配置 "Feign" 节读取
builder.Services.AddSyZeroFeign();
// 注册服务方式2 - 配置 + 委托
builder.Services.AddSyZeroFeign(options =>
{
options.Global.Timeout = 30;
options.Global.Retry = 3;
});
// 注册服务方式3 - 直接传入 RemoteServiceOptions
builder.Services.AddSyZeroFeign(new RemoteServiceOptions
{
Service =
{
new RemoteServiceItem
{
ServiceName = "user-service",
AssemblyName = "User.IApplication",
Protocol = RemoteServiceProtocol.Http
}
}
});
// 也可直接走 Core 的 Provider 注册入口(与上面等价):
// builder.Services.AddRemoteService<FeignRemoteServiceProvider>();
var app = builder.Build();
// 使用SyZero
app.UseSyZero();
app.Run();
3. 使用示例
[FeignClient("user-service")]
public interface IUserServiceClient
{
[Get("/api/user/{id}")]
Task<UserDto> GetUserAsync(long id);
[Post("/api/user")]
Task<UserDto> CreateUserAsync([Body] CreateUserInput input);
}
public class MyService
{
private readonly IUserServiceClient _userClient;
public MyService(IUserServiceClient userClient)
{
_userClient = userClient;
}
public async Task<UserDto> GetUserAsync(long id)
{
return await _userClient.GetUserAsync(id);
}
}
📖 配置选项
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Timeout |
int |
30000 |
请求超时时间(毫秒) |
RetryCount |
int |
3 |
重试次数 |
BaseUrl |
string |
"" |
基础地址(不使用服务发现时) |
📖 API 说明
FeignClient 特性
| 属性 | 说明 |
|---|---|
Name |
服务名称(用于服务发现) |
Url |
固定地址(不使用服务发现) |
FallbackType |
降级处理类型 |
使用特性标记接口方法的 HTTP 方法和路径
🔧 高级用法
请求拦截器
builder.Services.AddSyZeroFeign()
.AddInterceptor<AuthHeaderInterceptor>();
public class AuthHeaderInterceptor : IRequestInterceptor
{
public Task InterceptAsync(HttpRequestMessage request)
{
request.Headers.Add("Authorization", "Bearer xxx");
return Task.CompletedTask;
}
}
gRPC 调用
[FeignClient("user-service", Protocol = Protocol.Grpc)]
public interface IUserGrpcClient
{
Task<UserDto> GetUserAsync(long id);
}
⚠️ 注意事项
- 服务发现 - 使用服务名时需要配置服务发现组件
- 超时配置 - 根据业务合理配置超时时间
- 降级处理 - 建议实现 Fallback 以提高可用性
📄 许可证
MIT License - 详见 LICENSE
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Grpc.Net.Client (>= 2.76.0)
- Refit.Newtonsoft.Json (>= 8.0.0)
- SyZero.Core (>= 1.2.0)
- SyZero.Web.Common (>= 1.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.0.0 | 95 | 10/23/2026 |
| 9.1.0 | 85 | 10/22/2026 |
| 1.2.0 | 88 | 10/22/2026 |
| 1.2.0-dev.1 | 53 | 10/22/2026 |
| 1.1.9 | 155 | 4/19/2026 |
| 1.1.9-dev.2 | 82 | 4/19/2026 |
| 1.1.9-dev.1 | 78 | 4/17/2026 |
| 1.1.8 | 139 | 4/17/2026 |
| 1.1.6 | 131 | 4/17/2026 |
| 1.1.6-dev.1 | 82 | 4/17/2026 |
| 1.1.5 | 146 | 4/13/2026 |
| 1.1.5-dev.3 | 88 | 4/13/2026 |
| 1.1.5-dev.2 | 100 | 2/11/2026 |
| 1.1.5-dev.1 | 99 | 1/29/2026 |
| 1.1.4 | 195 | 1/2/2026 |
| 1.1.4-dev.2 | 112 | 1/2/2026 |
| 1.1.4-dev.1 | 97 | 12/30/2025 |
| 1.1.3 | 166 | 12/30/2025 |
| 1.1.3-dev.6 | 96 | 12/30/2025 |
| 1.0.0 | 140 | 4/17/2026 |