Ocelot介绍
公司打算将以前的系统利用core进行重构并且向微服务进军,趁着目前手上任务不是很多也就慢慢进军了微服务,本篇章是我对微服务实践的开篇,后续会逐步介绍到Ocelot、Consul、IdentityServer相关知识。
Ocelot是一个基于.net core的开源webapi 服务网关项目,目前已经集成了IdentityServer认证。Ocelot本质上是一堆中间件的集合,当HttpRequest请求到达后由一堆中间件进行处理,处理完毕,请求根据配置转发给下游服务。然后接受下游服务的返回信息在转发给客户端,这样就避免了在调用的时候记录复杂的服务ip与端口,只需要使用Ocelot的ip/端口即可。详细信息请参考 官方文档:https://ocelot.readthedocs.io。
安装Ocelot
在管理nuget程序中搜索Ocelot进行安装。非常简单就不多说。
配置路由
{
"ReRoutes": [
// API:Service1
{
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": "5011"
}
],
"UpstreamPathTemplate": "/Service1/{url}",
"UpstreamHttpMethod": [ "Get", "Post" ]
},
// API:Service2
{
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",