RefitConsul 0.0.2

dotnet add package RefitConsul --version 0.0.2
NuGet\Install-Package RefitConsul -Version 0.0.2
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="RefitConsul" Version="0.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RefitConsul --version 0.0.2
#r "nuget: RefitConsul, 0.0.2"
#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.
// Install RefitConsul as a Cake Addin
#addin nuget:?package=RefitConsul&version=0.0.2

// Install RefitConsul as a Cake Tool
#tool nuget:?package=RefitConsul&version=0.0.2

RefitConsul

RefitConsul扩展了Refit组件的Consul服务发现功能,能更加便捷调用Restful服务。 consul服务发现实现了随机轮询策略与缓存。

如何使用

1、定义服务接口

	public interface IAuthApi
	{
		/// <summary>
		/// 不需要验证的接口
		/// </summary>
		/// <returns></returns>
		[Get("/sys/users")]
		Task <dynamic> GetUsers();

		/// <summary>
		/// 接口采用Bearer方式验证,Token在ConsulDiscoveryDelegatingHandler统一获取
		/// </summary>
		/// <returns></returns>
		[Get("/sys/session")]
		[Headers("Authorization: Bearer")]
		Task<dynamic> GetCurrentUserInfo();

		/// <summary>
		/// 接口采用Bearer方式验证,Token使用参数方式传递
		/// </summary>
		/// <returns></returns>
		[Get("/sys/session")]
		Task<dynamic> GetCurrentUserInfo([Header("Authorization")] string authorization);
	}

2、startup中注册并配置

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            
	    //注册RefitClient
            //用SystemTextJsonContentSerializer替换默认的NewtonsoftJsonContentSerializer序列化组件
            //如果调用接口是使用NewtonsoftJson序列化则不需要替换
            services.AddRefitClient<IAuthApi>(new RefitSettings(new SystemTextJsonContentSerializer()))
                    //设置服务名称,andc-api-sys是系统在Consul注册的服务名
                    .ConfigureHttpClient(c => c.BaseAddress = new Uri("http://andc-api-sys"))
                    //注册ConsulDiscoveryDelegatingHandler,
                    .AddHttpMessageHandler(() =>
                    {
                        //http://12.112.75.55:8550是consul服务器的地址
                        //() => Helper.GetToken() 获取token的方法,是可选参数,如果不需要token验证不需要传递。
                        return new ConsulDiscoveryDelegatingHandler("http://12.112.75.55:8550", () => Helper.GetToken());
                    })
                    //设置httpclient生命周期时间,默认也是2分钟。
                    .SetHandlerLifetime(TimeSpan.FromMinutes(2))
                    //添加polly相关策略
                    .AddPolicyHandler(retryPolicy)
                    .AddPolicyHandler(timeoutPolicy)
                    .AddPolicyHandler(bulkheadPolicy);
        }

3、controller中使用

    public class HomeController : ControllerBase
    {
        private readonly IAuthApi _authApi;
        private readonly string _token = Helper.GetToken().Result;

        /// <summary>
        /// RefitConsul测试
        /// </summary>
        /// <param name="authApi">IAuthApi服务</param>
        public HomeController(IAuthApi authApi)
        {
            _authApi = authApi;
        }

        [HttpGet]
        public async Task<dynamic> GetAsync()
        {
            //不需要验证的服务
            var result1 = await _authApi.GetUsers();

            //需要验证,token采用参数传递
            var result2 = await _authApi.GetCurrentUserInfo($"Bearer {_token}");

            //需要验证,token在ConsulDiscoveryDelegatingHandler获取。
            var result3 = await _authApi.GetCurrentUserInfo();

            return result3;
        }

License

MIT Free Software, Hell Yeah!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
0.0.2 3,089 10/20/2020
0.0.1 429 10/7/2020

RefitConsul扩展了Refit组件的Consul服务发现功能,能更加便捷调用Restful服务。 consul服务发现实现了随机轮询策略与缓存。