lmjbk

.Net Core Ocelot API网关

.Net Core Ocelot API网关

vs2017

.net core 2.2

 1.首先,我们新建三个API项目

 

 

 

  项目说明:

   1.ApiGetAway:网关项目

   2.ApiServer01/ApiServer02  api项目

 

  1.ApiGetAway项目:

  1.安装依赖包:Install-Package Ocelot

       2.新建配置文件:Ocelot.json

    修改配置文件Program.cs

  public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseUrls("http://localhost:9011") //端口
            .ConfigureAppConfiguration((hostingContext, builder) =>
            {
                builder.AddJsonFile("Ocelot.json", false, false);
            })
            .UseStartup<Startup>();
    }

  修改Startup.cs

  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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            //配置Ocelot
            services.AddOcelot(Configuration);
        }

        // 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();

            //配置Ocelot
            app.UseOcelot().Wait();
        }
    }

 

修改Ocelot.json

{
  "ReRoutes": [

    //ApiServer01
    {
      //下游请求地址,即APIGateway转发的目标服务地址
      "DownstreamPathTemplate": "/api/{url}",
      "DownstreamScheme": "http", //请求协议
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost", //请求服务器地址
          "Port": "9001" //端口号
        }
      ],
      //上游请求地址,即客户端请求APIGateway的地址
      "UpstreamPathTemplate": "/api01/{url}", //请求路径模板
      "UpstreamHttpMethod": [ "Get" ],
      "ReRouteIsCaseSensitive": false //大小写敏感 true:是 false:否
    },
    //ApiServer02
    {
      "DownstreamPathTemplate": "/api/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": "9002"
        }
      ],
      "UpstreamPathTemplate": "/api02/{url}",
      "UpstreamHttpMethod": [ "Get" ],
      "ReRouteIsCaseSensitive": false //大小写敏感 true:是 false:否
    }

  ]
}

 

修改Program.cs    ApiServer01,ApiServer02 的端口分别为9001,9002

 

 // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "ApiServer01", Guid.NewGuid().ToString() };
        }
 // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "ApiServer02", Guid.NewGuid().ToString() };
        }

 

重新生成项目,打开项目生成目录

 

   使用dotnet ApiServer01.dll   dotnet ApiServer02.dll   dotnet ApiGetAway.dll  分别运行项目

 

使用浏览器访问网关api

完结..

posted on 2019-06-14 11:39 LiMis 阅读(...) 评论(...) 编辑 收藏

分类:

技术点:

相关文章: