consul ocelot
2024-09-02 08:02:53
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,然后会访问服务发现中的路由
最新文章
- python fork 用法
- [Node.js] DSL in action
- JavaScript制作时钟特效
- Lvs原理
- ftp如何预览图片 解决方案
- Permutation Sequence [LeetCode]
- Redis配置文件解读
- HDFS 搭建记录
- Js 旋转平滑特效
- jsp验证表单后再提交
- POJ 1515 Street Directions (边双连通)
- 前端性能优化成神之路--SSR(服务端渲染)
- localstorage实现网页状态记录比如放音乐功能的实例
- Python pickle模块
- JNUOJ 1032 - 食物处理器
- STM32 CAN总线标识符过滤器难点解析
- orm之路由层
- AlexNet——ImageNet Classification with Deep Convolutional Neural Networks
- php 文件上传,下载
- 将实体转换为Hashtable
热门文章
- SpringMVC【二、项目搭建】
- VS---《在VS2010中 使用C++创建和使用DLL》(001)
- 遍历二叉树 - 基于队列的BFS
- 【小知识】证明 $1$ 到 $n$ 的立方和公式
- 误删rpm命令的恢复方法
- WPF DevExpress ChartControl使用之PieChart
- redis cluster 介绍
- Create Advanced Web Applications With Object-Oriented Techniques
- C#中使用ListView动态添加数据不闪烁(网上方法会出问题)
- vue-cli使用less