当前位置: 首页 > wzjs >正文

石家庄的网站开发公司婚庆策划公司名称

石家庄的网站开发公司,婚庆策划公司名称,网站设置方案,网站源码采集搭建3个项目,分别是OcelotDemo、ServerApi1和ServerApi2这3个项目。访问都是通过OcelotDemo进行轮训转发。 代码案例链接:https://download.csdn.net/download/ly1h1/90715035 1.架构图 2.解决方案结构 3.步骤一,添加Nuget包 4.步骤二&…

搭建3个项目,分别是OcelotDemo、ServerApi1和ServerApi2这3个项目。访问都是通过OcelotDemo进行轮训转发。

代码案例链接:https://download.csdn.net/download/ly1h1/90715035

1.架构图

 2.解决方案结构

3.步骤一,添加Nuget包

4.步骤二,配置ocelot.json

{"Routes": [{"DownstreamPathTemplate": "/api/values/{action}","DownstreamScheme": "http","DownstreamHostAndPorts": [{"Host": "localhost","Port": 5000}, // ServerAPI1{"Host": "localhost","Port": 8016} // ServerAPI2],"UpstreamPathTemplate": "/balanced-api/values/{action}","UpstreamHttpMethod": [ "GET", "POST" ],"LoadBalancerOptions": {"Type": "RoundRobin" // 轮询策略}}],"GlobalConfiguration": {"BaseUrl": "http://localhost:8080"}
}

5.编写OcelotDemo的Program

using Ocelot.DependencyInjection;
using Ocelot.Middleware;var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddOcelot(builder.Configuration);var app = builder.Build();
if (app.Environment.IsDevelopment())
{app.UseDeveloperExceptionPage();
}
await app.UseOcelot();
app.Run("http://localhost:8080");

5.步骤三,编写ServerApi1的接口

using Microsoft.AspNetCore.Mvc;
using ServerAPI1.Models;namespace ServerAPI1.Controllers
{[ApiController][Route("api/[controller]")][Produces("application/json")]public class ValuesController : ControllerBase{/// <summary>/// 获取服务基本信息/// </summary>[HttpGet("info")][ProducesResponseType(200)]public IActionResult GetInfo(){return Ok(new { Service = "ServerAPI1", Port = 5000 });}/// <summary>/// 计算服务 (A+B)/// </summary>/// <param name="model">输入参数</param>[HttpPost("calculate")][ProducesResponseType(200)][ProducesResponseType(400)]public IActionResult Calculate([FromBody] TestModel model){if (!ModelState.IsValid) return BadRequest(ModelState);return Ok(new { Result = 123 + 321, Input = model });}}
}---------------------------
using System.ComponentModel.DataAnnotations;namespace ServerAPI1.Models
{public class TestModel{[Required][StringLength(100)]public string AAA { get; set; }[StringLength(200)]public string BBB { get; set; }}
}

6.步骤四,编写ServerApi1的Program

var builder = WebApplication.CreateBuilder(args);// 添加Swagger
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{c.SwaggerDoc("v1", new() { Title = "ServerAPI1", Version = "v1" });
});builder.Services.AddControllers();
var app = builder.Build();// 配置Swagger
if (app.Environment.IsDevelopment())
{app.UseSwagger();app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ServerAPI1 v1"));
}app.UseRouting();
app.MapControllers();
app.Run("http://localhost:5000");

步骤五,编写Server2的接口

using Microsoft.AspNetCore.Mvc;
using ServerAPI2.Models;namespace ServerAPI2.Controllers
{[ApiController][Route("api/[controller]")][Produces("application/json")]public class ValuesController : ControllerBase{/// <summary>/// 获取服务元数据/// </summary>[HttpGet("info")][ProducesResponseType(200)]public IActionResult GetInfo(){return Ok(new { Service = "ServerAPI2", Port = 8016 });}/// <summary>/// 字符串转换服务/// </summary>/// <param name="model">输入参数</param>[HttpPost("calculate")][ProducesResponseType(200)][ProducesResponseType(400)]public IActionResult Transform([FromBody] TestModel model){if (!ModelState.IsValid) return BadRequest(ModelState);return Ok(new { Result = $"{model.AAA}-{model.BBB}".ToUpper() });}}
}
----------------------------
using System.ComponentModel.DataAnnotations;namespace ServerAPI2.Models
{public class TestModel{[Required][StringLength(100)]public string AAA { get; set; }[StringLength(200)]public string BBB { get; set; }}
}

7.步骤六,编写ServerAPI2的Program

var builder = WebApplication.CreateBuilder(args);// 添加Swagger
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{c.SwaggerDoc("v1", new() { Title = "ServerAPI2", Version = "v1" });
});builder.Services.AddControllers();
var app = builder.Build();// 配置Swagger
if (app.Environment.IsDevelopment())
{app.UseSwagger();app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ServerAPI2 v1"));
}app.UseRouting();
app.MapControllers();
app.Run("http://localhost:8016");

8.测试

采用PostMan直接访问http://localhost:8080/balanced-api/values/info

可实现Server1和Server2的轮询调用。

9.如果需要网关地址和服务是一对一

{"Routes": [{"DownstreamPathTemplate": "/api/values/{action}","DownstreamScheme": "http","DownstreamHostAndPorts": [{"Host": "localhost","Port": 5000}],"UpstreamPathTemplate": "/service1/api/values/{action}","UpstreamHttpMethod": [ "GET", "POST" ]},{"DownstreamPathTemplate": "/api/values/{action}","DownstreamScheme": "http","DownstreamHostAndPorts": [{"Host": "localhost","Port": 8016}],"UpstreamPathTemplate": "/service2/api/values/{action}","UpstreamHttpMethod": [ "GET", "POST" ]}],"GlobalConfiguration": {"BaseUrl": "http://localhost:8080"}
}

http://www.dtcms.com/wzjs/817566.html

相关文章:

  • 门户网站建设文案福建省住房城乡和建设厅网站
  • 北京网站制作与网站设计网页设计实训报告ppt
  • seo网站关键词优化机构整合营销传播论文
  • IP网站登记wordpress 搭建交易所
  • 一个网站的百度反链多好还是少好做免费看电影的网站不违法吗
  • 设计asp网站企业网站标签页是什么
  • 东城企业网站建设企业形象设计教案
  • 顺的网站建设案例uniapp做网站
  • 网站开发图片框响应式网站模版
  • 干事儿网网站开发wordpress 网摘插件
  • 手机膜 东莞网站建设广告商
  • 做网站成品陕西建设局官方网站
  • 株洲网站建设公司排名前程无忧网杭州网站建设类岗位
  • 网站关键词排名没有了网站建设在哪里推广
  • html5网站开发教学石家庄网站开发费用
  • 为中国移动做网站的公司叫什么陕西省住建厅官网
  • 微信推广和网站推广哪个好搭建网站的软件有哪些
  • 做网站做推广有效果吗织梦网站后台模版更换
  • 做电商网站的大连网站建设lccm
  • 成都工业学院文献检索在哪个网站做企业邮箱域名
  • 淘宝网站开发多少金额网站建设背景及意义
  • 专业东莞网站建设报价即在市场中确立自己的位置
  • 网站建设需要具备哪些西安网站搭建的公司
  • 微信小程序 网站开发如何建设网站吸引人
  • 商业网站策划方案网站建设那些公司比较好
  • 编辑网站教程网站用wordpress还是wp
  • 做个网站需要学会什么中信建设四川分公司招聘
  • 农家乐网站模板苏州钻木网络科技有限公司
  • 北京一家专门做会所的网站网页版游戏排行榜女
  • 做网站如何与腾讯合作机械网站建设比较好的