天道酬勤

asp.net core 整合nacos

目前业务需要将现有的asp.net core 程序整合到nacos中,以下是整合的过程

准备

包准备

nacos-sdk-csharp

nacos-sdk-csharp.AspNetCore

nacos-sdk-csharp.Extensions.Configuration

默认的配置文件是以json解析,如果需要yaml或者ini支持,则需要

nacos-sdk-csharp.YamlParser

nacos-sdk-csharp.IniParser

nacos准备

nacos通过docker直接跑,这里就不详细介绍,nacos跑起来之后,新建一个配置文件

整合Nacos

nacos获取配置

首先appsetting.json 配置config

"NacosConfig": {
    "Listeners": [
      {
        "Optional": false,
        "DataId": "jptlc-dev",
        "Group": "DEFAULT_GROUP"
      }
    ],
    "Namespace": "",
    "ServerAddresses": [
      "http://127.0.0.1:8848/"
    ],
    "UserName": "nacos",
    "Password": "nacos",
    "AccessKey": "",
    "SecretKey": "",
    "ConfigFilterAssemblies": [],
    "ConfigFilterExtInfo": "some ext infomation"
  }

以上的 "Namespace": "",要注意一下,如果是public 则默认是空。

其他的需要自己配置

 

nacos服务注册和发现

先准备配置文件

"nacos": {
    "ServerAddresses": [
      "http://127.0.0.1:8848/"
    ],
    "DefaultTimeOut": 15000,
    "Namespace": "",
    "ListenInterval": 5000,
    "ServiceName": "jptlc-background",
    "GroupName": "DEFAULT_GROUP",
    "ClusterName": "DEFAULT",
    "Ip": "",
    "PreferredNetworks": "",
    "Port": 5007,
    "Weight": 1,
    "RegisterEnabled": true,
    "InstanceEnabled": true,
    "Ephemeral": true,
    "Secure": false,
    "AccessKey": "",
    "SecretKey": "",
    "UserName": "nacos",
    "Password": "nacos",
    "ConfigUseRpc": false,
    "NamingUseRpc": false,
    "NamingLoadCacheAtStart": "",
    "LBStrategy": "WeightRandom",
    "Metadata": {
      "aa": "bb",
      "cc": "dd"
    }
  }

 

Asp.net Core 整合Nacos

前期准备好之后,代码就比较的简单

 builder.Host.UseNacosConfig(section: "NacosConfig");
builder.Services.AddNacosAspNet(builder.Configuration, "nacos");

 

最终效果

 

posted @ 2023-07-19 15:57  JulyLuo  阅读(134)  评论(0编辑  收藏  举报