1首先创建一个json的配置文件,文件名随便取,我取Ocelot.json

这个配置文件有两种配置方式,第一种,手动填写 服务所在的ip和端口;第二种,用Consul进行服务发现

第一种如下:

{
"ReRoutes": [
{
//转发处理格式
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
//手动指明ip和端口号
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 6001
}
],
//请求格式
"UpstreamPathTemplate": "/Ocelot_Consul_Service/{url}",
"UpstreamHttpMethod": [ "Get", "Post" ]
}
]
//例如,我的Ocelot ip是127.0.0.1 端口是8888的情况下,
//我请求的是localhost:8888/Ocelot_Consul_Service/values
//会被转到localhost 的6001端口 6001端口对应的是 Ocelot_Consul_Service 对应的webapi
//请求转后的路径是:localhost:6001/api/Ocelot_Consul_Service/values
}

第二种如下:

{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http", "UpstreamPathTemplate": "/Ocelot_Consul_Service/{url}",
"UpstreamHttpMethod": [ "Get", "Post" ],
//指明服务名
"ServiceName": "Ocelot_Consul_Service",
//指明负载平衡方式
"LoadBalancerOptions": {
"Type": "RoundRobin" //轮询
},
//使用服务发现
"UseServiceDiscovery": true
} ],
//全局配置
"GlobalConfiguration": {
//服务发现的提供者
"ServiceDiscoveryProvider": {
//ip
"Host": "localhost",
//端口
"Port": 8500,
//由Consul提供服务发现
"Type": "Consul"
}
}
}

2.接下来我们要安装Ocelot  install-package Ocelot

3.安装完毕 要在Program.cs文件中使用第一步中创建的json文件,把它读到配置里面去。

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
//解析出从控制台传入的ip和端口号
var config = new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
string ip = config["ip"];
string port = config["port"]; return WebHost.CreateDefaultBuilder(args)
.UseUrls($"http://{ip}:{port}")
//注册应用配置
.ConfigureAppConfiguration((hostingContext,builder)=> {
//false 此文件是否是可选的,不是!true 如果此文件被修改了是否重新加载 是!
builder.AddJsonFile("Ocelot.json", false, true);
})
.UseStartup<Startup>();
}

4.在启动类(startup.cs)文件中添加Ocelot服务

public void ConfigureServices(IServiceCollection services)
{
//这个AddOcelot方法是Ocelot包给IServiceCollection扩展的方法
//如果不使用Consul进行服务发现,只需要services.AddOcelot(configuration)即可
//但是如果使用Consul进行服务发现 后面还要AddConsul()
//要使用AddConsul()必须安装包 Ocelot.Provider.Consul
services.AddOcelot(configuration).AddConsul();
}

一定要注意第4步,使用Consul做服务发现要安装 Ocelot.Provider.Consul 包 并AddConsul()。在实际中 我们要尽量要用Consul进行服务发现。

附上Ocelot文档截图一张如下:

最新文章

  1. 每周一书《Oracle 12 c PL(SQL)程序设计终极指南》
  2. mysql实用操作
  3. enum使用
  4. View (五)自定义View的实现方法
  5. 结果集一组数据的第几条ROW_NUMBER基本用法
  6. Docker系列(三)常用命令
  7. KEIL C51中的_at_关键字
  8. 【git】error: Your local changes to the following files
  9. WIN7 数据源配置问题(32位&amp;&amp;64位)
  10. nosql和关系型数据库比较?
  11. nmon安装(转)
  12. mysql查询文章的评论数量
  13. offset系列,client系列,scroll系列回顾
  14. mongodb+express+nodejs(登陆退出)
  15. Request method &#39;POST&#39; not supported错误和解决方法
  16. Android 8 蓝牙打开过程
  17. [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]
  18. MongoDB数据导出
  19. 一篇关于CountDownLatch的好文章
  20. Python内置类型(6)——生成器

热门文章

  1. bootstrap 自定义
  2. .net core 2.0的一次奇特经历
  3. 免费视频教学:30天精通iPho…
  4. [iOS]UIScrollView左右拨动,第二页宽度只有一半问题
  5. 前端开发之javascript BOM篇
  6. 前端 webpack
  7. 解剖Nginx&#183;模块开发篇(1)跑起你的 Hello World 模块!
  8. TIMER_PWM_CAPTURE
  9. HUST软测1504班第4周小组作业成绩:WordCount优化
  10. 为 pycharm 修改 Theme &amp; Color