1.解释说明
- 这里只解释在wpf中如何去发现可用的实例,这里主要去发现健康的Port和ip
- 关于nacos的环境搭建和服务注册,可以自行选择资料去实施
- 在使用nacos时需要先去下载对应的nacos-sdk-csharp包
2.程序示例
- 这里都给封装好对应的方法了,到时候只需要调用GetIP即可完美执行,引用时先将Example进行实例化
- 返回的数据都存储到list数组里面,可以根据自己的使用情况对list里面的数据进行处理
public class Example
{
public async Task GetIP()
{
var serviceProvider = InitServiceProvider();
INacosNamingService namingSvc = serviceProvider.GetService<INacosNamingService>();
await GetAllInstances(namingSvc);
}
static async Task GetAllInstances(INacosNamingService svc)
{
var list = await svc.GetAllInstances("ServiceName", "GroupName", false);
}
static IServiceProvider InitServiceProvider()
{
IServiceCollection services = new ServiceCollection();
services.AddNacosV2Naming(x =>
{
x.ServerAddresses = new System.Collections.Generic.List<string> { "http://localhost:8848/" };
x.EndPoint = "";
x.Namespace = "namespaceID";
/*x.UserName = "nacos";
x.Password = "nacos";*/
// swich to use http or rpc
x.NamingUseRpc = true;
});
services.AddLogging(builder => { builder.AddConsole(); });
IServiceProvider serviceProvider = services.BuildServiceProvider();
return serviceProvider;
}
}