环境准备

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

完成后在appsettings.json 添加节点

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

搭建过程

添加文件configuration.json

  1. {
  2. "ReRoutes": [
  3. // API:demo1
  4. {
  5. "UseServiceDiscovery": true,
  6. "DownstreamPathTemplate": "/api/{url}",
  7. "DownstreamScheme": "http",
  8. "ServiceName": "demoAPi",
  9. "LoadBalancerOptions": {
  10. "Type": "RoundRobin"
  11. },
  12. "UpstreamPathTemplate": "/demo1/{url}",
  13. "UpstreamHttpMethod": [ "Get", "Post" ],
  14. "ReRoutesCaseSensitive": false // non case sensitive
  15. }
  16. //,
  17. //// API:demo2
  18. //{
  19. // "UseServiceDiscovery": true,
  20. // "DownstreamPathTemplate": "/api/{url}",
  21. // "DownstreamScheme": "http",
  22. // "ServiceName": "demoAPi2",
  23. // "LoadBalancerOptions": {
  24. // "Type": "RoundRobin"
  25. // },
  26. // "UpstreamPathTemplate": "/demo2/{url}",
  27. // "UpstreamHttpMethod": [ "Get", "Post" ],
  28. // "ReRoutesCaseSensitive": false // non case sensitive
  29. //}
  30. ],
  31. "GlobalConfiguration": {
  32. "ServiceDiscoveryProvider": {
  33. "Host": "localhost", // Consul Service IP
  34. "Port": // Consul Service Port
  35. }
  36. }
  37. }

configuration.json

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

修改Program.cs 如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.Logging;
  10.  
  11. namespace APIGateWay
  12. {
  13. public class Program
  14. {
  15. public static string StartPort;
  16. public static void Main(string[] args)
  17. {
  18. var config = new ConfigurationBuilder()
  19. .SetBasePath(Directory.GetCurrentDirectory())
  20. .AddJsonFile("appsettings.json", optional: true)
  21. .Build();
  22. StartPort = config.GetSection("Setting")["Port"];
  23. CreateWebHostBuilder(args).Build().Run();
  24. }
  25.  
  26. public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
  27. WebHost.CreateDefaultBuilder(args)
  28. .UseStartup<Startup>()
  29. .UseUrls($"http://*:{StartPort}")
  30. .ConfigureAppConfiguration((hostingContext, builder) =>
  31. {
  32. builder.AddJsonFile("configuration.json", false, true);
  33. });
  34. }
  35. }

Program

添加 Ocelot.Provider.Consul nuget引用

修改startup.cs文件为

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.Extensions.Logging;
  11. using Microsoft.Extensions.Options;
  12. using Ocelot.DependencyInjection;
  13. using Ocelot.Middleware;
  14. using Ocelot.Provider.Consul;
  15.  
  16. namespace APIGateWay
  17. {
  18. public class Startup
  19. {
  20. public Startup(IConfiguration configuration)
  21. {
  22. Configuration = configuration;
  23. }
  24.  
  25. public IConfiguration Configuration { get; }
  26.  
  27. // This method gets called by the runtime. Use this method to add services to the container.
  28. public void ConfigureServices(IServiceCollection services)
  29. {
  30. services.AddOcelot(Configuration).AddConsul();
  31. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  32. }
  33.  
  34. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  35. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  36. {
  37. if (env.IsDevelopment())
  38. {
  39. app.UseDeveloperExceptionPage();
  40. }
  41.  
  42. //app.UseMvc();//no need
  43. app.UseOcelot().Wait();
  44. }
  45. }
  46. }

Startup

注意事项:

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

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

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

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

