consul配置完成后

新建.netcoreapi项目,

nuget安装ocelot

添加多个配置文件,.netcore中会自动合并为一个文件,global配置总的配置,其他为各个项目的配置

ServiceName为consul中配置的服务名称:

{
"ReRoutes": [
{
"DownstreamPathTemplate": "/Api/{everything}",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/Api/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"LoadBalancerOptions": {
//"Type": "RoundRobin",
"Type": "LeastConnection"
},
"ServiceName": "Api",
"UseServiceDiscovery ": true
}
]
}

配置服务发现:ocelot.global.json

{
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Host": "47.92.80.220",
"Port": ,
"Type": "Consul"
}
}
}

在program.cs中添加json文件,注意AddOcelot

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
//.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
.AddOcelot( hostingContext.HostingEnvironment)
.AddEnvironmentVariables();
})
//.UseUrls("http://localhost:1000")
.UseStartup<Startup>();

在startup.cs的ConfigureServices中注入

services.AddOcelot(Configuration)
.AddConsul();

在Configure中

app.UseOcelot().Wait();

至此就完成了配置,启动项目后根据配置的路由找到ServiceName,然后会访问服务发现中的路由

最新文章

  1. python fork 用法
  2. [Node.js] DSL in action
  3. JavaScript制作时钟特效
  4. Lvs原理
  5. ftp如何预览图片 解决方案
  6. Permutation Sequence [LeetCode]
  7. Redis配置文件解读
  8. HDFS 搭建记录
  9. Js 旋转平滑特效
  10. jsp验证表单后再提交
  11. POJ 1515 Street Directions (边双连通)
  12. 前端性能优化成神之路--SSR(服务端渲染)
  13. localstorage实现网页状态记录比如放音乐功能的实例
  14. Python pickle模块
  15. JNUOJ 1032 - 食物处理器
  16. STM32 CAN总线标识符过滤器难点解析
  17. orm之路由层
  18. AlexNet——ImageNet Classification with Deep Convolutional Neural Networks
  19. php 文件上传,下载
  20. 将实体转换为Hashtable

热门文章

  1. SpringMVC【二、项目搭建】
  2. VS---《在VS2010中 使用C++创建和使用DLL》(001)
  3. 遍历二叉树 - 基于队列的BFS
  4. 【小知识】证明 $1$ 到 $n$ 的立方和公式
  5. 误删rpm命令的恢复方法
  6. WPF DevExpress ChartControl使用之PieChart
  7. redis cluster 介绍
  8. Create Advanced Web Applications With Object-Oriented Techniques
  9. C#中使用ListView动态添加数据不闪烁(网上方法会出问题)
  10. vue-cli使用less