<develop>:(ShopERP 端)<无> 项目提交。

parent ee1068e4
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Consul" Version="1.7.14.7" />
<PackageReference Include="Ocelot" Version="24.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
</Project>
using Microsoft.AspNetCore.Mvc;
namespace ConsulAndOcelot.ApiGateWay.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
using Consul;
namespace ConsulAndOcelot.ApiGateWay.Infrastructure
{
/// <summary>
/// Consul 服务发现-帮助类
/// </summary>
public class ConsulServiceDiscover
{
private IConfiguration _configuration;
public ConsulServiceDiscover(IConfiguration configuration)
{
_configuration = configuration;
}
#region 根据服务名称获取服务地址
/// <summary>
/// 根据服务名称获取服务地址
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
public string GetDomainByServiceName(string serviceName)
{
string domain = string.Empty;
//Consul 客户端, 用 Using 包起来的程序域会被自动 回收
using (ConsulClient client = new ConsulClient(c =>
{
c.Address = new Uri(_configuration["Consul:consulAddress"]);
c.Datacenter = "apiService";
}))
{
//根据服务名获取健康的服务
var queryResult = client.Health.Service(serviceName, string.Empty, true);
var len = queryResult.Result.Response.Length;
//平均策略-多个负载中随机获取一个
var node = queryResult.Result.Response[new Random().Next(len)];
domain = $"http://{node.Service.Address}:{node.Service.Port}";
}
return domain;
}
#endregion
#region 获取 api 域名
/// <summary>
/// 获取 api 域名
/// </summary>
/// <returns></returns>
public string GetApiDomin()
{
return GetDomainByServiceName(_configuration["Consul:apiServiceName"]);
}
#endregion
}
}

using Ocelot.DependencyInjection;
using Ocelot.Middleware;
namespace ConsulAndOcelot.ApiGateWay
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c => { /* Configuration */ });
//添加 Ocelot 并添加配置文件
builder.Configuration.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true);
//使用 Ocelot 接管代码
builder.Services.AddOcelot(builder.Configuration);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseOcelot();
app.Run();
}
}
}
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:15944",
"sslPort": 44370
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5198",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7171;http://localhost:5198",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
namespace ConsulAndOcelot.ApiGateWay
{
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
{
"Routes": [
{
//转发到下游服务地址--url变量
"DownstreamPathTemplate": "/api/{url}",
//下游http协议
"DownstreamScheme": "http",
//负载方式,
"LoadBalancerOptions": {
"Type": "RoundRobin" // 轮询
},
"DownstreamHostAndPorts": [
{
"Host": "192.168.8.248",
"Port": 5201 //服务端口
}, //可以多个,自行负载均衡
{
"Host": "192.168.8.248",
"Port": 5202 //服务端口
},
{
"Host": "192.168.8.248",
"Port": 5203 //服务端口
}
],
//上游地址
"UpstreamPathTemplate": "/T1/{url}", //网关地址--url变量 //冲突的还可以加权重Priority
"UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ]
}
]
}
......@@ -57,8 +57,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "8.MicroService", "8.MicroSe
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "5.MicroService", "5.MicroService", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsulAndOcelot.ApiGateWay", "ConsulAndOcelot.ApiGateWay\ConsulAndOcelot.ApiGateWay.csproj", "{1262A559-1A8D-444D-B191-86A904770FCB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShopERP.Ocelot.ApiGateWay", "ShopERP.Ocelot.ApiGateWay\ShopERP.Ocelot.ApiGateWay.csproj", "{3C435058-1A99-4548-A217-93359E4869B9}"
EndProject
Global
......@@ -147,10 +145,6 @@ Global
{927E68B4-92D1-4F66-B137-87804055DE93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{927E68B4-92D1-4F66-B137-87804055DE93}.Release|Any CPU.ActiveCfg = Release|Any CPU
{927E68B4-92D1-4F66-B137-87804055DE93}.Release|Any CPU.Build.0 = Release|Any CPU
{1262A559-1A8D-444D-B191-86A904770FCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1262A559-1A8D-444D-B191-86A904770FCB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1262A559-1A8D-444D-B191-86A904770FCB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1262A559-1A8D-444D-B191-86A904770FCB}.Release|Any CPU.Build.0 = Release|Any CPU
{3C435058-1A99-4548-A217-93359E4869B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C435058-1A99-4548-A217-93359E4869B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C435058-1A99-4548-A217-93359E4869B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
......@@ -180,7 +174,6 @@ Global
{0E024CA5-FCE6-48A7-8209-201FF2D9237E} = {82634406-6F43-4E90-B57D-5C7AC3DAFF11}
{7043B7B2-6496-4FCB-A230-1EBC5E11B91C} = {B3F41F4C-3681-48F3-9BE3-809763AC1937}
{927E68B4-92D1-4F66-B137-87804055DE93} = {B3F41F4C-3681-48F3-9BE3-809763AC1937}
{1262A559-1A8D-444D-B191-86A904770FCB} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{3C435058-1A99-4548-A217-93359E4869B9} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment