.Netcore 2.0 Ocelot Api網(wǎng)關(guān)教程(1)
路由介紹
上一篇文章搭建了一個(gè)簡(jiǎn)單的Api網(wǎng)關(guān),可以實(shí)現(xiàn)簡(jiǎn)單的Api路由,本文介紹一下路由迟赃,即配置文件中ReRoutes惠毁,ReRoutes是Ocelot配置文件中最重要的部分,實(shí)現(xiàn)了由上游到下游的路由轉(zhuǎn)發(fā)袁辈。
上一篇文章中使用的configuration.json文件如下:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/values",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}
],
"UpstreamPathTemplate": "/webapia/values",
"UpstreamHttpMethod": [ "Get" ]
},
{
"DownstreamPathTemplate": "/api/values",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5002
}
],
"UpstreamPathTemplate": "/webapib/values",
"UpstreamHttpMethod": [ "Get" ]
}
]
}
Routes是一個(gè)數(shù)組,其中包含了若干個(gè)路由配置,上邊的配置文件中包含了2個(gè)路由配置减牺,以第一個(gè)為例介紹(以下簡(jiǎn)稱配置)。
- DownstreamPathTemplate:下游路徑
- DownstreamScheme:下游協(xié)議
- DownstreamHostAndPorts:下游主機(jī)及端口存谎,該部分為一個(gè)數(shù)組拔疚,包含若干個(gè)Host及Port配置
以上三個(gè)下游配置組成了下游路由的完整鏈接,配置的完整鏈接為:http://localhost:5001/api/values - UpstreamPathTemplate:上游路徑
- UpstreamHttpMethod:上游使用的http方法既荚,該部分為一個(gè)數(shù)組稚失,包含若干個(gè)http方法,配置中使用的為get方法
如此組成了一個(gè)路由配置恰聘,具體實(shí)現(xiàn)的功能為:當(dāng)Ocelot網(wǎng)關(guān)接收到鏈接為http(s)://yourdomain.com(:port)/webapia/values的get方法時(shí)轉(zhuǎn)發(fā)到http://localhost:5001/api/values
但是在我們的實(shí)際應(yīng)用中不會(huì)把所有鏈接都去配置一個(gè)路由(每個(gè)鏈接都去配置這不可能實(shí)現(xiàn))句各,Ocelot為我們提供了占位符(placeholder)。
占位符
繼續(xù)使用上一篇中創(chuàng)建的項(xiàng)目我們首先修改WebApiA中的ValuesController類中的public string Get(int id)
方法
[HttpGet("{id}")]
public string Get(int id)
{
return $"value {id} from WebApiA";
}
同樣晴叨,WebApiB
[HttpGet("{id}")]
public string Get(int id)
{
return $"value {id} from WebApiB";
}
然后向configuration.json配置文件中的ReRoutes節(jié)點(diǎn)添加如下配置:
{
"DownstreamPathTemplate": "/api/values/{id}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}],
"UpstreamPathTemplate": "/webapia/values/{id}",
"UpstreamHttpMethod": [ "Get" ]
},
{
"DownstreamPathTemplate": "/api/values/{id}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5002
}],
"UpstreamPathTemplate": "/webapib/values/{id}",
"UpstreamHttpMethod": [ "Get" ]
}
以上配置實(shí)現(xiàn)
- http(s)://yourdomain.com(:port)/webapia/values/{id} => http://localhost:5001/api/values/{id}
- http(s)://yourdomain.com(:port)/webapib/values/{id} => http://localhost:5002/api/values/{id}
分別運(yùn)行WebApiA凿宾、WebApiB、OcelotGetway兼蕊,之后瀏覽器分別訪問(wèn)http://http://localhost:5000/webapia/values/5 http://localhost:5000/webapib/values/6 運(yùn)行效果如下圖
運(yùn)行效果.png
如果需要上游鏈接對(duì)大小寫敏感可以添加ReRouteIsCaseSensitive
屬性初厚,該屬性默認(rèn)為false,修改配置文件如下(注意UpstreamPathTemplate鏈接):
{
"DownstreamPathTemplate": "/api/values/{id}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}],
"UpstreamPathTemplate": "/WebApiA/values/{id}",
"UpstreamHttpMethod": [ "Get" ],
"ReRouteIsCaseSensitive": true
},
{
"DownstreamPathTemplate": "/api/values/{id}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5002
}],
"UpstreamPathTemplate": "/WebApib/values/{id}",
"UpstreamHttpMethod": [ "Get" ]
}
再次運(yùn)行孙技,瀏覽器分別訪問(wèn)http://localhost:5000/WebApiA/values/5 http://localhost:5000/webapia/values/5惧所,可以發(fā)現(xiàn)大小寫拼寫有誤的鏈接已經(jīng)訪問(wèn)不了了
而另一個(gè)沒(méi)有添加ReRouteIsCaseSensitive的配置可以正常訪問(wèn)