.net core 集成Ocelot

官方地址

Configuration — Ocelot 1.0.0 documentation

github地址

GitHub - ThreeMammals/Ocelot at 07263be530cc145996cf4ece724aeac16a137d7e

ocelot 可以做什么  在官网上都有说明

Ocelot功能的快速列表 有关详细信息,请参阅文档

  • 路由
  • 请求聚合
  • 与 Consul & Eureka 合作的服务发现
  • 服务结构
  • Kubernetes
  • 网络套接字
  • 认证
  • 授权
  • 速率限制
  • 缓存
  • 重试策略/QoS  (Polly)
  • 负载平衡
  • 日志记录/跟踪/关联
  • 标头/方法/查询字符串/声明转换
  • 自定义中间件/委派处理程序
  • 配置/管理 REST API
  • 平台/云不可知论

用到 最多的 是路由 ,认证授权,负载均衡 ,集成 polly ,consul 

nuget 下载相关依赖包 注意 本地.net 框架的版本 

ocelot 18 支持.net 6.0

<PackageReference Include="Ocelot" Version="18.0.0" />
<PackageReference Include="Ocelot.Cache.CacheManager" Version="18.0.0" />
<PackageReference Include="Ocelot.Provider.Consul" Version="18.0.0" />
<PackageReference Include="Ocelot.Provider.Polly" Version="18.0.0" />

service注入 Ocelot相关

1 builder.Services.AddOcelot() //添加Ocelot
2                 .AddPolly()  //添加Ocelot扩展Polly服务降级和重试
3                 .AddConsul() //添加Ocelot扩展Consul服务发现与治理
4                 .AddCacheManager(options => //添加Ocelot扩展缓存
5                 {
6                     options.WithDictionaryHandle();//默认字典形式存储
7                 });

搞个ocelot配置文件吧 

这些配置文件官网说明很详细

 

 1 {
 2   "Routes": [
 3     {
 4       "DownstreamPathTemplate": "/",
 5       "UpstreamPathTemplate": "/",
 6       "UpstreamHttpMethod": [
 7         "Get"
 8       ],
 9       "DownstreamHttpMethod": "",
10       "DownstreamHttpVersion": "",
11       "AddHeadersToRequest": {},
12       "AddClaimsToRequest": {},
13       "RouteClaimsRequirement": {},
14       "AddQueriesToRequest": {},
15       "RequestIdKey": "",
16       "FileCacheOptions": {
17         "TtlSeconds": 0,
18         "Region": ""
19       },
20       "RouteIsCaseSensitive": false,
21       "ServiceName": "",
22       "DownstreamScheme": "http",
23       "DownstreamHostAndPorts": [
24         {
25           "Host": "localhost",
26           "Port": 51876
27         }
28       ],
29       "QoSOptions": {
30         "ExceptionsAllowedBeforeBreaking": 0,
31         "DurationOfBreak": 0,
32         "TimeoutValue": 0
33       },
34       "LoadBalancer": "",
35       "RateLimitOptions": {
36         "ClientWhitelist": [],
37         "EnableRateLimiting": false,
38         "Period": "",
39         "PeriodTimespan": 0,
40         "Limit": 0
41       },
42       "AuthenticationOptions": {
43         "AuthenticationProviderKey": "",
44         "AllowedScopes": []
45       },
46       "HttpHandlerOptions": {
47         "AllowAutoRedirect": true,
48         "UseCookieContainer": true,
49         "UseTracing": true,
50         "MaxConnectionsPerServer": 100
51       },
52       "DangerousAcceptAnyServerCertificateValidator": false
53     }
54 
55   ],
56   "GlobalConfiguration": {
57     "ServiceDiscoveryProvider": {
58       "Host": "localhost",
59       "Port": 9500
60     }
61   }
62 }

 

添加完配置文件 得加载配置文件吧

1 builder.WebHost.ConfigureAppConfiguration(options =>
2 {
3     //添加ocelot配置文件
4     options.AddJsonFile("ocelotconfiguration.json", true, true);
5 });

 

