zoukankan      html  css  js  c++  java
  • 微服务网关从零搭建——(二)搭建api网关(不带验证)

    环境准备

    创建空的core2.1 api项目  演示使用名称APIGateWay  过程参考上一篇

    完成后在appsettings.json 添加节点

    "Setting": {
    "Port": "5000"
    }

    搭建过程

    添加文件configuration.json

    {
      "ReRoutes": [
        // API:demo1
        {
          "UseServiceDiscovery": true,
          "DownstreamPathTemplate": "/api/{url}",
          "DownstreamScheme": "http",
          "ServiceName": "demoAPi",
          "LoadBalancerOptions": {
            "Type": "RoundRobin"
          },
          "UpstreamPathTemplate": "/demo1/{url}",
          "UpstreamHttpMethod": [ "Get", "Post" ],
          "ReRoutesCaseSensitive": false // non case sensitive
        }
        //,
        //// API:demo2
        //{
        //  "UseServiceDiscovery": true,
        //  "DownstreamPathTemplate": "/api/{url}",
        //  "DownstreamScheme": "http",
        //  "ServiceName": "demoAPi2",
        //  "LoadBalancerOptions": {
        //    "Type": "RoundRobin"
        //  },
        //  "UpstreamPathTemplate": "/demo2/{url}",
        //  "UpstreamHttpMethod": [ "Get", "Post" ],
        //  "ReRoutesCaseSensitive": false // non case sensitive
        //}
      ],
      "GlobalConfiguration": {
        "ServiceDiscoveryProvider": {
          "Host": "localhost", // Consul Service IP
          "Port": 8500 // Consul Service Port
        }
      }
    }
    configuration.json

    参数说明参见上一篇结尾处。

    修改Program.cs 如下:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Logging;
    
    namespace APIGateWay
    {
        public class Program
        {
            public static string StartPort;
            public static void Main(string[] args)
            {
                var config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: true)
    .Build();
                StartPort = config.GetSection("Setting")["Port"];
                CreateWebHostBuilder(args).Build().Run();
            }
    
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>()
                    .UseUrls($"http://*:{StartPort}")
                    .ConfigureAppConfiguration((hostingContext, builder) =>
                    {
                        builder.AddJsonFile("configuration.json", false, true);
                    });
        }
    }
    Program

    添加 Ocelot.Provider.Consul nuget引用

    修改startup.cs文件为

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Options;
    using Ocelot.DependencyInjection;
    using Ocelot.Middleware;
    using Ocelot.Provider.Consul;
    
    namespace APIGateWay
    {
        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.AddOcelot(Configuration).AddConsul();
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                //app.UseMvc();//no need
                app.UseOcelot().Wait();
            }
        }
    }
    Startup

    注意事项:

    1.appsettings.json 和 configuration.json 均需要设置

     2.services.AddOcelot(Configuration).AddConsul();

    此处必须增加 服务发现的AddConsul

    到此带有consul的网关搭建完成

     

  • 相关阅读:
    sql 在日期范围内搜索
    js 处理日期时间字符串显示的方法
    matlab练习程序(并行计算)
    C++程序运行时间
    matlab练习程序(KNN,K最邻近分类法)
    多媒体指令(像素处理)
    ubuntu启动/重启/停止apache
    matlab练习程序(matlab调用c/c++)
    我的vim设置
    matlab练习程序(c/c++调用matlab<engine>)
  • 原文地址:https://www.cnblogs.com/nontracey/p/9963298.html
Copyright © 2011-2022 走看看