当前位置: 移动技术网 > IT编程>开发语言>.net > Ocelot入门实践

Ocelot入门实践

2019年03月17日  | 移动技术网IT编程  | 我要评论

相亲才会赢贝塔,瓦斯的主要成分是什么,伴奏网站

博主是第一次写技术文档,一是对这两年工作以来的一些技术和经验进行整理,二也是希望能和大家多多分享交流,如有写的不对的地方望大家多多指正。进入正题


 

ocelot 概念就不说了,大家自行百度,今天做一个ocelot实例

 

1.vs新建空白解决方案

 

2.右键解决方案新建项目service1,service2选择api项目模板

 

右键解决方案添加项目gateway选择空项目模板

 

建立完成后解决方案如下

3.右键解决方案=>设置启动项目

 

打开service1 launchsettings.json文件,修改"applicationurl": "http://localhost:7001" ,"launchbrowser": false,

打开service2 launchsettings.json文件,修改"applicationurl": "http://localhost:7002" ,"launchbrowser": false,

打开gateway launchsettings.json文件,修改"applicationurl": "http://www.lhsxpumps.com/_localhost:7000" ,"launchbrowser": false,

 

 4.打开service1 中 valuescontroller改为如下:

 1 using system;
 2 using system.collections.generic;
 3 using system.linq;
 4 using system.threading.tasks;
 5 using microsoft.aspnetcore.mvc;
 6 
 7 namespace service1.controllers
 8 {
 9     [route("api/[controller]")]
10     [apicontroller]
11     public class valuescontroller : controllerbase
12     {
13         // get api/values
14         [httpget]
15         public actionresult<string> get()
16         {
17             return "这是 service1 ";
18         }
19 
20     }
21 }

 

 

 打开service2 中 valuescontroller改为如下:

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore.mvc;

namespace service2.controllers
{
    [route("api/[controller]")]
    [apicontroller]
    public class valuescontroller : controllerbase
    {
        // get api/values
        [httpget]
        public actionresult<string> get()
        {
            return "这是 service2 ";
        }
    }
}

 

5. vs =>调试=>开始执行

打开postman api测试工具 请求 http://www.lhsxpumps.com/_localhost:7001/api/values

 

 

请求 http://www.lhsxpumps.com/_localhost:7002/api/values

 

 

service准备完毕,接下来接入ocelot

6.gateway项目安装nuget包 install-package ocelot

 

gateway项目下添加ocelot.json文件,右键属性,如果较新则复制,并进行如下配置 

{
  "reroutes": [
    {
      //upstream表示上游请求,即客户端请求到api gateway的请求
      "upstreampathtemplate": "/service1/{url}", //请求路径模板
      "upstreamhttpmethod": [ "get", "post" ], //请求方法数组

      "useservicediscovery": false, //启用服务发现

      //downstream表示下游请求,即api gateway转发的目标服务地址
      "downstreampathtemplate": "/api/{url}", //下游请求地址模板
      "downstreamscheme": "http", //请求协议,目前应该是支持http和https
      "downstreamhostandports": [ //请求服务地址
        {
          "host": "localhost",
          "port": 7001
        }
      ]
    },
    {
      "upstreampathtemplate": "/service2/{url}",
      "upstreamhttpmethod": [ "get", "post" ],

      "useservicediscovery": false,

      "downstreampathtemplate": "/api/{url}",
      "downstreamscheme": "http",
      "downstreamhostandports": [
        {
          "host": "localhost",
          "port": 7002
        }
      ]
    }
  ],
  "globalconfiguration": {
    //"servicediscoveryprovider": {
    //  "host": "127.0.0.1",
    //  "port": 8500,
    //  "type": "pollconsul"
    //}
  }
}

 打开program.cs,修改如下:

public class program
    {
        public static void main(string[] args)
        {
            createwebhostbuilder(args).build().run();
        }

        public static iwebhostbuilder createwebhostbuilder(string[] args) =>
            webhost.createdefaultbuilder(args)
            .configureappconfiguration((hostingcontext, builder) =>
            {
                builder.setbasepath(hostingcontext.hostingenvironment.contentrootpath)
                .addjsonfile("ocelot.json", false, true);
            })
            .usestartup<startup>();
    }

打开startup.cs文件,进行如下配置:

public void configureservices(iservicecollection services)
        {
            services.addocelot();//添加ocelot服务
        }
public void configure(iapplicationbuilder app, ihostingenvironment env)
        {
            if (env.isdevelopment())
            {
                app.usedeveloperexceptionpage();
            }

            app.useocelot().wait();//使用ocelot服务
        }

ocelot配置完毕,vs=>调试=>开始执行

打开postman工具进行测试

 

请求 http://www.lhsxpumps.com/_localhost:7000/service1/values

 

请求 http://www.lhsxpumps.com/_localhost:7000/service2/values

 

ok!打完收工

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网