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改为如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; namespace Service1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<string> Get()
{
return "这是 Service1 ";
} }
}
打开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":
}
]
},
{
"UpstreamPathTemplate": "/Service2/{url}",
"UpstreamHttpMethod": [ "Get", "Post" ], "UseServiceDiscovery": false, "DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port":
}
]
}
],
"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://localhost:7000/Service1/values
请求 http://localhost:7000/Service2/values
OK!打完收工
最新文章
- 初学JAVA的 感想 尹鑫磊
- Highcharts 在低版本 IE 上使用注意事项
- Effective C++ -----条款12: 复制对象时勿忘其每一个成分
- CODESOFT中怎样打印数据库中的特定数据?
- [反汇编练习] 160个CrackMe之024
- C语言结构
- Openstack部署工具
- Easyui tabs学习
- c语言,结构体里面的函数
- MySQL 採用Xtrabackup对数据库进行全库备份
- Java之equals和==详解
- [BZOJ4005][JLOI2015]骗我呢-[dp+容斥]
- HDU 1074 (DP + 状态压缩)
- SAP固定资产业务场景及方案
- java浅析final关键字
- atom编辑器社区插件推荐
- 2018-2019 网络对抗技术 20165226 Exp4:恶意代码分析
- [前端随笔][CSS] 制作一个加载动画 即帖即用
- Afinal的jar包进行代码混淆出错
- CSS3新增的伪类选择器
热门文章
- Python之路(第二篇):Python基本数据类型字符串(一)
- ExportGrid Aspose.Cells.dll
- ubuntu系统ftp连接 以及ssh连接
- CSS实现背景透明而背景上的文字不透明
- SpringBoot中文乱码解决方案
- python中将两个数组压缩成一个数组
- 49.UILable宽度高度自适应
- hadoop mapreduce 写入hbase报错 Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
- swift-基础语法2
- 威伦TK6070iQ触摸屏的使用