SyZero.Feign 1.1.4

dotnet add package SyZero.Feign --version 1.1.4
                    
NuGet\Install-Package SyZero.Feign -Version 1.1.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SyZero.Feign" Version="1.1.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.Feign" Version="1.1.4" />
                    
Directory.Packages.props
<PackageReference Include="SyZero.Feign" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SyZero.Feign --version 1.1.4
                    
#r "nuget: SyZero.Feign, 1.1.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SyZero.Feign@1.1.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SyZero.Feign&version=1.1.4
                    
Install as a Cake Addin
#tool nuget:?package=SyZero.Feign&version=1.1.4
                    
Install as a Cake Tool

SyZero.Feign

SyZero 框架的声明式 HTTP 客户端模块,基于 Refit 实现类似 Spring Cloud OpenFeign 的功能。

📦 安装

dotnet add package SyZero.Feign

✨ 特性

  • 🚀 声明式调用 - 使用接口定义 HTTP 客户端
  • 💾 服务发现 - 集成服务注册与发现
  • 🔒 负载均衡 - 内置负载均衡支持
  • 🔄 gRPC 支持 - 同时支持 HTTP 和 gRPC 调用

🚀 快速开始

1. 配置 appsettings.json

{
  "Feign": {
    "Timeout": 30000,
    "RetryCount": 3
  }
}

2. 注册服务

// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 添加SyZero
builder.AddSyZero();

// 注册服务方式1 - 使用默认配置
builder.Services.AddSyZeroFeign();

// 注册服务方式2 - 使用委托配置
builder.Services.AddSyZeroFeign(options =>
{
    options.Timeout = 30000;
    options.RetryCount = 3;
});

// 注册服务方式3 - 注册特定客户端
builder.Services.AddSyZeroFeign()
    .AddClient<IUserServiceClient>("user-service");

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);
}

⚠️ 注意事项

  1. 服务发现 - 使用服务名时需要配置服务发现组件
  2. 超时配置 - 根据业务合理配置超时时间
  3. 降级处理 - 建议实现 Fallback 以提高可用性

📄 许可证

MIT License - 详见 LICENSE

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.1.4 97 1/2/2026
1.1.4-dev.2 41 1/2/2026
1.1.4-dev.1 43 12/30/2025
1.1.3 89 12/30/2025
1.1.3-dev.6 42 12/30/2025
1.1.3-dev.3 121 1/19/2024
1.1.3-dev.2 178 11/3/2023
1.1.3-dev.1 182 3/21/2023
1.1.2 394 3/15/2023
1.1.2-dev.108.29344 182 3/15/2023
1.1.2-dev.108.28054 173 3/15/2023
1.1.2-dev.108.27487 178 3/15/2023
1.1.1 347 3/15/2023
1.1.1-dev.108.14980 177 3/15/2023
1.1.1-dev.108.13289 180 3/15/2023
1.1.1-dev.107.27144 172 3/14/2023
1.1.0 322 3/14/2023
1.1.0-workflow-dev.107.22552 182 3/14/2023
1.1.0-workflow-dev.107.21746 181 3/14/2023
1.1.0-workflow-dev.107.21506 186 3/14/2023
1.1.0-workflow-dev.107.20979 175 3/14/2023
1.1.0-dev.107.26364 176 3/14/2023
1.1.0-dev.107.24396 185 3/14/2023
1.1.0-dev.107.22787 175 3/14/2023
1.0.6 420 3/5/2022