设为首页 加入收藏

TOP

Ocelot入门实践(一)
2019-09-17 18:34:37 】 浏览:33
Tags:Ocelot 入门 实践

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


 

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://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://localhost:7001/api/values

 

 

请求 http://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,ws
      "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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C#调用C++库知识点 下一篇C# 中数组、ArrayList、List<T..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目