微服务(入门三):netcore ocelot api网关结合consul服务发现
简介
api网关是提供给外部调用的统一入口,类似于dns,所有的请求统一先到api网关,由api网关进行指定内网链接。
ocelot是基于netcore开发的开源API网关项目,功能强大,使用方便,它包含了负载均衡、路由、请求聚合、服务发现、权限认证等功能。
基础准备
开发环境:vs2017
netcore:2.1
新建项目

netcore安装ocelot
- install-package Ocelot 安装ocelot组件
配置ocelot
1.添加ocelotSettings.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 ocelotTest
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
} public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://localhost:8683")
.ConfigureAppConfiguration(conf => {
conf.AddJsonFile("OcelotSettings.json", optional: false, reloadOnChange: true);
})
.Build();
}
}
2.注入ocelot服务
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;
using Ocelot.Provider.Polly; namespace ocelotTest
{
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)
{
//注入ocelot服务
services.AddOcelot(Configuration).AddConsul().AddPolly();
} // 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.UseOcelot().Wait();
}
}
}
3.配置ocelotSettings.json
{
"ReRoutes": [
{
//下游路由模板,真实请求的路径
"DownstreamPathTemplate": "/api/{everything}",
//请求的方式,例如:http,https
"DownstreamScheme": "http",
//服务器名称
"ServiceName": "zyz",
//启用consul服务
"UseServiceDiscovery": true,
//服务熔断
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": , //允许多少次异常请求
"DurationOfBreak": , //熔断时间,单位为秒
"TimeoutValue": //如果下游请求的处理时间超过多少则自动设置超时
},
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false,
"UseTracing": false
},
"ReRouteIsCaseSensitive": false,
//负载均衡:
//RoundRobin轮流发送;
//LeastConnection – 将请求发往最空闲的那个服务器
//NoLoadBalance – 总是发往第一个请求或者是服务发现
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
//上游地址配置
"UpstreamPathTemplate": "/test/{everything}",
//上游支持的请求类型
"UpstreamHttpMethod": [ "Post", "Get" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:8683",
//consul服务器地址和ip
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port":
}
}
}
4.启动ocelot项目和consul服务。
把服务注册到consul当中,通过postman发送请求测试,成功转发消息,并且实现负载均衡。


小结:简单的ocelot搭建完成,后续的一些扩展功能慢慢在研究。
快速入口:微服务(入门一):netcore安装部署consul
快速入口: 微服务(入门二):netcore通过consul注册服务
快速入口: 微服务(入门三):netcore ocelot api网关结合consul服务发现
快速入口:微服务(入门四):identityServer的简单使用(客户端授权)
微服务(入门三):netcore ocelot api网关结合consul服务发现的更多相关文章
- .NET Core微服务二:Ocelot API网关
.NET Core微服务一:Consul服务中心 .NET Core微服务二:Ocelot API网关 .NET Core微服务三:polly熔断与降级 本文的项目代码,在文章结尾处可以下载. 本文使 ...
- .Netcore 2.0 Ocelot Api网关教程(2)- 路由
.Netcore 2.0 Ocelot Api网关教程(1) 路由介绍 上一篇文章搭建了一个简单的Api网关,可以实现简单的Api路由,本文介绍一下路由,即配置文件中ReRoutes,ReRoutes ...
- Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(三)
在前面两篇文章中,我介绍了基于IdentityServer4的一个Identity Service的实现,并且实现了一个Weather API和基于Ocelot的API网关,然后实现了通过Ocelot ...
- 微服务基础——厉害了!API网关
微服务刚刚诞生的时候,人们将服务进行拆分,实现服务之间的松耦合,并且每个服务有专门的团队维护,然后客户端直接和各个子服务进行交互.比如,订单,商品,会员服务. 那么这种客户端直接和后端服务交互的方式会 ...
- .Netcore 2.0 Ocelot Api网关教程(7)- 限流
本文介绍Ocelot中的限流,限流允许Api网关控制一段时间内特定api的总访问次数.限流的使用非常简单,只需要添加配置即可. 1.添加限流 修改 configuration.json 配置文件,对 ...
- .Netcore 2.0 Ocelot Api网关教程(6)- 配置管理
本文介绍Ocelot中的配置管理,配置管理允许在Api网关运行时动态通过Http Api查看/修改当前配置.由于该功能权限很高,所以需要授权才能进行相关操作.有两种方式来认证,外部Identity S ...
- Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(四)
在上一讲中,我们已经完成了一个完整的案例,在这个案例中,我们可以通过Angular单页面应用(SPA)进行登录,然后通过后端的Ocelot API网关整合IdentityServer4完成身份认证.在 ...
- Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(二)
上文已经介绍了Identity Service的实现过程.今天我们继续,实现一个简单的Weather API和一个基于Ocelot的API网关. 回顾 <Angular SPA基于Ocelot ...
- ASP.NET Core on K8S学习之旅(13)Ocelot API网关接入
本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章. 上一篇介绍了Ingress的基本概念和Nginx Ingress的基本配置和使 ...
随机推荐
- Unity5 assetbundle笔记
Assetbundle api试验----打包选项试验--------结论:BuildAssetBundleOptions说明:------------None: 把所有以来资源到到一个包里----- ...
- 洛谷 P1054 解题报告
P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式,题目的 ...
- 关于HTML、CSS、JavaScript三者关系的简述
总述 我对于网页这部分的理解吧,对于静态网页来说,无非分为三部分:第一部分.HTML,第二部分.CSS,第三部分.JavaScript(这部分暂且放一边).这俩个给我的第一印象就是,这你妹的都是一串串 ...
- linux下c编程 基础
1. 熟悉Linux系统下的开发环境 2. 熟悉vi的基本操作 3. 熟悉gcc编译器的基本原理 4. 熟练使用gcc编译器的常用选项 5 .熟练使用gdb调试技术 6. 熟悉makefile基本原理 ...
- maven的pom文件中配置测试用例
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...
- SSM-SpringMVC-21:SpringMVC中处理器方法之返回值Object篇
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 今天要记录的是处理方法,返回值为Object的那种,我给它分了一下类: 1.返回值为Object数值(例如1) ...
- 通过VirtualBox安装Linux系统(CentOS7)
本文目的:创建虚拟系统.在windows系统中通过虚拟工具VirtualBox创建一个虚拟系统CentOS. 备注:(1)版本如下:VirtualBox-5.2.12-122591-Win 和Cent ...
- vm12 和14密钥
vm12:5A02H-AU243-TZJ49-GTC7K-3C61N vm14:FF31K-AHZD1-H8ETZ-8WWEZ-WUUVA
- python一行代码就能搞定的事情!
打印9*9乘法表: >>> print( '\n'.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)]) ...
- Java 字符流文件读写
上篇文章,我们介绍了 Java 的文件字节流框架中的相关内容,而我们本篇文章将着重于文件字符流的相关内容. 首先需要明确一点的是,字节流处理文件的时候是基于字节的,而字符流处理文件则是基于一个个字符为 ...