.net core 集成nacos 并且 使用ocelot 网关集成nacos

nuget 下载

nacos-sdk-csharp.Extensions.Configuration

 

docker 安装 nacos 现在高版本的nacos 与低版本 有些不一样 开放了端口  9848,9849 用于grpc通信

docker run --name nacos \
-e MODE=standalone \
-p 8848:8848 -d \
-p 9848:9848   \
-p 9849:9849 \
--restart always nacos/nacos-server:latest 

页面 访问地址 是

http://192.168.0.196:8848/

在nacos中设置 配置文件

 

  "nacos": {
    "ServerAddresses": [ "http://192.168.0.196:8848" ],
    "DefaultTimeOut": 15000,
    "Namespace": "OderService",
    "ListenInterval": 1500,
    "ServiceName": "OderService",
    "GroupName": "DEFAULT_GROUP",
    "ClusterName": "DEFAULT",
    "RegisterEnabled": true,
    "InstanceEnabled": true,
    "ConfigUseRpc": true,
    "NamingUseRpc": true,
    "Ephemeral": true,
    "LBStrategy": "WeightRoundRobin"
  },

接下来是 .net core 配置 读取

appsettings.json 设置 

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "NacosConfig": {
    "Listeners": [
      {
        "Optional": false,
        "DataId": "OderService",
        "Group": "DEFAULT_GROUP"
      }
    ],
    "Optional": false,
    "Tenant": "fa6bd7e2-ff16-4faf-b0f6-c6a3e0c509fa",
    "ServerAddresses": [ "http://192.168.0.196:8848/" ]
  }
}

 

 1 namespace NacosAPI
 2 {
 3     public class Program
 4     {
 5         public static void Main(string[] args)
 6         {
 7             CreateHostBuilder(args).Build().Run();
 8         }
 9 
10         public static IHostBuilder CreateHostBuilder(string[] args) =>
11             Host.CreateDefaultBuilder(args)
12                 .ConfigureAppConfiguration((context, buider) =>
13                 {
14                     buider.AddDefualtNacos();
15                 })
16                 .ConfigureWebHostDefaults(webBuilder =>
17                 {
18                     webBuilder.UseStartup<Startup>();
19                 });
20     }
21 }

 

nacos扩展

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Extensions.Configuration
{
    public static class NacosExtend
    {
        public static void AddDefualtNacos(this IConfigurationBuilder builder,string nacosConfigStr= "NacosConfig")
        {
            builder.AddNacosV2Configuration(builder.Build().GetSection(nacosConfigStr));
        }
    }
}

读取 我在nacos控制台添加了

 如果能读取就是成功

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NacosAPI
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "NacosAPI", Version = "v1" });
            });

            string value = Configuration.GetSection("verson").Value;
            ILoggerFactory loggerFactory = services.BuildServiceProvider().GetService<ILoggerFactory>();
            ILogger logger = loggerFactory.CreateLogger<Startup>();
            logger.LogInformation($"nacos值{value}-----");
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "NacosAPI v1"));
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

成功读取

 

posted on 2023-05-27 17:24  是水饺不是水饺  阅读(524)  评论(0)    收藏  举报

导航