搞搞上游的路由

 

  1 ////*****************************单地址多实例负载均衡+Consul********************************
  2 //{
  3 //  "Routes": [
  4 //    {
  5 //      "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
  6 //      "DownstreamScheme": "http",
  7 //      "UpstreamPathTemplate": "/T/{url}", //网关地址--url变量
  8 //      "UpstreamHttpMethod": [ "Get", "Post" ],
  9 //      "UseServiceDiscovery": true,
 10 //      "ServiceName": "ZhaoxiService", //consul服务名称
 11 //      "LoadBalancerOptions": {
 12 //        "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
 13 //      }
 14 //    }
 15 //  ],
 16 //  "GlobalConfiguration": {
 17 //    "BaseUrl": "http://127.0.0.1:6299", //网关对外地址
 18 //    "ServiceDiscoveryProvider": {
 19 //      "Host": "47.95.2.2",
 20 //      "Port": 8089,
 21 //      "Type": "Consul" //由Consul提供服务发现, 每次请求去consul
 22 //    } //Ocelot没有支持配置多个Consul
 23 
 24 //    //,"ServiceDiscoveryProvider": {
 25 //    //  "Host": "localhost",
 26 //    //  "Port": 8500,
 27 //    //  "Type": "PollConsul", //由Consul提供服务发现,
 28 //    //  "PollingInterval": 1000 //轮询consul,频率毫秒--down掉是不知道的
 29 //    //  //"Token": "footoken"//需要ACL的话
 30 //    //}
 31 //  }
 32 //}
 33 
 34 
 35 
 36 //*****************************Ocelot+Consul********************************
 37 {
 38   "Routes": [
 39     {
 40       "UpstreamPathTemplate": "/api/auth/{url}", //上游请求地址--网关
 41       "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
 42       "UseServiceDiscovery": true,
 43       "ServiceName": "AuthenticationCenter",
 44       "LoadBalancerOptions": {
 45         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
 46       },
 47       "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
 48       "DownstreamScheme": "http",
 49       "DownstreamHeaderTransform": {
 50         "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
 51         "Access-Control-Allow-Methods": "*",
 52         "Access-Control-Allow-Headers": "*"
 53       }
 54     },
 55     {
 56       "UpstreamPathTemplate": "/auth/swagger/v1/swagger.json",
 57       "UpstreamHttpMethod": [ "Get" ],
 58       "UseServiceDiscovery": true,
 59       "ServiceName": "AuthenticationCenter",
 60       "LoadBalancerOptions": {
 61         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
 62       },
 63       "DownstreamPathTemplate": "/swagger/v1/swagger.json",
 64       "DownstreamScheme": "http",
 65       "RateLimitOptions": {
 66         "ClientWhitelist": [ "eleven", "seven" ], //白名单 ClientId 区分大小写
 67         "EnableRateLimiting": true,
 68         "Period": "5m", //1s, 5m, 1h, 1d
 69         "PeriodTimespan": 30, //多少秒之后客户端可以重试
 70         "Limit": 5 //统计时间段内允许的最大请求数量
 71       }
 72     },
 73     {
 74       "UpstreamPathTemplate": "/api/user/{url}", //网关地址--url变量
 75       "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
 76       "UseServiceDiscovery": true,
 77       "ServiceName": "UserMicroservice",
 78       "LoadBalancerOptions": {
 79         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
 80       },
 81       "DownstreamPathTemplate": "/api/user/{url}", //服务地址--url变量
 82       "DownstreamScheme": "http",
 83       "DownstreamHeaderTransform": {
 84         "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
 85         "Access-Control-Allow-Methods": "*",
 86         "Access-Control-Allow-Headers": "*"
 87       }
 88     },
 89     {
 90       "UpstreamPathTemplate": "/user/swagger/v1/swagger.json",
 91       "UpstreamHttpMethod": [ "Get" ],
 92       "UseServiceDiscovery": true,
 93       "ServiceName": "UserMicroservice",
 94       "LoadBalancerOptions": {
 95         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
 96       },
 97       "DownstreamPathTemplate": "/swagger/v1/swagger.json",
 98       "DownstreamScheme": "http"
 99     },
100     {
101       "UpstreamPathTemplate": "/api/search/{url}", //网关地址--url变量
102       "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
103       "UseServiceDiscovery": true,
104       "ServiceName": "GoodsSearchMicroservice",
105       "LoadBalancerOptions": {
106         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
107       },
108       "DownstreamPathTemplate": "/api/search/{url}", //服务地址--url变量
109       "DownstreamScheme": "http",
110       "DownstreamHeaderTransform": {
111         "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
112         "Access-Control-Allow-Methods": "*",
113         "Access-Control-Allow-Headers": "*"
114       }
115     },
116     {
117       "UpstreamPathTemplate": "/search/swagger/v1/swagger.json",
118       "UpstreamHttpMethod": [ "Get" ],
119       "UseServiceDiscovery": true,
120       "ServiceName": "GoodsSearchMicroservice",
121       "LoadBalancerOptions": {
122         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
123       },
124       "DownstreamPathTemplate": "/swagger/v1/swagger.json",
125       "DownstreamScheme": "http"
126     },
127     {
128       "UpstreamPathTemplate": "/api/category/{url}", //网关地址--url变量
129       "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
130       "UseServiceDiscovery": true,
131       "ServiceName": "CategoryMicroservice",
132       "LoadBalancerOptions": {
133         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
134       },
135       "DownstreamPathTemplate": "/api/category/{url}", //服务地址--url变量
136       "DownstreamScheme": "http",
137       "DownstreamHeaderTransform": {
138         "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
139         "Access-Control-Allow-Methods": "*",
140         "Access-Control-Allow-Headers": "*"
141       }
142     },
143     {
144       "UpstreamPathTemplate": "/category/swagger/v1/swagger.json",
145       "UpstreamHttpMethod": [ "Get" ],
146       "UseServiceDiscovery": true,
147       "ServiceName": "CategoryMicroservice",
148       "LoadBalancerOptions": {
149         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
150       },
151       "DownstreamPathTemplate": "/swagger/v1/swagger.json",
152       "DownstreamScheme": "http"
153     },
154     {
155       "UpstreamPathTemplate": "/api/cart/{url}", //网关地址--url变量
156       "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
157       "UseServiceDiscovery": true,
158       "ServiceName": "CartMicroservice",
159       "LoadBalancerOptions": {
160         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
161       },
162       "DownstreamPathTemplate": "/api/cart/{url}", //服务地址--url变量
163       "DownstreamScheme": "http",
164       "DownstreamHeaderTransform": {
165         "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
166         "Access-Control-Allow-Methods": "*",
167         "Access-Control-Allow-Headers": "*"
168       }
169     },
170     {
171       "UpstreamPathTemplate": "/cart/swagger/v1/swagger.json",
172       "UpstreamHttpMethod": [ "Get" ],
173       "UseServiceDiscovery": true,
174       "ServiceName": "CartMicroservice",
175       "LoadBalancerOptions": {
176         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
177       },
178       "DownstreamPathTemplate": "/swagger/v1/swagger.json",
179       "DownstreamScheme": "http"
180     },
181     {
182       "UpstreamPathTemplate": "/api/brand/{url}", //网关地址--url变量
183       "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
184       "UseServiceDiscovery": true,
185       "ServiceName": "BrandMicroservice",
186       "LoadBalancerOptions": {
187         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
188       },
189       "DownstreamPathTemplate": "/api/brand/{url}", //服务地址--url变量
190       "DownstreamScheme": "http",
191       "DownstreamHeaderTransform": {
192         "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
193         "Access-Control-Allow-Methods": "*",
194         "Access-Control-Allow-Headers": "*"
195       }
196     },
197     {
198       "UpstreamPathTemplate": "/brand/swagger/v1/swagger.json",
199       "UpstreamHttpMethod": [ "Get" ],
200       "UseServiceDiscovery": true,
201       "ServiceName": "BrandMicroservice",
202       "LoadBalancerOptions": {
203         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
204       },
205       "DownstreamPathTemplate": "/swagger/v1/swagger.json",
206       "DownstreamScheme": "http"
207     },
208     {
209       "UpstreamPathTemplate": "/api/order/{url}", //网关地址--url变量
210       "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
211       "UseServiceDiscovery": true,
212       "ServiceName": "OrderMicroservice",
213       "LoadBalancerOptions": {
214         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
215       },
216       "DownstreamPathTemplate": "/api/order/{url}", //服务地址--url变量
217       "DownstreamScheme": "http",
218       "DownstreamHeaderTransform": {
219         "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
220         "Access-Control-Allow-Methods": "*",
221         "Access-Control-Allow-Headers": "*"
222       }
223     },
224     {
225       "UpstreamPathTemplate": "/order/swagger/v1/swagger.json",
226       "UpstreamHttpMethod": [ "Get" ],
227       "UseServiceDiscovery": true,
228       "ServiceName": "OrderMicroservice",
229       "LoadBalancerOptions": {
230         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
231       },
232       "DownstreamPathTemplate": "/swagger/v1/swagger.json",
233       "DownstreamScheme": "http"
234     },
235     {
236       "UpstreamPathTemplate": "/api/stock/{url}", //网关地址--url变量
237       "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
238       "UseServiceDiscovery": true,
239       "ServiceName": "StockMicroservice",
240       "LoadBalancerOptions": {
241         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
242       },
243       "DownstreamPathTemplate": "/api/stock/{url}", //服务地址--url变量
244       "DownstreamScheme": "http",
245       "DownstreamHeaderTransform": {
246         "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
247         "Access-Control-Allow-Methods": "*",
248         "Access-Control-Allow-Headers": "*"
249       }
250     },
251     {
252       "UpstreamPathTemplate": "/stock/swagger/v1/swagger.json",
253       "UpstreamHttpMethod": [ "Get" ],
254       "UseServiceDiscovery": true,
255       "ServiceName": "StockMicroservice",
256       "LoadBalancerOptions": {
257         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
258       },
259       "DownstreamPathTemplate": "/swagger/v1/swagger.json",
260       "DownstreamScheme": "http"
261     },
262     {
263       "UpstreamPathTemplate": "/api/pay/{url}", //网关地址--url变量
264       "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
265       "UseServiceDiscovery": true,
266       "ServiceName": "PayMicroservice",
267       "LoadBalancerOptions": {
268         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
269       },
270       "DownstreamPathTemplate": "/api/pay/{url}", //服务地址--url变量
271       "DownstreamScheme": "http",
272       "DownstreamHeaderTransform": {
273         "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
274         "Access-Control-Allow-Methods": "*",
275         "Access-Control-Allow-Headers": "*"
276       }
277     },
278     {
279       "UpstreamPathTemplate": "/pay/swagger/v1/swagger.json",
280       "UpstreamHttpMethod": [ "Get" ],
281       "UseServiceDiscovery": true,
282       "ServiceName": "PayMicroservice",
283       "LoadBalancerOptions": {
284         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
285       },
286       "DownstreamPathTemplate": "/swagger/v1/swagger.json",
287       "DownstreamScheme": "http"
288     },
289     {
290       "UpstreamPathTemplate": "/api/seckill/{url}",
291       "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
292       "UseServiceDiscovery": true,
293       "ServiceName": "SeckillMicroservice",
294       "LoadBalancerOptions": {
295         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
296       },
297       "DownstreamPathTemplate": "/api/seckill/{url}", //服务地址--url变量
298       "DownstreamScheme": "http",
299       "DownstreamHeaderTransform": {
300         "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
301         "Access-Control-Allow-Methods": "*",
302         "Access-Control-Allow-Headers": "*"
303       }
304     },
305     {
306       "UpstreamPathTemplate": "/seckill/swagger/v1/swagger.json",
307       "UpstreamHttpMethod": [ "Get" ],
308       "UseServiceDiscovery": true,
309       "ServiceName": "SeckillMicroservice",
310       "LoadBalancerOptions": {
311         "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
312       },
313       "DownstreamPathTemplate": "/swagger/v1/swagger.json",
314       "DownstreamScheme": "http"
315     }
316   ],
317   "GlobalConfiguration": {
318     "BaseUrl": "http://127.0.0.1:6299", //网关对外地址
319     "ServiceDiscoveryProvider": {
320       "Host": "localhost",
321       "Port": 8500,
322       "Type": "Consul" //由Consul提供服务发现, 每次请求去consul
323     },
324     "RateLimitOptions": {
325       "QuotaExceededMessage": "Too many requests, maybe later? 11", // 当请求过载被截断时返回的消息
326       "HttpStatusCode": 666 // 当请求过载被截断时返回的http status
327       //"ClientIdHeader": "client_id" // 用来识别客户端的请求头,默认是 ClientId
328     }
329 
330     //,"ServiceDiscoveryProvider": {
331     //  "Host": "localhost",
332     //  "Port": 8500,
333     //  "Type": "PollConsul", //由Consul提供服务发现,
334     //  "PollingInterval": 1000 //轮询consul,频率毫秒--down掉是不知道的
335     //  //"Token": "footoken"//需要ACL的话
336     //}
337   }
338 }
339 
340 ////*****************************单地址--无Consul********************************
341 //{
342 //  "Routes": [
343 //    {
344 //      "UpstreamPathTemplate": "/api/auth/{url}", //上游请求地址--网关
345 //      "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
346 //      "DownstreamHostAndPorts": [
347 //        {
348 //          "Host": "localhost",
349 //          "Port": 7200 //网关api 端口
350 //        }
351 //      ],
352 //      "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
353 //      "DownstreamScheme": "http",
354 //      "DownstreamHeaderTransform": {
355 //        "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
356 //        "Access-Control-Allow-Methods": "*",
357 //        "Access-Control-Allow-Headers": "*"
358 //      }
359 //    },
360 //    {
361 //      "UpstreamPathTemplate": "/auth/swagger/v1/swagger.json",
362 //      "UpstreamHttpMethod": [ "Get" ],
363 //      "DownstreamHostAndPorts": [
364 //        {
365 //          "Host": "localhost",
366 //          "Port": 7200 //网关api 端口
367 //        }
368 //      ],
369 //      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
370 //      "DownstreamScheme": "http"
371 //    },
372 //    {
373 //      "UpstreamPathTemplate": "/api/user/{url}", //网关地址--url变量
374 //      "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
375 //      "DownstreamHostAndPorts": [
376 //        {
377 //          "Host": "localhost",
378 //          "Port": 5726 //userapi 端口
379 //        }
380 //      ],
381 //      "DownstreamPathTemplate": "/api/user/{url}", //服务地址--url变量
382 //      "DownstreamScheme": "http",
383 //      "DownstreamHeaderTransform": {
384 //        "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
385 //        "Access-Control-Allow-Methods": "*",
386 //        "Access-Control-Allow-Headers": "*"
387 //      }
388 //    },
389 //    {
390 //      "UpstreamPathTemplate": "/user/swagger/v1/swagger.json",
391 //      "UpstreamHttpMethod": [ "Get" ],
392 //      "DownstreamHostAndPorts": [
393 //        {
394 //          "Host": "localhost",
395 //          "Port": 5726 //网关api 端口
396 //        }
397 //      ],
398 //      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
399 //      "DownstreamScheme": "http"
400 //    },
401 //    {
402 //      "UpstreamPathTemplate": "/api/search/{url}", //网关地址--url变量
403 //      "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
404 //      "DownstreamHostAndPorts": [
405 //        {
406 //          "Host": "localhost",
407 //          "Port": 5730 //userapi 端口
408 //        }
409 //      ],
410 //      "DownstreamPathTemplate": "/api/search/{url}", //服务地址--url变量
411 //      "DownstreamScheme": "http",
412 //      "DownstreamHeaderTransform": {
413 //        "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
414 //        "Access-Control-Allow-Methods": "*",
415 //        "Access-Control-Allow-Headers": "*"
416 //      }
417 //    },
418 //    {
419 //      "UpstreamPathTemplate": "/search/swagger/v1/swagger.json",
420 //      "UpstreamHttpMethod": [ "Get" ],
421 //      "DownstreamHostAndPorts": [
422 //        {
423 //          "Host": "localhost",
424 //          "Port": 5730 //网关api 端口
425 //        }
426 //      ],
427 //      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
428 //      "DownstreamScheme": "http"
429 //    },
430 //    {
431 //      "UpstreamPathTemplate": "/api/category/{url}", //网关地址--url变量
432 //      "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
433 //      "DownstreamHostAndPorts": [
434 //        {
435 //          "Host": "localhost",
436 //          "Port": 5732 //category 端口
437 //        }
438 //      ],
439 //      "DownstreamPathTemplate": "/api/category/{url}", //服务地址--url变量
440 //      "DownstreamScheme": "http",
441 //      "DownstreamHeaderTransform": {
442 //        "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
443 //        "Access-Control-Allow-Methods": "*",
444 //        "Access-Control-Allow-Headers": "*"
445 //      }
446 //    },
447 //    {
448 //      "UpstreamPathTemplate": "/category/swagger/v1/swagger.json",
449 //      "UpstreamHttpMethod": [ "Get" ],
450 //      "DownstreamHostAndPorts": [
451 //        {
452 //          "Host": "localhost",
453 //          "Port": 5732 //category 端口
454 //        }
455 //      ],
456 //      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
457 //      "DownstreamScheme": "http"
458 //    },
459 //    {
460 //      "UpstreamPathTemplate": "/api/cart/{url}", //网关地址--url变量
461 //      "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
462 //      "DownstreamHostAndPorts": [
463 //        {
464 //          "Host": "localhost",
465 //          "Port": 5733 //cart 端口
466 //        }
467 //      ],
468 //      "DownstreamPathTemplate": "/api/cart/{url}", //服务地址--url变量
469 //      "DownstreamScheme": "http",
470 //      "DownstreamHeaderTransform": {
471 //        "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
472 //        "Access-Control-Allow-Methods": "*",
473 //        "Access-Control-Allow-Headers": "*"
474 //      }
475 //    },
476 //    {
477 //      "UpstreamPathTemplate": "/cart/swagger/v1/swagger.json",
478 //      "UpstreamHttpMethod": [ "Get" ],
479 //      "DownstreamHostAndPorts": [
480 //        {
481 //          "Host": "localhost",
482 //          "Port": 5733 //category 端口
483 //        }
484 //      ],
485 //      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
486 //      "DownstreamScheme": "http"
487 //    },
488 //    {
489 //      "UpstreamPathTemplate": "/api/brand/{url}", //网关地址--url变量
490 //      "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
491 //      "DownstreamHostAndPorts": [
492 //        {
493 //          "Host": "localhost",
494 //          "Port": 5734 //brand 端口
495 //        }
496 //      ],
497 //      "DownstreamPathTemplate": "/api/brand/{url}", //服务地址--url变量
498 //      "DownstreamScheme": "http",
499 //      "DownstreamHeaderTransform": {
500 //        "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
501 //        "Access-Control-Allow-Methods": "*",
502 //        "Access-Control-Allow-Headers": "*"
503 //      }
504 //    },
505 //    {
506 //      "UpstreamPathTemplate": "/brand/swagger/v1/swagger.json",
507 //      "UpstreamHttpMethod": [ "Get" ],
508 //      "DownstreamHostAndPorts": [
509 //        {
510 //          "Host": "localhost",
511 //          "Port": 5734 //brand 端口
512 //        }
513 //      ],
514 //      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
515 //      "DownstreamScheme": "http"
516 //    },
517 //    {
518 //      "UpstreamPathTemplate": "/api/order/{url}", //网关地址--url变量
519 //      "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
520 //      "DownstreamHostAndPorts": [
521 //        {
522 //          "Host": "localhost",
523 //          "Port": 5729 //order 端口---80为了穿透-为了微信
524 //        }
525 //      ],
526 //      "DownstreamPathTemplate": "/api/order/{url}", //服务地址--url变量
527 //      "DownstreamScheme": "http",
528 //      "DownstreamHeaderTransform": {
529 //        "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
530 //        "Access-Control-Allow-Methods": "*",
531 //        "Access-Control-Allow-Headers": "*"
532 //      }
533 //    },
534 //    {
535 //      "UpstreamPathTemplate": "/order/swagger/v1/swagger.json",
536 //      "UpstreamHttpMethod": [ "Get" ],
537 //      "DownstreamHostAndPorts": [
538 //        {
539 //          "Host": "localhost",
540 //          "Port": 5729 //order 端口
541 //        }
542 //      ],
543 //      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
544 //      "DownstreamScheme": "http"
545 //    },
546 //    {
547 //      "UpstreamPathTemplate": "/api/stock/{url}", //网关地址--url变量
548 //      "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
549 //      "DownstreamHostAndPorts": [
550 //        {
551 //          "Host": "localhost",
552 //          "Port": 5735 //stock 端口
553 //        }
554 //      ],
555 //      "DownstreamPathTemplate": "/api/stock/{url}", //服务地址--url变量
556 //      "DownstreamScheme": "http",
557 //      "DownstreamHeaderTransform": {
558 //        "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
559 //        "Access-Control-Allow-Methods": "*",
560 //        "Access-Control-Allow-Headers": "*"
561 //      }
562 //    },
563 //    {
564 //      "UpstreamPathTemplate": "/stock/swagger/v1/swagger.json",
565 //      "UpstreamHttpMethod": [ "Get" ],
566 //      "DownstreamHostAndPorts": [
567 //        {
568 //          "Host": "localhost",
569 //          "Port": 5735 //stock 端口
570 //        }
571 //      ],
572 //      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
573 //      "DownstreamScheme": "http"
574 //    },
575 //    {
576 //      "UpstreamPathTemplate": "/api/pay/{url}", //网关地址--url变量
577 //      "UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
578 //      "DownstreamHostAndPorts": [
579 //        {
580 //          "Host": "192.168.3.103",
581 //          "Port": 80 //pay 端口---80为了穿透-为了微信---5736
582 //        }
583 //      ],
584 //      "DownstreamPathTemplate": "/api/pay/{url}", //服务地址--url变量
585 //      "DownstreamScheme": "http",
586 //      "DownstreamHeaderTransform": {
587 //        "Access-Control-Allow-Origin": "http://localhost:8070", //不存在就添加
588 //        "Access-Control-Allow-Methods": "*",
589 //        "Access-Control-Allow-Headers": "*"
590 //      }
591 //    },
592 //    {
593 //      "UpstreamPathTemplate": "/pay/swagger/v1/swagger.json",
594 //      "UpstreamHttpMethod": [ "Get" ],
595 //      "DownstreamHostAndPorts": [
596 //        {
597 //          "Host": "192.168.3.103",
598 //          "Port": 80 //pay 端口
599 //        }
600 //      ],
601 //      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
602 //      "DownstreamScheme": "http"
603 //    }
604 //  ]
605 //}
606 
607 ////*****************************单地址全匹配********************************
608 //{
609 //  "Routes": [
610 //    {
611 //      "DownstreamPathTemplate": "/{url}", //服务地址--url变量
612 //      "DownstreamScheme": "http",
613 //      "DownstreamHostAndPorts": [
614 //        {
615 //          "Host": "localhost",
616 //          "Port": 5726 //服务端口
617 //        }
618 //      ],
619 //      "UpstreamPathTemplate": "/{url}", //网关地址--url变量   //冲突的还可以加权重Priority
620 //      "UpstreamHttpMethod": [ "Get", "Post" ]
621 //    }
622 //  ]
623 //}
624 
625 ////*****************************多地址多实例********************************
626 //{
627 //  "Routes": [
628 //    {
629 //      "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
630 //      "DownstreamScheme": "http",
631 //      "DownstreamHostAndPorts": [
632 //        {
633 //          "Host": "localhost",
634 //          "Port": 5726 //服务端口
635 //        }
636 //      ],
637 //      "UpstreamPathTemplate": "/T5726/{url}", //网关地址--url变量
638 //      "UpstreamHttpMethod": [ "Get", "Post" ]
639 //    },
640 //    {
641 //      "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
642 //      "DownstreamScheme": "http",
643 //      "DownstreamHostAndPorts": [
644 //        {
645 //          "Host": "localhost",
646 //          "Port": 5727 //服务端口
647 //        }
648 //      ],
649 //      "UpstreamPathTemplate": "/T5727/{url}", //网关地址--url变量
650 //      "UpstreamHttpMethod": [ "Get", "Post" ]
651 //    },
652 //    {
653 //      "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
654 //      "DownstreamScheme": "http",
655 //      "DownstreamHostAndPorts": [
656 //        {
657 //          "Host": "localhost",
658 //          "Port": 5728 //服务端口
659 //        }
660 //      ],
661 //      "UpstreamPathTemplate": "/T5728/{url}", //网关地址--url变量
662 //      "UpstreamHttpMethod": [ "Get", "Post" ]
663 //    }
664 //  ]
665 //}
666 
667 //////MVC的路由规则是近水楼台先得月--
668 ////*****************************路由冲突+带权匹配********************************
669 //{
670 //  "Routes": [
671 //    {
672 //      "DownstreamPathTemplate": "/{url}", //服务地址--url变量
673 //      "DownstreamScheme": "http",
674 //      "DownstreamHostAndPorts": [
675 //        {
676 //          "Host": "localhost",
677 //          "Port": 5726 //服务端口
678 //        }
679 //      ],
680 //      "UpstreamPathTemplate": "/{url}", //网关地址--url变量   //冲突的还可以加权重Priority
681 //      "UpstreamHttpMethod": [ "Get", "Post" ],
682 //      "Priority": 0 //默认是0 加个1
683 //    },
684 //    {
685 //      "DownstreamPathTemplate": "/api/users/get?id={id}", //服务地址--url变量
686 //      "DownstreamScheme": "http",
687 //      "DownstreamHostAndPorts": [
688 //        {
689 //          "Host": "localhost",
690 //          "Port": 5727 //服务端口
691 //        }
692 //      ],
693 //      "UpstreamPathTemplate": "/api/users/get/{id}", //网关地址--url变量   //冲突的还可以加权重Priority
694 //      "UpstreamHttpMethod": [ "Get", "Post" ],
695 //      "Priority": 1 //默认是0 加个1
696 //    },
697 //    {
698 //      "DownstreamPathTemplate": "/api/users/{url}?id={id}", //服务地址--url变量
699 //      "DownstreamScheme": "http",
700 //      "DownstreamHostAndPorts": [
701 //        {
702 //          "Host": "localhost",
703 //          "Port": 5728 //服务端口
704 //        }
705 //      ],
706 //      "UpstreamPathTemplate": "/api/users/{url}/{id}", //网关地址--url变量   //冲突的还可以加权重Priority
707 //      "UpstreamHttpMethod": [ "Get", "Post" ],
708 //      "Priority": 2 //默认是0 加个1
709 //    }
710 //  ]
711 //}
712 
713 ////*****************************单地址多实例负载均衡********************************
714 //{
715 //  "Routes": [
716 //    {
717 //      "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
718 //      "DownstreamScheme": "http",
719 //      "DownstreamHostAndPorts": [
720 //        {
721 //          "Host": "47.95.2.2",
722 //          "Port": 5726
723 //        }, //Ocelot负载均衡
724 //        {
725 //          "Host": "47.95.2.2",
726 //          "Port": 5727
727 //        },
728 //        {
729 //          "Host": "47.95.2.2",
730 //          "Port": 5728
731 //        }
732 //      ],
733 //      "UpstreamPathTemplate": "/T/{url}", //网关地址--url变量   //冲突的还可以加权重Priority
734 //      "UpstreamHttpMethod": [ "Get", "Post" ],
735 //      "LoadBalancerOptions": {
736 //        "Type": "RoundRobin" //轮询 //"LeastConnection" //最少连接数的服务器  "NoLoadBalance" //不负载均衡  //"CookieStickySessions" //会话粘滞  //
737 //      }
738 //      //"LoadBalancerOptions": {
739 //      //  "Type": "CookieStickySessions",
740 //      //  "Key": "ASP.NET_SessionId",
741 //      //  "Expiry": 1800000
742 //      //}
743 //    }
744 //  ]
745 //}
746 
747 ////*****************************单地址多实例负载均衡+Consul********************************
748 //{
749 //  "Routes": [
750 //    {
751 //      "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
752 //      "DownstreamScheme": "http",
753 //      "UpstreamPathTemplate": "/T/{url}", //网关地址--url变量
754 //      "UpstreamHttpMethod": [ "Get", "Post" ],
755 //      "UseServiceDiscovery": true,
756 //      "ServiceName": "ZhaoxiService", //consul服务名称
757 //      "LoadBalancerOptions": {
758 //        "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
759 //      }
760 //    }
761 //  ],
762 //  "GlobalConfiguration": {
763 //    "BaseUrl": "http://127.0.0.1:6299", //网关对外地址
764 //    "ServiceDiscoveryProvider": {
765 //      "Host": "47.95.2.2",
766 //      "Port": 8089,
767 //      "Type": "Consul" //由Consul提供服务发现, 每次请求去consul
768 //    } //Ocelot没有支持配置多个Consul
769 
770 //    //,"ServiceDiscoveryProvider": {
771 //    //  "Host": "localhost",
772 //    //  "Port": 8500,
773 //    //  "Type": "PollConsul", //由Consul提供服务发现,
774 //    //  "PollingInterval": 1000 //轮询consul,频率毫秒--down掉是不知道的
775 //    //  //"Token": "footoken"//需要ACL的话
776 //    //}
777 //  }
778 //}
779 
780 ////*****************************Consul+缓存Cache********************************
781 //{
782 //  "Routes": [
783 //    {
784 //      "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
785 //      "DownstreamScheme": "http",
786 //      "UpstreamPathTemplate": "/T/{url}", //网关地址--url变量
787 //      "UpstreamHttpMethod": [ "Get", "Post" ],
788 //      "UseServiceDiscovery": true,
789 //      "ServiceName": "ZhaoxiService", //consul服务名称
790 //      "LoadBalancerOptions": {
791 //        "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
792 //      },
793 //      "FileCacheOptions": {
794 //        "TtlSeconds": 15, //Second
795 //        "Region": "UserCache" //可以调用Api缓存清理
796 //      }
797 //    }
798 //  ],
799 //  "GlobalConfiguration": {
800 //    "BaseUrl": "http://127.0.0.1:6299", //网关对外地址
801 //    "ServiceDiscoveryProvider": {
802 //      "Host": "47.95.2.2",
803 //      "Port": 8089,
804 //      "Type": "Consul" //由Consul提供服务发现, 每次请求去consul
805 //    }
806 //    //"ServiceDiscoveryProvider": {
807 //    //  "Host": "localhost",
808 //    //  "Port": 8500,
809 //    //  "Type": "PollConsul", //由Consul提供服务发现,
810 //    //  "PollingInterval": 1000 //轮询consul,频率毫秒--down掉是不知道的
811 //    //  //"Token": "footoken"//需要ACL的话
812 //    //}
813 //  }
814 //}
815 
816 ////*****************************超时+限流+熔断+降级+Consul+Polly********************************
817 //{
818 //  "Routes": [
819 //    {
820 //      "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
821 //      "DownstreamScheme": "http",
822 //      "UpstreamPathTemplate": "/T/{url}", //网关地址--url变量
823 //      "UpstreamHttpMethod": [ "Get", "Post" ],
824 //      "UseServiceDiscovery": true,
825 //      "ServiceName": "ZhaoxiService", //consul服务名称
826 //      "LoadBalancerOptions": {
827 //        "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均衡
828 //      },
829 //      "RateLimitOptions": {
830 //        "ClientWhitelist": [ "eleven", "seven" ], //白名单 ClientId 区分大小写
831 //        "EnableRateLimiting": true,
832 //        "Period": "5m", //1s, 5m, 1h, 1d
833 //        "PeriodTimespan": 30, //多少秒之后客户端可以重试
834 //        "Limit": 5 //统计时间段内允许的最大请求数量
835 //      },
836 //      "AuthenticationOptions": {
837 //        "AuthenticationProviderKey": "UserGatewayKey",
838 //        "AllowedScopes": []
839 //      },
840 //      "QoSOptions": {
841 //        "ExceptionsAllowedBeforeBreaking": 3, //允许多少个异常请求
842 //        "DurationOfBreak": 10000, // 熔断的时间,单位为ms
843 //        "TimeoutValue": 2000 //单位ms 如果下游请求的处理时间超过多少则自如将请求设置为超时 默认90秒
844 //      }
845 //      //"FileCacheOptions": {
846 //      //  "TtlSeconds": 15,
847 //      //  "Region": "UserCache" //可以调用Api清理
848 //      //}
849 //    }
850 //  ],
851 //  "GlobalConfiguration": {
852 //    "BaseUrl": "http://127.0.0.1:6299", //网关对外地址
853 //    "ServiceDiscoveryProvider": {
854 //      "Host": "47.95.2.2",
855 //      "Port": 8089,
856 //      "Type": "Consul" //由Consul提供服务发现
857 //    },
858 //    "RateLimitOptions": {
859 //      "QuotaExceededMessage": "Too many requests, maybe later? 11", // 当请求过载被截断时返回的消息
860 //      "HttpStatusCode": 666, // 当请求过载被截断时返回的http status
861 //      //"ClientIdHeader": "client_id" // 用来识别客户端的请求头,默认是 ClientId
862 //    }
863 //  }
864 //}
865 
866 ////*****************************请求聚合Aggregator********************************
867 //{
868 //  "Routes": [
869 //    {
870 //      "DownstreamPathTemplate": "/api/users/all",
871 //      "DownstreamScheme": "http",
872 //      "DownstreamHostAndPorts": [
873 //        {
874 //          "Host": "localhost",
875 //          "Port": 5726 //服务端口
876 //        } //可以多个,自行负载均衡
877 //      ],
878 //      "UpstreamPathTemplate": "/T5726/users/all",
879 //      "UpstreamHttpMethod": [ "Get", "Post" ],
880 //      "key": "T5726"
881 //    },
882 //    {
883 //      "DownstreamPathTemplate": "/api/users/all",
884 //      "DownstreamScheme": "http",
885 //      "DownstreamHostAndPorts": [
886 //        {
887 //          "Host": "localhost",
888 //          "Port": 5727 //服务端口
889 //        }
890 //      ],
891 //      "UpstreamPathTemplate": "/T5727/users/all",
892 //      "UpstreamHttpMethod": [ "Get", "Post" ],
893 //      "key": "T5727"
894 //    },
895 //    {
896 //      "DownstreamPathTemplate": "/api/users/all",
897 //      "DownstreamScheme": "http",
898 //      "DownstreamHostAndPorts": [
899 //        {
900 //          "Host": "localhost",
901 //          "Port": 5728 //服务端口
902 //        }
903 //      ],
904 //      "UpstreamPathTemplate": "/T5728/users/all",
905 //      "UpstreamHttpMethod": [ "Get", "Post" ],
906 //      "key": "T5728"
907 //    }
908 //  ],
909 //  "Aggregates": [
910 //    {
911 //      "RouteKeys": [
912 //        "T5726",
913 //        "T5727",
914 //        "T5728"
915 //      ],
916 //      "UpstreamPathTemplate": "/UserAggregator", //如果某个404 是不影响返回,当成null
917 //      "Aggregator": "CustomUserAggregator" //自定义聚合器
918 //    }
919 //  ]
920 //}

 

posted on 2023-05-13 11:46  是水饺不是水饺  阅读(77)  评论(0)    收藏  举报

导航