首页 > 其他分享 >ocelot + consul 保姆级快速上手及遇到的坑,只讲步骤不讲原理

ocelot + consul 保姆级快速上手及遇到的坑,只讲步骤不讲原理

时间:2022-12-15 17:01:59浏览次数:31  
标签:127.0 http 0.1 consul json ocelot 上手 app

本文只讲步骤不讲原理,先搭建成功再自行找资料看具体配置是什么意思!

环境:win10,vs2022 , .net6 ,neget包(Ocelot和Ocelot.Provider.Consul皆为18.0.0版本,Consul1.6.10.8版本),consul_1.14.2_windows_386

配置ocelot + consul花了一天时间,主要问题在于ocelot访问的坑,consul注册的时候,Node Name为计算机名称,consul访问直接的计算机名称,而并非127.0.0.1或者localhost,导致报错,提示目标计算机积极拒绝。同时注意你的访问协议是https还是http,本文用的全是http

只需要将consul的Node Name改为127.0.0.1即可

 

 还需要注意项目启动和注册的地址是否一致,如项目启动用的是:localhost,注册时使用的ip是:127.0.0.1,也会出现上述目标计算机积极拒绝的错误。

问题解决方案:

注意consul所在文件夹地址

第一种:在启动consul的时候,node参数可以写成 -node=127.0.0.1
如:consul agent -server -ui -bootstrap-expect=1 -data-dir=D:\Tools\consul_1.14.2_windows_386 -node=127.0.0.1 -client=0.0.0.0 -bind=127.0.0.1 -datacenter=dc1 -join 127.0.0.1

第二种:在启动consul的时候,node参数可写成"hostname",在Hosts文件中对,node参数添加dns解析.
consul agent -server -ui -bootstrap-expect=1 -data-dir=D:\Tools\consul_1.14.2_windows_386 -node=hostname -client=0.0.0.0 -bind=127.0.0.1 -datacenter=dc1 -join 127.0.0.1
win7 hosts文件位置:C:\Windows\System32\drivers\etc

在hosts文件中添加一行"127.0.0.1 hostname",即可

https://www.cnblogs.com/huyougurenxinshangguo/p/15246313.html

https://blog.csdn.net/salty_oy/article/details/119636278

 

下面说一下实现

 

 

 

 S1,S2引用了consul包,APIGateway引用了Ocelot包,Ocelot.Provider.Consul包

 创建3个webapi,其中S1,S2为相同代码,只修改了配置文件appsettings.json,一个端口为7000,一个端口为7001

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Consul": {
    "IP": "127.0.0.1",
    "Port": "8500"

  },
  "Service": {
    "Name": "consul_test"
  },
  "ip": "127.0.0.1",
  "port": "7000",
  "AllowedHosts": "*"
}

  launchSettings.json也把端口改一下,一个7000,一个7001,同时注意一下sslport这个key

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://127.0.0.1:7000",
      "sslPort": 0
    }
  },
  "profiles": {
    "S2": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://127.0.0.1:7000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
} 

webapi页面WeatherForecastController

using Microsoft.AspNetCore.Mvc;

namespace S1.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };
        private readonly IConfiguration _IConfiguration;

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration iConfiguration)
        {
            _logger = logger;
            _IConfiguration = iConfiguration;
        }

        [HttpGet]
        public string GetTest()
        {
            string ip = _IConfiguration["IP"];
            string port = _IConfiguration["Port"];
            return $@"{ip} {port}";
        }

        //[HttpGet(Name = "GetWeatherForecast")]
        //public IEnumerable<WeatherForecast> Get()
        //{
        //    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        //    {
        //        Date = DateTime.Now.AddDays(index),
        //        TemperatureC = Random.Shared.Next(-20, 55),
        //        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        //    })
        //    .ToArray();
        //}
    }
}

 

然后在S1,S2两个api中注入consulhelper类

using Consul;

namespace S2
{
    public static class ConsulHelper
    {
        /// <summary>
        /// 将当前站点注入到Consul
        /// </summary>
        /// <param name="configuration"></param>
        public static void ConsulRegister(this IConfiguration configuration)
        {
            //初始化Consul服务
            ConsulClient client = new ConsulClient(c => {
                c.Address = new Uri("http://127.0.0.1:8500");
                c.Datacenter = "dc1";
            });

            //站点做集群时ip和端口都会不一样,所以我们通过环境变量来确定当前站点的ip与端口
            string ip = configuration["ip"];
            int port = int.Parse(configuration["port"]);
            client.Agent.ServiceRegister(new AgentServiceRegistration
            {
                ID = "service" + Guid.NewGuid(), //注册进Consul后给个唯一id,
                Name = "consul_test", //站点做集群时,Name作为该集群的群名,
                //Address = ip,
                Address = "127.0.0.1",
                Port = port,
                Tags = null, //这个就是一个随意的参数,文章后面做负载均衡的时候说
                Check = new AgentServiceCheck
                {
                    Interval = TimeSpan.FromSeconds(10),
                    //HTTP = $"http://{ip}:{port}/api/health",
                    HTTP = $"http://{ip}:{port}/api/WeatherForecast/GetTest",
                    Timeout = TimeSpan.FromSeconds(5),
                    DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5)
                }
            });
        }
    }
}

Program类中注入

using S2;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();
app.Configuration.ConsulRegister();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();

S1,S2代码完全一样,区别只修改了launchSettings.json,appsettings.json配置文件的端口号

再说一下ocelot吧,

Program类注入Ocelot