微服务网关从零搭建——(二)搭建api网关(不带验证)的更多相关文章

  1. 实测 | 转型微服务,这4大工具谁是API网关性能最优?

    转自:http://www.servicemesh.cn/?/article/45 作者:Turgay Çelik 翻译:钟毅(Drew Zhong) 原文:Comparing API Gateway ...

  2. NET Core微服务之路:基于Ocelot的API网关Relay实现--RPC篇

    前言 我们都知道,API网关是工作在应用层上网关程序,为何要这样设计呢,而不是将网关程序直接工作在传输层.或者网络层等等更底层的环境呢?让我们先来简单的了解一下TCP/IP的五层模型.     (图片 ...

  3. Spring Cloud 微服务二:API网关spring cloud zuul

    前言:本章将继续上一章Spring Cloud微服务,本章主要内容是API 网关,相关代码将延续上一章,如需了解请参考:Spring Cloud 微服务一:Consul注册中心 Spring clou ...

  4. .Net Core微服务入门全纪录(五)——Ocelot-API网关(下)

    前言 上一篇[.Net Core微服务入门全纪录(四)--Ocelot-API网关(上)]已经完成了Ocelot网关的基本搭建,实现了服务入口的统一.当然,这只是API网关的一个最基本功能,它的进阶功 ...

  5. JHipster生成微服务架构的应用栈(四)- 网关微服务示例

    本系列文章演示如何用JHipster生成一个微服务架构风格的应用栈. 环境需求:安装好JHipster开发环境的CentOS 7.4(参考这里) 应用栈名称:appstack 认证微服务: uaa 业 ...

  6. 微服务之:从零搭建ocelot网关和consul集群

    介绍 微服务中有关键的几项技术,其中网关和服务服务发现,服务注册相辅相成. 首先解释几个本次教程中需要的术语 网关 Gateway(API GW / API 网关),顾名思义,是企业 IT 在系统边界 ...

  7. SpringCloud微服务实战——搭建企业级开发框架(四十五):【微服务监控告警实现方式二】使用Actuator(Micrometer)+Prometheus+Grafana实现完整的微服务监控

      无论是使用SpringBootAdmin还是使用Prometheus+Grafana都离不开SpringBoot提供的核心组件Actuator.提到Actuator,又不得不提Micrometer ...

  8. docker微服务部署之:三,搭建Zuul微服务项目

    docker微服务部署之:二.搭建文章微服务项目 一.新增demo_eureka模块,并编写代码 右键demo_parent->new->Module->Maven,选择Module ...

  9. docker微服务部署之:一,搭建Eureka微服务项目

    先说明一下docker需要搭建的微服务的基本情况: 项目情况:一个demo_parent项目,下面三个子模块:demo_eureka(eureka服务).demo_article(文章服务).demo ...

  10. 微服务架构:Eureka集群搭建

    版权声明:本文为博主原创文章,转载请注明出处,欢迎交流学习! 服务注册.发现是微服务架构的关键原理之一,由于微服务架构是由一系列职责单一的细粒度服务构成的网状结构,服务之间通过轻量机制进行通信,这就必 ...

随机推荐

  1. HNOI模拟 Day3.22

    第一题: 盾盾的打字机 (drdrd) [题目描述] 盾盾有一个非常有意思的打字机,现在盾哥要用这台打字机来打出一段文章. 由于有了上次的经验,盾盾预先准备好了一段模板 A 存在了内存中,并以此为基础 ...

  2. 1.jdk安装和环境配置

    这个简单,但是记不住,非要网上搜一遍不可: 1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值:C:\Progr ...

  3. 嵌入式Linux系统---ppp拨号,4G模块上网【转】

    本文转载自:http://blog.csdn.net/qq562029186/article/details/65438553 4G模块PPP拨号上网 方法1 所需文件: xxx-chat-conne ...

  4. ResolveUrl in external JavaScript file in asp.net project

    https://stackoverflow.com/questions/11263425/page-resolveurl-is-not-working-in-javascript The proble ...

  5. 【Silverlight】Bing Maps学习系列(六):使用扩展模式(Extended Modes)(转)

    [Silverlight]Bing Maps学习系列(六):使用扩展模式(Extended Modes) 微软Bing Maps推出有有段时间了,通过不断的改进和新的地图更新,现在已经基本上形成了一套 ...

  6. 分布式消息中间件Rabbit Mq的了解与使用

    MQ(消息队列)作为现代比较流行的技术,在互联网应用平台中作为中间件,主要解决了应用解耦.异步通信.流量削锋.服务总线等问题,为实现高并发.高可用.高伸缩的企业应用提供了条件. 目前市面比较流行的消息 ...

  7. nginx开发(一) 源码-编译

    1:获取源码 http://nginx.org/download/nginx-1.8.0.tar.gz 2:编译 解压之后,进入根目录,执行 ./configuer.sh make make inst ...

  8. gitlab调试

    Bundle complete! 104 Gemfile dependencies, 161 gems now installed.Gems in the groups development, te ...

  9. Hibernate的表之间的关系

    表:A.B 一对多关系:A中的一个字段的记录(主键)引用B中的多个记录(外键) 多对一关系:A中的一个字段的记录(外键)引用B中的一个记录(主键) 一对一关系:A.B两个表的关联字段都是主键 多对多关 ...

  10. WPF-CheckBox(复选框、功能开关)美化

    老规矩,先放图 按钮美化背景: 由于特殊需求,复选框样式单一,所以我们需要将其按钮重构和美化达到我们的需求 复选框美化思维引导: 图中1为背景色 图中2为边框 图中3为句柄控件组成(Path+Rect ...