傲渊山岳 发表于 2022-6-20 08:27:22

【微服务专题之】.Net6下集成微服务网关-Ocelot

 
 
微信公众号:趣编程ACE
关注可了解更多的.NET日常实战开发技巧,如需源码 请公众号后台留言 源码;
[如果觉得本公众号对您有帮助,欢迎关注]
 
.Net6下集成微服务网关-Ocelot

https://img-blog.csdnimg.cn/img_convert/39b594417aa76bb8ca7e090e65cf8cb0.png​
https://img2022.cnblogs.com/blog/2562673/202206/2562673-20220615095155135-2095322688.gif视频讲解
网关常见功能

[*]1:路由 routing
[*]2: 请求聚合
[*]3:身份验证和授权
[*]4:速率限制
[*]5:缓存
[*]6:负载均衡
路由 routing基本使用

安装Nugget包
1// 18.0.0 最新版 支持.net 6 
2Install-Package Ocelot  https://img2022.cnblogs.com/blog/2562673/202206/2562673-20220615095155135-2095322688.gif建立一个.Net6 Web Api 的项目
https://img-blog.csdnimg.cn/img_convert/2e971315bc239f58a3398de6d93e4a06.png​
https://img2022.cnblogs.com/blog/2562673/202206/2562673-20220615095155135-2095322688.gif服务端代码编写
1// 添加日志服务 ,这样在访问网关接口的时候可以在控制台打印输出相应的信息
2builder.Host.ConfigureLogging(log=>{
3    log.ClearProviders();
4    log.AddConsole();
5});
6// 注册Ocelot 服务
7builder.Services.AddOcelot();
8
9// ----------
10// 注册好Ocelot 服务后 启用其中间件
11app.UseOcelot().Wait();https://img2022.cnblogs.com/blog/2562673/202206/2562673-20220615095155135-2095322688.gif设置Ocelot 配置json文件
在项目根目录下创建一个ocelot.json 文件
PS:配置文件详细参数介绍参考上文视频或者官网地址
1{
2    "Routes": [
3        {
4        "DownstreamPathTemplate": "/todos/{id}",
5        "DownstreamScheme": "https",
6        "DownstreamHostAndPorts": [
7            {
8                "Host": "jsonplaceholder.typicode.com",
9                "Port": 443
10            }
11        ],
12        "UpstreamPathTemplate": "/todos/{id}",
13        "UpstreamHttpMethod": [ "Get" ]
14        }
15    ],
16    "GlobalConfiguration": {
17        "BaseUrl": "https://localhost:5000"
18    }
19}https://img2022.cnblogs.com/blog/2562673/202206/2562673-20220615095155135-2095322688.gif最终效果-详情见视频

https://img-blog.csdnimg.cn/img_convert/ca28c0035a43f2e57eb095a291409c7a.gif​
https://img2022.cnblogs.com/blog/2562673/202206/2562673-20220615095155135-2095322688.gif下篇分享网关中授权鉴权~

 
https://img-blog.csdnimg.cn/8b6e0acec70049a79d21a54f02273faf.jpeg​
https://img2022.cnblogs.com/blog/2562673/202206/2562673-20220615095155135-2095322688.gif 


来源:https://www.cnblogs.com/qubiancheng666/archive/2022/06/15/16377301.html
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 【微服务专题之】.Net6下集成微服务网关-Ocelot