using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
using Ocelot.Values;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOcelot(new ConfigurationBuilder()
              .AddJsonFile("Ocelot.json")
              .Build()).AddConsul();
builder.Host.ConfigureLogging(c =>
{
    c.ClearProviders();
    c.AddConsole();
});
//builder.Host.ConfigureAppConfiguration((context, config) =>
//{
//    config.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
//}).ConfigureWebHostDefaults(c => c.UseUrls("http://localhost:5000"));
//builder.Services.AddSwaggerGen();

var app = builder.Build();
app.UseOcelot();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    //app.UseSwagger();
    //app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();

Ocelot.json

{
    "Routes": [
      {
        "DownstreamPathTemplate": "/api/{url}", //下游服务地址--url变量
        "DownstreamScheme": "http",
        "DownstreamHostAndPorts": [
          {
            "Host": "127.0.0.1",
            "Port": 7000
          }
          ,
          {
            "Host": "127.0.0.1",
            "Port": 7001
          }
        ],
        "UpstreamPathTemplate": "/{url}", //上游请求路径,网关地址--url变量
        "UpstreamHttpMethod": [ "Get", "Post" ],
        //consul
        "UseServiceDiscovery": true, //使用服务发现
        "ServiceName": "consul_test", //consul服务名称
"LoadBalancerOptions": { "Type": "RoundRobin" //轮询 LeastConnection-最少连接数的服务器 NoLoadBalance不负载均衡  }, "GlobalConfiguration": { "RequestIdKey": "OcRequestId", "BaseUrl": "http://127.0.0.1:5000", //网关对外地址 //配置consul地址 "ServiceDiscoveryProvider": { "Host": "127.0.0.1", "Port": 8500, "Type": "Consul" //由consul提供服务发现,ocelot也支持etc等  } } } ] }

launchSettings.json

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://127.0.0.1:5001",
      "sslPort": 0
    }
  },
  "profiles": {
    "ApiGateway": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://127.0.0.1:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

 

完成之后,先启动S1,S2,看看consul注入成功没有,

 

 

 然后启动ApiGateway,使用其对外地址进行访问,http://127.0.0.1:5000,注意看地址,这里和Ocelot.json里面的配置有关

 

"DownstreamPathTemplate": "/api/{url}", //下游服务地址--url变量

 

 

"UpstreamPathTemplate": "/{url}", //上游请求路径,网关地址--url变量

 

 

 

没配置成功请仔细看加粗加红部分

 

源码:https://gitee.com/yuanrang123/ocelot-consul

 

标签:127.0,http,0.1,consul,json,ocelot,上手,app
From: https://www.cnblogs.com/gongzi/p/16985104.html

相关文章

  • 新人小白想做跨境电商,怎么上手比较稳妥一点呢?
    近年来,随着互联网的发展,国内外商业贸易越来越顺畅,直播电商的普及也带动了大量相关产业链的发展,其中跨境电商是尤为突出的一个。虽然国内做跨境电商的企业很多,但还是有很多新......
  • asp.net core 微服务网关示例 ocelot gateway Demo
    ocelotasp.netcore微服务gateway介绍https://ocelot.readthedocs.io/en/latest/introduction/gettingstarted.html 1.新建asp.netcorewebapi空项目AProject,nug......
  • Ocelot API网关的实现剖析
    在微软TechSummit2017大会上和大家分享了一门课程《.NETCore在腾讯财付通的企业级应用开发实践》,其中重点是基于ASP.NETCore打造可扩展的高性能企业级API网关,以开源的......
  • aspnetcore 应用 接入Keycloak快速上手指南
    登录及身份认证是现代web应用最基本的功能之一,对于企业内部的系统,多个系统往往希望有一套SSO服务对企业用户的登录及身份认证进行统一的管理,提升用户同时使用多个系统的体验......
  • Ocelot 集成Butterfly 实现分布式跟踪
    微服务,通常都是用复杂的、大规模分布式集群来实现的。微服务构建在不同的软件模块上,这些软件模块,有可能是由不同的团队开发、可能使用不同的编程语言来实现、有可能布在了几......
  • 快速上手 Pytest + Requests + Allure2 测试框架实战技能
    随着分层测试策略和自动化测试的普及,测试框架和接口测试成为测试工程师需重点掌握的底层核心技能。在Python自动化测试领域,Pytest由于入门简单,扩展丰富,功能强大,易于维护......
  • 轻松上手ECS云服务器
    轻松上手ECS云服务器•什么是阿里云服务器ECS?•如何操作阿里云服务器ECS?•总结什么是阿里云服务器ECS?既然是要上手ECS云服务器,那么我们首先要了解......
  • “零代码”的瓴羊Quick BI即席分析,业务人员也能轻松上手
    企业无论规模大小,在经营与管理过程中均会产生海量数据,这些数据是企业实现持续发展的宝贵资产。一些企业倾向于采用传统数据管理方式,如聘用数据工程师、数据分析师等,建构企业......
  • 使用SpringBoot连接MySQL数据库,快速上手「建议收藏」
    大家好,又见面了,我是你们的朋友全栈君。使用SpringBoot连接MySQL目录0环境配置1建立MySQL数据库2使用SpringInitializer快速搭建springboot项目3配置pom.xml文件4配......
  • 一款小白可上手的懒人听书下载器(爬虫、下载器、提取mp3、批量下载工具)
    它来啦!支持下载提取mp3! 推荐一款新手工具,可以一键将懒人听书上的节目资源下载到本地,支持(爬虫、下载器、提取mp3、批量下载)。 懒人听书爬虫批量下载工具: ​​windows电脑......