zoukankan      html  css  js  c++  java
  • Ocelot使用

    1、在网关项目中通过nuget引入Ocelot
    2、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.HttpsPolicy;
    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;
    
    namespace ApiGateway
    {
        public class Startup
        {
            public Startup(IConfiguration configuration, IHostingEnvironment env)
            {
                //通过nuget引入如下包
                //Microsoft.Extensions.Options.ConfigurationExtensions
                //Microsoft.Extensions.Configuration.Json
    
                var builder = new ConfigurationBuilder()
                    .SetBasePath(env.ContentRootPath)
                    .AddJsonFile("configuration.json", true, true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json");
    
                Configuration = builder.Build();
            }
    
            public IConfiguration Configuration { get; }
    
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
                services.AddOcelot(Configuration);
            }
    
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseHsts();
                }
    
                app.UseOcelot();
                app.UseHttpsRedirection();
                app.UseMvc();
            }
        }
    }
    

    3、添加网关支持配置configuration.json

    {
      "ReRoutes": [
        {
          "DownstreamPathTemplate": "/api/order", /*下游路径模板*/
          "DownstreamScheme": "http",
          "DownstreamHostAndPorts": [
            {
              "Host": "localhost",
              "Port": 9001
            }
          ],
          "UpstreamPathTemplate": "/order", /*上游路径模板*/
          "UpstreamHttpMethod": [ "Get" ]
        },
    
        {
          "DownstreamPathTemplate": "/api/product",
          "DownstreamScheme": "http",
          "DownstreamHostAndPorts": [
            {
              "Host": "localhost",
              "Port": 9002
            }
          ],
          "UpstreamPathTemplate": "/product",
          "UpstreamHttpMethod": [ "Get" ]
        }
      ],
      "GlobalConfiguration": {
        "RequestIdKey": "OcRequestId",
        "AdministrationPath" :  "administration"
      }
    }
    

    4、运行网关项目验证,访问http://localhost:9000/product进行验证,localhost:9000是你网关项目的host
    参考链接:https://www.cnblogs.com/yotsuki/p/7928095.html

  • 相关阅读:
    线程池的使用和实现
    VS2015中搭建lua环境
    word2vect实战
    微服务架构下的数据一致性:可靠事件模式
    微服务架构下的数据一致性:概念及相关模式
    深入研究Clang(七) Clang Lexer代码阅读笔记之Lexer
    [洛谷 P1402] 酒店之王|网络流
    [BZOJ2007] [NOI2010] 海拔|网络流80‘|最短路100’
    [JSOI2008] [洛谷P1198] 最大数|线段树80'|单调栈100'
    [JSOI2008] [洛谷P1227] 完美的对称|乱搞
  • 原文地址:https://www.cnblogs.com/huangzelin/p/9922580.html
Copyright © 2011-2022 走看看