Lycoris.Yarp.Nacos.Extensions
6.0.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Lycoris.Yarp.Nacos.Extensions --version 6.0.1
NuGet\Install-Package Lycoris.Yarp.Nacos.Extensions -Version 6.0.1
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="Lycoris.Yarp.Nacos.Extensions" Version="6.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lycoris.Yarp.Nacos.Extensions" Version="6.0.1" />
<PackageReference Include="Lycoris.Yarp.Nacos.Extensions" />
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 Lycoris.Yarp.Nacos.Extensions --version 6.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Lycoris.Yarp.Nacos.Extensions, 6.0.1"
#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 Lycoris.Yarp.Nacos.Extensions@6.0.1
#: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=Lycoris.Yarp.Nacos.Extensions&version=6.0.1
#tool nuget:?package=Lycoris.Yarp.Nacos.Extensions&version=6.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
yarp网关使用nacos做为注册中心,监控服务上下线并自动生成配置文件
扩展默认的路由匹配规则为:
http://ip:port/nacos服务群组/nacos微服务名称/后续接口地址
转换为
http://微服务实例IP:微服务实例端口/后续接口地址
使用方法:
var builder = WebApplication.CreateBuilder(args);
// 添加Nacos相关服务
builder.Services.AddNacosV2Config(opt =>
{
opt.EndPoint = string.Empty;
opt.ServerAddresses = new List<string>() { "your nacos service ipaddress" };
opt.Namespace = "your service namespace";
opt.UserName = "your username";
opt.Password = "your password";
// swich to use http or rpc
opt.ConfigUseRpc = true;
});
// 添加Nacos相关服务
builder.Services.AddNacosV2Naming(opt =>
{
opt.EndPoint = string.Empty;
opt.ServerAddresses = new List<string>() { "your nacos service ipaddress" };
opt.Namespace = "your service namespace";
opt.UserName = "your username";
opt.Password = "your password";
// swich to use http or rpc
opt.NamingUseRpc = true;
});
// 添加扩展
builder.Services.AddYarpNacosPaoxy(builder =>
{
builder.OptionBuilder(opt =>
{
// 需要生成配置的Nacos上的群组名称列表
// 扩展会拉取这些群组下的所有微服务集群生成反向代理配置
opt.GroupNameList = new List<string>() { "your service groupname" };
});
});
// 添加Yarp服务
builder.Services.AddReverseProxy();
// Add services to the container.
var app = builder.Build();
// Configure the HTTP request pipeline.
//
app.UseRouting();
//
app.UseEndpoints(endpoints =>
{
endpoints.MapReverseProxy(proxyPipeline =>
{
proxyPipeline.UsePassiveHealthChecks();
});
});
app.Run();
当然扩展也支持自定义规则,只需要实现IYarpNacosPaoxyConfigMapper
接口。
builder.Services.AddYarpNacosPaoxy(builder =>
{
builder.OptionBuilder(opt =>
{
// 需要生成配置的Nacos上的群组名称列表
opt.GroupNameList = new List<string>() { "your service groupname" };
});
builder.AddYarpNacosPaoxyConfigMapper<CustomePaoxyConfigMapper>();
});
当然你还想自定义更多,扩展也支持你自己改造,具体的可自定义的接口如下
IYarpNacosPaoxyConfigMapper
:网关反向代理规则。IYarpNacosStore
:规则管理(包含获取规则、热重载、实时更新规则)。IProxyConfigProvider
:Yarp代理的接口,用来重载反向代理配置。ILycorisLoggerFactory
:扩展的自定义日志工厂,在开发的时候,使用别人的扩展,但是由于自己的日志需要按格式,ES才能进行切割关键词分片等,很多不支持,所以自己开发的时候额外增加了这部分。
注意:使用自定义日志工厂时候,还需要自己实现ILycorisLogger
来配合日志工厂实现自定义日志功能
builder.Services.AddYarpNacosPaoxy(builder =>
{
builder.OptionBuilder(opt =>
{
// 需要生成配置的Nacos上的群组名称列表
opt.GroupNameList = new List<string>() { "your service groupname" };
});
builder.AddYarpNacosPaoxyConfigMapper<CustomePaoxyConfigMapper>();
builder.AddYarpNacosStore<CustomeStore>();
builder.AddYarpProxyConfigProvider<CustomeConfigProvider>();
builder.AddLycorisLoggerFactory<CustomeLoggerFactory>();
});
PS:如果你使用了多个Lycoris系列扩展,那你可以在注册这些扩展之前使用builder.Serovces.AddLycorisLoggerFactory<CustomeLoggerFactory>()
进行替换,就不需要在每个扩展中使用AddLycorisLoggerFactory
进行逐一替换了
另外,还有Nacos的心跳监听服务,用来实时观测是否用新的集群服务上下线,并自动更新反向代理配置,当然它也是支持自定义的,只需要实现.Net的IHostedService
builder.Services.AddYarpNacosPaoxy(builder =>
{
builder.OptionBuilder(opt =>
{
// 需要生成配置的Nacos上的群组名称列表
opt.GroupNameList = new List<string>() { "your service groupname" };
});
builder.AddNacosServiceHeart<CustomeServiceHeart>();
});
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. net9.0 was computed. 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.
-
net6.0
- Lycoris.Base (>= 1.0.0)
- nacos-sdk-csharp (>= 1.3.4)
- Newtonsoft.Json (>= 13.0.1)
- Yarp.ReverseProxy (>= 1.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.