From a652c3182c8f6c7dc608f7692fd80063c6d6f70d Mon Sep 17 00:00:00 2001 From: "SmallChi(Koike)" <564952747@qq.com> Date: Thu, 13 Aug 2020 23:37:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=8F=E8=B0=83=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=E7=9A=84=E6=8E=A7=E5=88=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/CoordinatorController.cs | 57 +++++++++++++++++++ .../Controller/UserController.cs | 31 ++++++++++ .../Dtos/ChannelCloseRequest.cs | 19 +++++++ .../Dtos/HeartbeatRequest.cs | 18 ++++++ .../Dtos/LoginRequest.cs | 13 +++++ src/JT1078.Gateway.Coordinator/Program.cs | 27 +++++---- .../Configurations/JT1078Configuration.cs | 10 +++- .../JT1078CoordinatorHttpClient.cs | 25 ++++++-- 8 files changed, 181 insertions(+), 19 deletions(-) create mode 100644 src/JT1078.Gateway.Coordinator/Controller/CoordinatorController.cs create mode 100644 src/JT1078.Gateway.Coordinator/Controller/UserController.cs create mode 100644 src/JT1078.Gateway.Coordinator/Dtos/ChannelCloseRequest.cs create mode 100644 src/JT1078.Gateway.Coordinator/Dtos/HeartbeatRequest.cs create mode 100644 src/JT1078.Gateway.Coordinator/Dtos/LoginRequest.cs diff --git a/src/JT1078.Gateway.Coordinator/Controller/CoordinatorController.cs b/src/JT1078.Gateway.Coordinator/Controller/CoordinatorController.cs new file mode 100644 index 0000000..a658ba9 --- /dev/null +++ b/src/JT1078.Gateway.Coordinator/Controller/CoordinatorController.cs @@ -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 +{ + /// + /// 协调器中心 + /// + [Route("JT1078WebApi/Coordinator")] + [ApiController] + [EnableCors("any")] + public class CoordinatorController:ControllerBase + { + private ILogger logger; + public CoordinatorController(ILoggerFactory loggerFactory) + { + logger = loggerFactory.CreateLogger(); + } + + /// + /// 集群服务器重置 + /// + [Route("Reset")] + [HttpPost] + public void Reset() + { + + } + + /// + /// 心跳检测 + /// + [Route("Heartbeat")] + [HttpPost] + public void Heartbeat([FromBody] HeartbeatRequest request) + { + + } + + /// + /// 关闭通道 + /// + [Route("ChannelClose")] + [HttpPost] + public void ChannelClose([FromBody] ChannelCloseRequest request) + { + + } + } +} diff --git a/src/JT1078.Gateway.Coordinator/Controller/UserController.cs b/src/JT1078.Gateway.Coordinator/Controller/UserController.cs new file mode 100644 index 0000000..7267601 --- /dev/null +++ b/src/JT1078.Gateway.Coordinator/Controller/UserController.cs @@ -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 +{ + /// + /// 用户功能 + /// + [Route("JT1078WebApi/User")] + [ApiController] + [EnableCors("any")] + public class UserController : ControllerBase + { + + + /// + /// 登录 + /// + [Route("Login")] + [HttpPost] + public void Login([FromBody] LoginRequest request) + { + + } + } +} diff --git a/src/JT1078.Gateway.Coordinator/Dtos/ChannelCloseRequest.cs b/src/JT1078.Gateway.Coordinator/Dtos/ChannelCloseRequest.cs new file mode 100644 index 0000000..bbd0470 --- /dev/null +++ b/src/JT1078.Gateway.Coordinator/Dtos/ChannelCloseRequest.cs @@ -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 + { + /// + /// 设备sim卡号 + /// + public string TerminalPhoneNo { get; set; } + /// + /// 通道号 + /// + public int ChannelNo { get; set; } + } +} diff --git a/src/JT1078.Gateway.Coordinator/Dtos/HeartbeatRequest.cs b/src/JT1078.Gateway.Coordinator/Dtos/HeartbeatRequest.cs new file mode 100644 index 0000000..61e21ee --- /dev/null +++ b/src/JT1078.Gateway.Coordinator/Dtos/HeartbeatRequest.cs @@ -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; } + } +} diff --git a/src/JT1078.Gateway.Coordinator/Dtos/LoginRequest.cs b/src/JT1078.Gateway.Coordinator/Dtos/LoginRequest.cs new file mode 100644 index 0000000..a700ea4 --- /dev/null +++ b/src/JT1078.Gateway.Coordinator/Dtos/LoginRequest.cs @@ -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; } + } +} diff --git a/src/JT1078.Gateway.Coordinator/Program.cs b/src/JT1078.Gateway.Coordinator/Program.cs index cace568..7803b7e 100644 --- a/src/JT1078.Gateway.Coordinator/Program.cs +++ b/src/JT1078.Gateway.Coordinator/Program.cs @@ -20,7 +20,18 @@ namespace JT1078.Gateway.Coordinator .UseEnvironment(args[0]) .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.UseCors("any"); @@ -32,19 +43,6 @@ namespace JT1078.Gateway.Coordinator { 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) => @@ -57,6 +55,7 @@ namespace JT1078.Gateway.Coordinator { services.AddSingleton(); services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); + }) .Build() .Run(); diff --git a/src/JT1078.Gateway/Configurations/JT1078Configuration.cs b/src/JT1078.Gateway/Configurations/JT1078Configuration.cs index 37a76e0..1c82fcb 100644 --- a/src/JT1078.Gateway/Configurations/JT1078Configuration.cs +++ b/src/JT1078.Gateway/Configurations/JT1078Configuration.cs @@ -43,7 +43,15 @@ namespace JT1078.Gateway.Configurations /// http://localhost/ /// http://127.0.0.1/ /// - public string CoordinatorUri { get; set; } = "http://localhost:1080/"; + public string CoordinatorUri { get; set; } = "http://localhost:1080/"; + /// + /// 协调器Coordinator主机登录账号 + /// + public string CoordinatorUserName { get; set; } = "admin"; + /// + /// 协调器Coordinator主机登录密码 + /// + public string CoordinatorPassword { get; set; } = "123456"; public JT1078Configuration Value => this; } } diff --git a/src/JT1078.Gateway/JT1078CoordinatorHttpClient.cs b/src/JT1078.Gateway/JT1078CoordinatorHttpClient.cs index f87e878..48b4728 100644 --- a/src/JT1078.Gateway/JT1078CoordinatorHttpClient.cs +++ b/src/JT1078.Gateway/JT1078CoordinatorHttpClient.cs @@ -25,6 +25,23 @@ namespace JT1078.Gateway this.httpClient = new HttpClient(); this.httpClient.BaseAddress = new Uri(Configuration.CoordinatorUri); this.httpClient.Timeout = TimeSpan.FromSeconds(3); + Login().GetAwaiter().GetResult(); + } + + /// + /// 登录 + /// + 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}"); } /// @@ -32,7 +49,7 @@ namespace JT1078.Gateway /// public async ValueTask Reset() { - await httpClient.GetAsync($"{endpoint}/reset"); + await httpClient.PostAsync($"{endpoint}/Coordinator/Reset", new StringContent("")); } /// @@ -41,7 +58,7 @@ namespace JT1078.Gateway /// public async ValueTask Heartbeat(string content) { - await httpClient.PostAsync($"{endpoint}/heartbeat", new StringContent(content)); + await httpClient.PostAsync($"{endpoint}/Coordinator/Heartbeat", new StringContent(content)); } /// @@ -49,11 +66,11 @@ namespace JT1078.Gateway /// /// /// - public async ValueTask ChannelClose(string terminalPhoneNo,int channelNo) + public async ValueTask ChannelClose(string terminalPhoneNo, int channelNo) { //todo:通过自维护,当协调重启导致集群内网关未关闭的情况下,通过轮询的方式再去调用 string json = $"{{\"TerminalPhoneNo\":\"{terminalPhoneNo}\",\"ChannelNo\":\"{channelNo}\"}}"; - await httpClient.PostAsync($"{endpoint}/ChannelClose", new StringContent(json)); + await httpClient.PostAsync($"{endpoint}/Coordinator/ChannelClose", new StringContent(json)); } } }