.Netcore 2.0 Ocelot Api网关教程(4)- 服务发现
本文介绍Ocelot中的服务发现(Service Discovery),Ocelot允许指定一个服务发现提供器,之后将从中寻找下游服务的host和port来进行请求路由。
关于服务发现的详细介绍请点击。
在Ocelot中使用了Consul作为服务发现的provider。
1、Consul下载安装
从官方下载页选择合适的平台下载,解压出一个二进制文件并保存到相应位置,并将路径存入path中,本文以windows版本为例(其他平台操作类似)。
打开 cmd/powershell 运行 consul agent -dev
输出如下文本表示成功以dev方式启动consul
==> Starting Consul agent...
==> Consul agent running!
Version: 'v1.0.7'
Node ID: '3fc1edca-b635-56cc-b767-01a942423f73'
Node name: 'Weidaicheng-PC'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false
==> Log data will now stream in as it occurs:
2018/04/29 09:22:03 [DEBUG] agent: Using random ID "3fc1edca-b635-56cc-b767-01a942423f73" as node ID
2018/04/29 09:22:03 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:3fc1edca-b635-56cc-b767-01a942423f73 Address:127.0.0.1:8300}]
2018/04/29 09:22:03 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
2018/04/29 09:22:03 [INFO] serf: EventMemberJoin: Weidaicheng-PC.dc1 127.0.0.1
2018/04/29 09:22:03 [INFO] serf: EventMemberJoin: Weidaicheng-PC 127.0.0.1
2018/04/29 09:22:03 [INFO] consul: Adding LAN server Weidaicheng-PC (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2018/04/29 09:22:03 [INFO] consul: Handled member-join event for server "Weidaicheng-PC.dc1" in area "wan"
2018/04/29 09:22:03 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
2018/04/29 09:22:03 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
2018/04/29 09:22:03 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
2018/04/29 09:22:03 [INFO] agent: started state syncer
2018/04/29 09:22:03 [WARN] raft: Heartbeat timeout from "" reached, starting election
2018/04/29 09:22:03 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
2018/04/29 09:22:03 [DEBUG] raft: Votes needed: 1
2018/04/29 09:22:03 [DEBUG] raft: Vote granted from 3fc1edca-b635-56cc-b767-01a942423f73 in term 2. Tally: 1
2018/04/29 09:22:03 [INFO] raft: Election won. Tally: 1
2018/04/29 09:22:03 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2018/04/29 09:22:03 [INFO] consul: cluster leadership acquired
2018/04/29 09:22:03 [INFO] consul: New leader elected: Weidaicheng-PC
2018/04/29 09:22:03 [DEBUG] consul: Skipping self join check for "Weidaicheng-PC" since the cluster is too small
2018/04/29 09:22:03 [INFO] consul: member 'Weidaicheng-PC' joined, marking health alive
2018/04/29 09:22:03 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2018/04/29 09:22:03 [INFO] agent: Synced node info
2018/04/29 09:22:03 [DEBUG] agent: Node info in sync
2018/04/29 09:22:04 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2018/04/29 09:22:04 [DEBUG] agent: Node info in sync
浏览器输入http://localhost:8500可以看到UI后台界面
或者打开另一个PowerShell输入 consul members
输出如下
Node Address Status Type Build Protocol DC Segment
Weidaicheng-PC 127.0.0.1:8301 alive server 1.0.7 2 dc1 <all>
至此,Consul安装完成
2、代码修改
继续使用上一篇的项目,在WebApiA和WebApiB中分别新建一个api控制器 CounterController
代码如下
//WebApiA
using Microsoft.AspNetCore.Mvc;
namespace WebApiA.Controllers
{
[Produces("application/json")]
[Route("api/[controller]/[action]")]
public class CounterController : Controller
{
private static int _count = 0;
[HttpGet]
public string Count()
{
return $"Count {++_count} from WebapiA";
}
}
}
//WebApiB
using Microsoft.AspNetCore.Mvc;
namespace WebApiB.Controllers
{
[Produces("application/json")]
[Route("api/[controller]/[action]")]
public class CounterController : Controller
{
private static int _count = 0;
[HttpGet]
public string Count()
{
return $"Count {++_count} from WebapiB";
}
}
}
修改OcelotGetway项目中的配置文件 configuration.json
在 ReRoutes
节点中添加
{
"DownstreamPathTemplate": "/api/Counter/Count",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/count",
"UpstreamHttpMethod": [ "Get" ],
"ServiceName": "Count",
"LoadBalancer": "RoundRobin",
"UseServiceDiscovery": true
}
其中,ServiceName指定服务发现的服务名称,在之后Consul中注册服务的时候会用到,LoadBalancer指定使用的负载均衡模式(目前Ocelot仅支持时间片轮询(RoundRobin)和最少连接(LeastConnection)模式),UseServiceDiscovery标志使用服务发现。
注:如果不指定负载均衡,将不使用负载均衡,本文中使用时间片轮询为例。
然后再根节点添加全局配置
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500
}
}
指定服务发现提供器的位置。目前Ocelot只支持共用一个provider。
3、注册服务
在consul所在的目录下创建文件夹 consul.d
,文件夹位置及文件夹名称可以随意,进入consul.d文件夹中创建两个json文件 Count_1.json
和 Count_2.json
,分别添加如下文本
{
"service": {
"id": "count1",
"name": "Count",
"tags": ["dev"],
"address": "localhost",
"port": 5001
}
}
{
"service": {
"id": "count2",
"name": "Count",
"tags": ["dev"],
"address": "localhost",
"port": 5002
}
}
以第一个为例,注册一个服务id为count1,服务名称为Count,和之前配置中使用的相同,还有地址和端口。
使用 Ctrl+C
关闭正在运行的consul,然后输入 consul agent -dev -config-dir=C:/consul_1.0.7/consul.d
其中 C:/consul_1.0.7/consul.d
是本机的配置文件路径。
可以看到如下输出
==> Starting Consul agent...
==> Consul agent running!
Version: 'v1.0.7'
Node ID: '91af5002-a1d6-409e-e36b-f7a081f84d24'
Node name: 'Weidaicheng-PC'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false
==> Log data will now stream in as it occurs:
2018/04/29 09:41:38 [DEBUG] agent: Using random ID "91af5002-a1d6-409e-e36b-f7a081f84d24" as node ID
2018/04/29 09:41:38 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:91af5002-a1d6-409e-e36b-f7a081f84d24 Address:127.0.0.1:8300}]
2018/04/29 09:41:38 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
2018/04/29 09:41:38 [INFO] serf: EventMemberJoin: Weidaicheng-PC.dc1 127.0.0.1
2018/04/29 09:41:38 [INFO] serf: EventMemberJoin: Weidaicheng-PC 127.0.0.1
2018/04/29 09:41:38 [INFO] consul: Adding LAN server Weidaicheng-PC (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2018/04/29 09:41:38 [INFO] consul: Handled member-join event for server "Weidaicheng-PC.dc1" in area "wan"
2018/04/29 09:41:38 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
2018/04/29 09:41:38 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
2018/04/29 09:41:38 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
2018/04/29 09:41:38 [INFO] agent: started state syncer
2018/04/29 09:41:38 [WARN] raft: Heartbeat timeout from "" reached, starting election
2018/04/29 09:41:38 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
2018/04/29 09:41:38 [DEBUG] raft: Votes needed: 1
2018/04/29 09:41:38 [DEBUG] raft: Vote granted from 91af5002-a1d6-409e-e36b-f7a081f84d24 in term 2. Tally: 1
2018/04/29 09:41:38 [INFO] raft: Election won. Tally: 1
2018/04/29 09:41:38 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2018/04/29 09:41:38 [INFO] consul: cluster leadership acquired
2018/04/29 09:41:38 [INFO] consul: New leader elected: Weidaicheng-PC
2018/04/29 09:41:38 [DEBUG] consul: Skipping self join check for "Weidaicheng-PC" since the cluster is too small
2018/04/29 09:41:38 [INFO] consul: member 'Weidaicheng-PC' joined, marking health alive
2018/04/29 09:41:39 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2018/04/29 09:41:39 [INFO] agent: Synced service "count1"
2018/04/29 09:41:39 [INFO] agent: Synced service "count2"
2018/04/29 09:41:39 [DEBUG] agent: Node info in sync
2018/04/29 09:41:39 [DEBUG] agent: Service "count1" in sync
2018/04/29 09:41:39 [DEBUG] agent: Service "count2" in sync
2018/04/29 09:41:39 [DEBUG] agent: Node info in sync
2018/04/29 09:41:39 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2018/04/29 09:41:39 [DEBUG] agent: Service "count1" in sync
2018/04/29 09:41:39 [DEBUG] agent: Service "count2" in sync
2018/04/29 09:41:39 [DEBUG] agent: Node info in sync
在倒数第三行和倒数第二行可以看到count1和count2的两个服务注册成功,使用postman/浏览器请求一下http://localhost:8500/v1/catalog/service/count
可以看到注册的两个Count服务的信息
[
{
"ID": "91af5002-a1d6-409e-e36b-f7a081f84d24",
"Node": "Weidaicheng-PC",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceID": "count1",
"ServiceName": "Count",
"ServiceTags": [
"dev"
],
"ServiceAddress": "localhost",
"ServiceMeta": {},
"ServicePort": 5001,
"ServiceEnableTagOverride": false,
"CreateIndex": 6,
"ModifyIndex": 6
},
{
"ID": "91af5002-a1d6-409e-e36b-f7a081f84d24",
"Node": "Weidaicheng-PC",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceID": "count2",
"ServiceName": "Count",
"ServiceTags": [
"dev"
],
"ServiceAddress": "localhost",
"ServiceMeta": {},
"ServicePort": 5002,
"ServiceEnableTagOverride": false,
"CreateIndex": 7,
"ModifyIndex": 7
}
]
4、运行与结果查看
分别运行WebApiA、WebApiB、OcelotGatway三个项目,多次请求http://localhost:5000/Count,效果图如下
可以看到,多次请求依次访问A、B、A......正是时间片轮询的负载均衡模式。
完,下一篇介绍认证和授权
参考链接
https://blog.csdn.net/mr_seaturtle_/article/details/77618403
http://ocelot.readthedocs.io/en/latest/features/servicediscovery.html
https://www.consul.io/intro/getting-started/install.html
9人点赞
作者:Weidaicheng
链接:https://www.jianshu.com/p/05a1bf2545a0
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
.Netcore 2.0 Ocelot Api网关教程(4)- 服务发现的更多相关文章
- .Netcore 2.0 Ocelot Api网关教程(2)- 路由
.Netcore 2.0 Ocelot Api网关教程(1) 路由介绍 上一篇文章搭建了一个简单的Api网关,可以实现简单的Api路由,本文介绍一下路由,即配置文件中ReRoutes,ReRoutes ...
- .Netcore 2.0 Ocelot Api网关教程(6)- 配置管理
本文介绍Ocelot中的配置管理,配置管理允许在Api网关运行时动态通过Http Api查看/修改当前配置.由于该功能权限很高,所以需要授权才能进行相关操作.有两种方式来认证,外部Identity S ...
- .Netcore 2.0 Ocelot Api网关教程(7)- 限流
本文介绍Ocelot中的限流,限流允许Api网关控制一段时间内特定api的总访问次数.限流的使用非常简单,只需要添加配置即可. 1.添加限流 修改 configuration.json 配置文件,对 ...
- .Netcore 2.0 Ocelot Api网关教程(10)- Headers Transformation
本文介绍Ocelot中的请求头传递(Headers Transformation),其可以改变上游request传递给下游/下游response传递给上游的header. 1.修改ValuesCont ...
- .Netcore 2.0 Ocelot Api网关教程(5)- 认证和授权
本文介绍Ocelot中的认证和授权(通过IdentityServer4),本文只使用最简单的IdentityServer,不会对IdentityServer4进行过多讲解. 1.Identity Se ...
- .Netcore 2.0 Ocelot Api网关教程(1)- 入门
Ocelot(Github)Ocelot官方文档(英文)本文不会介绍Api网关是什么以及Ocelot能干什么需要对Api网关及Ocelot有一定的理论了解 开始使用Ocelot搭建一个入门级Api网关 ...
- .Netcore 2.0 Ocelot Api网关教程(3)- 路由聚合
在实际的应用当中,经常会遇到同一个操作要请求多个api来执行.这里先假设一个应用场景:通过姓名获取一个人的个人信息(性别.年龄),而获取每种个人信息都要调用不同的api,难道要依次调用吗?在Ocelo ...
- .Netcore 2.0 Ocelot Api网关教程(9)- QoS
本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly ...
- .Netcore 2.0 Ocelot Api网关教程(8)- 缓存
Ocelot中使用 CacheManager 来支持缓存,官方文档中强烈建议使用该包作为缓存工具.以下介绍通过使用CacheManager来实现Ocelot缓存. 1.通过Nuget添加 Ocelot ...
随机推荐
- FFmpeg常用命令学习笔记(一)基本信息查询命令
笔者才开始学习音视频开发,FFmpeg学习笔记系列主要是从慕课网李超老师的FFmpeg音视频核心技术精讲与实战课程学习的心得体会. FFmpeg音视频核心技术精讲与实战:https://coding. ...
- ak-1
最近研究ak,网上也有很多这方面的资料,就不重复叙述了,本次记录就是自己在做适应时的一些记录. 本次环境 中标麒麟 金蝶apusic 人大金仓 先说说东西从哪下载怎么来的 基本都是通过官网打电话申请 ...
- Path variable [contentHash:8] not implemented in this context: styles.[contentHash:8].css
webPack 升级到 4.3.0 导致 extract-text-webpack-plugin 无法使用
- ProjectEuler237 Tours on a 4 x n playing board
思路是这样的 插头dp-->打表-->OEIS查表-->通项公式-->矩阵快速幂优化线性递推 OEIS竟然有这个东西的生成函数啊 答案为15836928 这是最终代码 #inc ...
- enum枚举变量
如果一个变量你需要几种可能存在的值,那么就可以被定义成为枚举类型.之所以叫枚举就是说将变量或者叫对象可能存在的情况也可以说是可能的值一一例举出来. 一个铅笔盒中有一支笔,但在没有打开之前你并不知道它是 ...
- BZOJ 3772: 精神污染 (dfs序+树状数组)
跟 BZOJ 4009: [HNOI2015]接水果一样- CODE #include <set> #include <queue> #include <cctype&g ...
- jmeter结果树乱码的解决方案
- jQuery系列(十四):jQuery中的ajax
1.什么是ajax AJAX = 异步的javascript和XML(Asynchronous Javascript and XML) 简言之,在不重载整个网页的情况下,AJAX通过后台加载数据,并在 ...
- 浅谈C++运算符重载
首先,什么是运算符重载? C++里运算符重载主要有两种方法.一是通过类的成员函数进行重载,二是通过类的友元函数进行重载. 下面以简单的复数类complex为例: 下面是通过类的成员函数进行运算符的重载 ...
- IP地址正则表达式的写法
IP地址的正则表达式写法 这里讲的是IPv4的地址格式,总长度 32位=4段*8位,每段之间用.分割, 每段都是0-255之间的十进制数值. 将0-255用正则表达式表示,可以分成一下几块来分别考虑: ...