@@ -0,0 +1,57 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Security.Cryptography.X509Certificates; | |||||
using System.Threading.Tasks; | |||||
using JT1078.Gateway.Coordinator.Dtos; | |||||
using Microsoft.AspNetCore.Cors; | |||||
using Microsoft.AspNetCore.Mvc; | |||||
using Microsoft.Extensions.Logging; | |||||
namespace JT1078.Gateway.Coordinator.Controller | |||||
{ | |||||
/// <summary> | |||||
/// 协调器中心 | |||||
/// </summary> | |||||
[Route("JT1078WebApi/Coordinator")] | |||||
[ApiController] | |||||
[EnableCors("any")] | |||||
public class CoordinatorController:ControllerBase | |||||
{ | |||||
private ILogger logger; | |||||
public CoordinatorController(ILoggerFactory loggerFactory) | |||||
{ | |||||
logger = loggerFactory.CreateLogger<CoordinatorController>(); | |||||
} | |||||
/// <summary> | |||||
/// 集群服务器重置 | |||||
/// </summary> | |||||
[Route("Reset")] | |||||
[HttpPost] | |||||
public void Reset() | |||||
{ | |||||
} | |||||
/// <summary> | |||||
/// 心跳检测 | |||||
/// </summary> | |||||
[Route("Heartbeat")] | |||||
[HttpPost] | |||||
public void Heartbeat([FromBody] HeartbeatRequest request) | |||||
{ | |||||
} | |||||
/// <summary> | |||||
/// 关闭通道 | |||||
/// </summary> | |||||
[Route("ChannelClose")] | |||||
[HttpPost] | |||||
public void ChannelClose([FromBody] ChannelCloseRequest request) | |||||
{ | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,31 @@ | |||||
using JT1078.Gateway.Coordinator.Dtos; | |||||
using Microsoft.AspNetCore.Cors; | |||||
using Microsoft.AspNetCore.Mvc; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace JT1078.Gateway.Coordinator.Controller | |||||
{ | |||||
/// <summary> | |||||
/// 用户功能 | |||||
/// </summary> | |||||
[Route("JT1078WebApi/User")] | |||||
[ApiController] | |||||
[EnableCors("any")] | |||||
public class UserController : ControllerBase | |||||
{ | |||||
/// <summary> | |||||
/// 登录 | |||||
/// </summary> | |||||
[Route("Login")] | |||||
[HttpPost] | |||||
public void Login([FromBody] LoginRequest request) | |||||
{ | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,19 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace JT1078.Gateway.Coordinator.Dtos | |||||
{ | |||||
public class ChannelCloseRequest | |||||
{ | |||||
/// <summary> | |||||
/// 设备sim卡号 | |||||
/// </summary> | |||||
public string TerminalPhoneNo { get; set; } | |||||
/// <summary> | |||||
/// 通道号 | |||||
/// </summary> | |||||
public int ChannelNo { get; set; } | |||||
} | |||||
} |
@@ -0,0 +1,18 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace JT1078.Gateway.Coordinator.Dtos | |||||
{ | |||||
public class HeartbeatRequest | |||||
{ | |||||
public int HttpPort { get; set; } | |||||
public int TcpPort { get; set; } | |||||
public int UdpPort { get; set; } | |||||
public int TcpSessionCount { get; set; } | |||||
public int UdpSessionCount { get; set; } | |||||
public int HttpSessionCount { get; set; } | |||||
public int WebSocketSessionCount { get; set; } | |||||
} | |||||
} |
@@ -0,0 +1,13 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace JT1078.Gateway.Coordinator.Dtos | |||||
{ | |||||
public class LoginRequest | |||||
{ | |||||
public string UserName { get; set; } | |||||
public string Password { get; set; } | |||||
} | |||||
} |
@@ -20,7 +20,18 @@ namespace JT1078.Gateway.Coordinator | |||||
.UseEnvironment(args[0]) | .UseEnvironment(args[0]) | ||||
.ConfigureWebHostDefaults(webBuilder => | .ConfigureWebHostDefaults(webBuilder => | ||||
{ | { | ||||
webBuilder.Configure(app => | |||||
webBuilder.ConfigureServices(services => | |||||
{ | |||||
services.AddCors(options => | |||||
options.AddPolicy("any", builder => | |||||
builder.AllowAnyOrigin() | |||||
.AllowAnyMethod() | |||||
.AllowAnyHeader() | |||||
.SetIsOriginAllowed(o=>true))); | |||||
services.AddMemoryCache(); | |||||
services.AddControllers(); | |||||
services.AddMvc(); | |||||
}).Configure(app => | |||||
{ | { | ||||
app.UseRouting(); | app.UseRouting(); | ||||
app.UseCors("any"); | app.UseCors("any"); | ||||
@@ -32,19 +43,6 @@ namespace JT1078.Gateway.Coordinator | |||||
{ | { | ||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost | ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost | ||||
}); | }); | ||||
}) | |||||
.ConfigureServices(services => | |||||
{ | |||||
services.AddCors(options => | |||||
options.AddPolicy("any", builder => | |||||
builder.AllowAnyOrigin() | |||||
.AllowAnyMethod() | |||||
.AllowAnyHeader() | |||||
.AllowAnyOrigin() | |||||
.SetIsOriginAllowed(o=>true))); | |||||
services.AddMemoryCache(); | |||||
services.AddControllers(); | |||||
services.AddMvc(); | |||||
}); | }); | ||||
}) | }) | ||||
.ConfigureAppConfiguration((hostingContext, config) => | .ConfigureAppConfiguration((hostingContext, config) => | ||||
@@ -57,6 +55,7 @@ namespace JT1078.Gateway.Coordinator | |||||
{ | { | ||||
services.AddSingleton<ILoggerFactory, LoggerFactory>(); | services.AddSingleton<ILoggerFactory, LoggerFactory>(); | ||||
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); | services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); | ||||
}) | }) | ||||
.Build() | .Build() | ||||
.Run(); | .Run(); | ||||
@@ -43,7 +43,15 @@ namespace JT1078.Gateway.Configurations | |||||
/// http://localhost/ | /// http://localhost/ | ||||
/// http://127.0.0.1/ | /// http://127.0.0.1/ | ||||
/// </summary> | /// </summary> | ||||
public string CoordinatorUri { get; set; } = "http://localhost:1080/"; | |||||
public string CoordinatorUri { get; set; } = "http://localhost:1080/"; | |||||
/// <summary> | |||||
/// 协调器Coordinator主机登录账号 | |||||
/// </summary> | |||||
public string CoordinatorUserName { get; set; } = "admin"; | |||||
/// <summary> | |||||
/// 协调器Coordinator主机登录密码 | |||||
/// </summary> | |||||
public string CoordinatorPassword { get; set; } = "123456"; | |||||
public JT1078Configuration Value => this; | public JT1078Configuration Value => this; | ||||
} | } | ||||
} | } |
@@ -25,6 +25,23 @@ namespace JT1078.Gateway | |||||
this.httpClient = new HttpClient(); | this.httpClient = new HttpClient(); | ||||
this.httpClient.BaseAddress = new Uri(Configuration.CoordinatorUri); | this.httpClient.BaseAddress = new Uri(Configuration.CoordinatorUri); | ||||
this.httpClient.Timeout = TimeSpan.FromSeconds(3); | this.httpClient.Timeout = TimeSpan.FromSeconds(3); | ||||
Login().GetAwaiter().GetResult(); | |||||
} | |||||
/// <summary> | |||||
/// 登录 | |||||
/// </summary> | |||||
public async ValueTask Login() | |||||
{ | |||||
string json = $"{{\"UserName\":\"{Configuration.CoordinatorUserName}\",\"Password\":\"{Configuration.CoordinatorPassword}\"}}"; | |||||
var response = await httpClient.PostAsync($"{endpoint}/User/Login", new StringContent(json)); | |||||
response.EnsureSuccessStatusCode(); | |||||
var token = await response.Content.ReadAsStringAsync(); | |||||
if (string.IsNullOrEmpty(token)) | |||||
{ | |||||
throw new NullReferenceException("token is null"); | |||||
} | |||||
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -32,7 +49,7 @@ namespace JT1078.Gateway | |||||
/// </summary> | /// </summary> | ||||
public async ValueTask Reset() | public async ValueTask Reset() | ||||
{ | { | ||||
await httpClient.GetAsync($"{endpoint}/reset"); | |||||
await httpClient.PostAsync($"{endpoint}/Coordinator/Reset", new StringContent("")); | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -41,7 +58,7 @@ namespace JT1078.Gateway | |||||
/// <param name="content"></param> | /// <param name="content"></param> | ||||
public async ValueTask Heartbeat(string content) | public async ValueTask Heartbeat(string content) | ||||
{ | { | ||||
await httpClient.PostAsync($"{endpoint}/heartbeat", new StringContent(content)); | |||||
await httpClient.PostAsync($"{endpoint}/Coordinator/Heartbeat", new StringContent(content)); | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -49,11 +66,11 @@ namespace JT1078.Gateway | |||||
/// </summary> | /// </summary> | ||||
/// <param name="terminalPhoneNo"></param> | /// <param name="terminalPhoneNo"></param> | ||||
/// <param name="channelNo"></param> | /// <param name="channelNo"></param> | ||||
public async ValueTask ChannelClose(string terminalPhoneNo,int channelNo) | |||||
public async ValueTask ChannelClose(string terminalPhoneNo, int channelNo) | |||||
{ | { | ||||
//todo:通过自维护,当协调重启导致集群内网关未关闭的情况下,通过轮询的方式再去调用 | //todo:通过自维护,当协调重启导致集群内网关未关闭的情况下,通过轮询的方式再去调用 | ||||
string json = $"{{\"TerminalPhoneNo\":\"{terminalPhoneNo}\",\"ChannelNo\":\"{channelNo}\"}}"; | string json = $"{{\"TerminalPhoneNo\":\"{terminalPhoneNo}\",\"ChannelNo\":\"{channelNo}\"}}"; | ||||
await httpClient.PostAsync($"{endpoint}/ChannelClose", new StringContent(json)); | |||||
await httpClient.PostAsync($"{endpoint}/Coordinator/ChannelClose", new StringContent(json)); | |||||
} | } | ||||
} | } | ||||
} | } |