.net core接入nacos注册服务并使用配置中心

1、安装依赖

Nuget包:nacos-sdk-csharp.Extensions.Configuration和nacos-sdk-csharp.AspNetCore

2、在appsettings.json中配置

复制代码
"nacos": {
    "ServerAddresses": [
      "http://localhost:8848/"
    ],
    "DefaultTimeOut": 15000,
    "Namespace": "xxxxx",//命名空间
    "ListenInterval": 5000,
    "ServiceName": "testService",//服务名称
    "GroupName": "DEFAULT_GROUP",//组明
    "ClusterName": "DEFAULT",
    "Ip": "",
    "PreferredNetworks": "",
    "Port": 0,
    "Weight": 1,
    "RegisterEnabled": true,
    "InstanceEnabled": true,
    "Ephemeral": true,
    "Secure": false,
    "AccessKey": "",
    "SecretKey": "",
    "UserName": "nacos",//用户名
    "Password": "nacos",//密码
    "ConfigUseRpc": false,
    "NamingUseRpc": false,
    "NamingLoadCacheAtStart": "",
    "LBStrategy": "WeightRandom",
    //下方是注册中心专用,如果不需要配置中心则可不配置
    "Listeners": [
      {
        "Group": "DEFAULT_GROUP",
        "DataId": "test-dev.json",
        "Optional": false
      }
    ]
  }

3、在Program.cs中注入服务

复制代码
builder.Services.AddNacosAspNet(builder.Configuration, section:"nacos");
builder.Configuration.AddNacosV2Configuration(builder.Configuration.GetSection("nacos"));//如果不使用服务注册中心可不写他

4、注.net 中需要添加如下开放IP

复制代码
builder.WebHost.UseUrls("http://*:5065");

5、启动可看到

6、如果配置了nacos的配置文件的话

测试用例

复制代码
public class TestController : ControllerBase
{
  private readonly IConfiguration _configuration;

    public TestController(IConfiguration configuration)
    {
        this._configuration = configuration;
    }
    
    [HttpGet("getConfig")]
    public string getConfig() => _configuration["a1"];
}
相关推荐
回家路上绕了弯13 小时前
朋友圈更新怎么实时通知?从发布到接收的全链路解析
后端·微服务
yivifu13 小时前
精益求精,支持处理嵌套表格的Word表格转HTML表格
开发语言·c#·word
唐青枫14 小时前
C#.NET 全局异常到底怎么做?最完整的实战指南
c#·.net
fanly111 天前
在抖音直播推广开源作品的可行性?
微服务·netty·.net core·microservice
Charles_go1 天前
C#13、什么是部分类
开发语言·c#
q***57501 天前
微服务搭建----springboot接入Nacos2.x
spring boot·微服务·架构
机智的豆子1 天前
物联网架构
物联网·微服务
是Yu欸1 天前
仓颉迁移实战:将 Node.js 微服务移植到 Cangjie 的工程化评测
微服务·云原生·开源·node.js·vim·gitcode·cangjie
hour_go1 天前
DeepHunt微服务故障定位系统核心技术解析2
微服务·云原生·架构·deephunt
ghie90901 天前
C#语言中使用“using“关键字的介绍
开发语言·c#