欢迎光临
个人技术文档整理

.net+nacos+ocelot 配置中心和服务发现实现

.net+nacos+ocelot 配置中心和服务发现实现

微服务项目 (User-Service)

  • 引入 NuGet 包
    nacos-sdk-csharp.aspnetcore
  • 在Program.cs文件中 引用nacos.json配置文件
     public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureAppConfiguration((hostingContext, config) =>
                    {
                        
                        config.AddJsonFile("nacos.json", optional: true, reloadOnChange: true)
                       .AddJsonFile($"nacos.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true);
     
                    })
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    });
  • 在Startup.cs中 注册nacos服务发现
    public void ConfigureServices(IServiceCollection services)
            {
                #region 注册 Nacos 服务
      
                services.AddNacosAspNet(Configuration, "nacos");
    
                #endregion
    
            }
  • nacos.json
    { 
      //服务发现
      "nacos": { 
        "ServerAddresses": [ "http://localhost:8848" ], //服务器地址
        "DefaultTimeOut": 15000, //默认超时
        "Namespace": "57fe759b-d357-49c9-826e-bc9cc663f622", // 这里请设置 Namespace ID 的值!!!!!!!!
        "ListenInterval": 1000, //侦听间隔时间
        "ServiceName": "User-Service", //服务名称
        "GroupName": "DEFAULT_GROUP", //组名
        "Weight": 2 //权重 
      }
    }
    

     

 

网关项目 (Ocelot-Gateway-Service)

  • 引入 NuGet 包
    Ocelot
    Ocelot.Provider.Nacos
    Ocelot.Provider.Polly
    
    
    nacos-sdk-csharp.aspnetcore
    nacos-sdk-csharp.Extensions.Configuration
    
    
  • 在Program.cs文件中引用ocelot.json,nacos.json配置文件
     public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureAppConfiguration((hostingContext, config) =>
                    {
                        
                        config.AddJsonFile("nacos.json", optional: true, reloadOnChange: true)
                       .AddJsonFile($"nacos.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true);
    
    
                        config.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true)
                            .AddJsonFile($"ocelot.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true);
                    })
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    });

     

  • 在Startup.cs中 注册 Ocelot 网关服务
    public void ConfigureServices(IServiceCollection services)
            { 
                #region 注册 Ocelot 网关服务
                services.AddOcelot() 
                    .AddNacosDiscovery("nacos");
                #endregion
    
            }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
     
                //使用 Ocelot 网关服务
                app.UseOcelot();
            }

     

  • ocelot.json

    {
      "GlobalConfiguration": { 
        "ServiceDiscoveryProvider": {
          "Type": "Nacos"
        }
      },
      "Routes": [
        {
    
          "DownstreamPathTemplate": "/api/{url}", //网关地址 --对外暴露 跳转地址模板;将用户的请求 /api/post/1 转发到 localhost/api/post/1
          "DownstreamScheme": "http", 
          "UpstreamPathTemplate": "/api/{url}", //服务地址
          "UpstreamHttpMethod": [ "Get", "Post" ], //服务请求方式 
          "LoadBalancerOptions": {
            "Type": "RoundRobin" //轮询     
          },
          "UseServiceDiscovery": true, //发现服务
          "ServiceName": "User-Service" //接口服务名称
        }
      ]
    
    }
    

     

  • nacos.json

    { 
      //服务发现
      "nacos": { 
        "ServerAddresses": [ "http://localhost:8848" ], //服务器地址
        "DefaultTimeOut": 15000, //默认超时
        "Namespace": "57fe759b-d357-49c9-826e-bc9cc663f622", // 这里请设置 Namespace ID 的值!!!!!!!!
        "ListenInterval": 1000, //侦听间隔时间
        "ServiceName": "Ocelot-Gateway-Service", //网关注册的服务名称",
        "GroupName": "DEFAULT_GROUP", //组名
        "Weight": 2 //权重 
      }
    }
    

     

赞(1)