From 27698a9af4171c8852896c141d8ce6ae41bf94eb Mon Sep 17 00:00:00 2001 From: Savorboard Date: Fri, 29 Sep 2017 17:46:52 +0800 Subject: [PATCH] remove cap.UseDashboard. It's will be automatically enabled by registerd services. --- samples/Sample.RabbitMQ.PostgreSql/Startup.cs | 25 ++++++-- samples/Sample.RabbitMQ.SqlServer/Startup.cs | 5 +- .../CAP.AppBuilderExtensions.cs | 24 +++----- .../Dashboard/CAP.DashboardMiddleware.cs | 61 +++++++++---------- .../GatewayProxy/GatewayProxyMiddleware.cs | 51 +++++++++------- .../Dashboard/JsonDispatcher.cs | 2 +- src/DotNetCore.CAP/IQueueExecutor.Subscibe.cs | 1 - 7 files changed, 89 insertions(+), 80 deletions(-) diff --git a/samples/Sample.RabbitMQ.PostgreSql/Startup.cs b/samples/Sample.RabbitMQ.PostgreSql/Startup.cs index ac7ead0..e0a1b4f 100644 --- a/samples/Sample.RabbitMQ.PostgreSql/Startup.cs +++ b/samples/Sample.RabbitMQ.PostgreSql/Startup.cs @@ -4,10 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; namespace Sample.RabbitMQ.PostgreSql { @@ -16,7 +13,25 @@ namespace Sample.RabbitMQ.PostgreSql // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - + services.AddCap(x => + { + x.UseEntityFramework(); + x.UseRabbitMQ(z => + { + z.HostName = "192.168.2.206"; + z.UserName = "admin"; + z.Password = "123123"; + }); + x.UseDashboard(); + x.UseDiscovery(d => + { + d.DiscoveryServerHostName = "localhost"; + d.DiscoveryServerProt = 8500; + d.CurrentNodeHostName = "localhost"; + d.CurrentNodePort = 5800; + d.NodeName = "CAP一号节点"; + }); + }); services.AddMvc(); } @@ -24,6 +39,8 @@ namespace Sample.RabbitMQ.PostgreSql public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMvc(); + + app.UseCap(); } } } diff --git a/samples/Sample.RabbitMQ.SqlServer/Startup.cs b/samples/Sample.RabbitMQ.SqlServer/Startup.cs index 730b58c..6cd19cd 100644 --- a/samples/Sample.RabbitMQ.SqlServer/Startup.cs +++ b/samples/Sample.RabbitMQ.SqlServer/Startup.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Sample.RabbitMQ.SqlServer.Services; @@ -26,7 +25,7 @@ namespace Sample.RabbitMQ.SqlServer z.UserName = "admin"; z.Password = "123123"; }); - x.UseDashboard(); + x.UseDashboard(d => { d.StatsPollingInterval = 1000000; }); x.UseDiscovery(d => { d.DiscoveryServerHostName = "localhost"; @@ -48,8 +47,6 @@ namespace Sample.RabbitMQ.SqlServer app.UseMvc(); app.UseCap(); - - app.UseCapDashboard(); } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP/CAP.AppBuilderExtensions.cs b/src/DotNetCore.CAP/CAP.AppBuilderExtensions.cs index 4e958a3..1caf994 100644 --- a/src/DotNetCore.CAP/CAP.AppBuilderExtensions.cs +++ b/src/DotNetCore.CAP/CAP.AppBuilderExtensions.cs @@ -26,24 +26,18 @@ namespace Microsoft.AspNetCore.Builder CheckRequirement(app); var provider = app.ApplicationServices; + var bootstrapper = provider.GetRequiredService(); bootstrapper.BootstrapAsync(); - return app; - } - /// - /// Enables cap dashboard for the current application - /// - /// The instance this method extends. - /// The instance this method extends. - public static IApplicationBuilder UseCapDashboard(this IApplicationBuilder app) - { - if (app == null) throw new ArgumentNullException(nameof(app)); - - CheckRequirement(app); - - app.UseMiddleware(); - app.UseMiddleware(); + if (provider.GetService() != null) + { + if (provider.GetService() != null) + { + app.UseMiddleware(); + } + app.UseMiddleware(); + } return app; } diff --git a/src/DotNetCore.CAP/Dashboard/CAP.DashboardMiddleware.cs b/src/DotNetCore.CAP/Dashboard/CAP.DashboardMiddleware.cs index f18d1d0..072d8ba 100644 --- a/src/DotNetCore.CAP/Dashboard/CAP.DashboardMiddleware.cs +++ b/src/DotNetCore.CAP/Dashboard/CAP.DashboardMiddleware.cs @@ -25,49 +25,44 @@ namespace DotNetCore.CAP public Task Invoke(HttpContext context) { - if (context.Request.Path.StartsWithSegments(_options.PathMatch, - out var matchedPath, out var remainingPath)) + if (!context.Request.Path.StartsWithSegments(_options.PathMatch, + out var matchedPath, out var remainingPath)) return _next(context); + + // Update the path + var path = context.Request.Path; + var pathBase = context.Request.PathBase; + context.Request.PathBase = pathBase.Add(matchedPath); + context.Request.Path = remainingPath; + + try { - // Update the path - var path = context.Request.Path; - var pathBase = context.Request.PathBase; - context.Request.PathBase = pathBase.Add(matchedPath); - context.Request.Path = remainingPath; + var dashboardContext = new CapDashboardContext(_storage, _options, context); + var findResult = _routes.FindDispatcher(context.Request.Path.Value); - try + if (findResult == null) { - var dashboardContext = new CapDashboardContext(_storage, _options, context); - var findResult = _routes.FindDispatcher(context.Request.Path.Value); - - if (findResult == null) - { - return _next.Invoke(context); - } + return _next.Invoke(context); + } - if (_options.Authorization.Any(filter => !filter.Authorize(dashboardContext))) - { - var isAuthenticated = context.User?.Identity?.IsAuthenticated; + if (_options.Authorization.Any(filter => !filter.Authorize(dashboardContext))) + { + var isAuthenticated = context.User?.Identity?.IsAuthenticated; - context.Response.StatusCode = isAuthenticated == true - ? (int)HttpStatusCode.Forbidden - : (int)HttpStatusCode.Unauthorized; + context.Response.StatusCode = isAuthenticated == true + ? (int)HttpStatusCode.Forbidden + : (int)HttpStatusCode.Unauthorized; - return Task.CompletedTask; - } + return Task.CompletedTask; + } - dashboardContext.UriMatch = findResult.Item2; + dashboardContext.UriMatch = findResult.Item2; - return findResult.Item1.Dispatch(dashboardContext); - } - finally - { - context.Request.PathBase = pathBase; - context.Request.Path = path; - } + return findResult.Item1.Dispatch(dashboardContext); } - else + finally { - return _next(context); + context.Request.PathBase = pathBase; + context.Request.Path = path; } } } diff --git a/src/DotNetCore.CAP/Dashboard/GatewayProxy/GatewayProxyMiddleware.cs b/src/DotNetCore.CAP/Dashboard/GatewayProxy/GatewayProxyMiddleware.cs index a33568a..645b713 100644 --- a/src/DotNetCore.CAP/Dashboard/GatewayProxy/GatewayProxyMiddleware.cs +++ b/src/DotNetCore.CAP/Dashboard/GatewayProxy/GatewayProxyMiddleware.cs @@ -46,40 +46,48 @@ namespace DotNetCore.CAP.Dashboard.GatewayProxy var request = context.Request; var pathMatch = discoveryOptions.MatchPath; var isCapRequest = request.Path.StartsWithSegments(new PathString(pathMatch)); - - var isSwitchNode = request.Cookies.TryGetValue(NodeCookieName, out string requestNodeId); - var isCurrentNode = discoveryOptions.NodeId.ToString() == requestNodeId; - - if (!isCapRequest || !isSwitchNode || isCurrentNode) + if (!isCapRequest) { await _next.Invoke(context); } else { - _logger.LogDebug("started calling gateway proxy middleware"); + //For performance reasons, we need to put this functionality in the else + var isSwitchNode = request.Cookies.TryGetValue(NodeCookieName, out string requestNodeId); + var isCurrentNode = discoveryOptions.NodeId.ToString() == requestNodeId; + var isNodesPage = request.Path.StartsWithSegments(new PathString(pathMatch + "/nodes")); - if (TryGetRemoteNode(requestNodeId, out Node node)) + if (!isSwitchNode || isCurrentNode || isNodesPage) + { + await _next.Invoke(context); + } + else { - try + _logger.LogDebug("started calling gateway proxy middleware"); + + if (TryGetRemoteNode(requestNodeId, out Node node)) { - DownstreamRequest = await _requestMapper.Map(request); + try + { + DownstreamRequest = await _requestMapper.Map(request); - SetDownStreamRequestUri(node, request.Path.Value); + SetDownStreamRequestUri(node, request.Path.Value); - var response = await _requester.GetResponse(DownstreamRequest); + var response = await _requester.GetResponse(DownstreamRequest); - await SetResponseOnHttpContext(context, response); + await SetResponseOnHttpContext(context, response); + } + catch (Exception ex) + { + _logger.LogError(ex.Message); + } } - catch(Exception ex) + else { - _logger.LogError(ex.Message); + context.Response.Cookies.Delete(NodeCookieName); + await _next.Invoke(context); } } - else - { - context.Response.Cookies.Delete(NodeCookieName); - await _next.Invoke(context); - } } } @@ -90,10 +98,9 @@ namespace DotNetCore.CAP.Dashboard.GatewayProxy AddHeaderIfDoesntExist(context, httpResponseHeader); } - var stringContent = await response.Content.ReadAsStringAsync(); var content = await response.Content.ReadAsByteArrayAsync(); - AddHeaderIfDoesntExist(context, + AddHeaderIfDoesntExist(context, new KeyValuePair>("Content-Length", new[] { content.Length.ToString() })); context.Response.OnStarting(state => @@ -128,7 +135,7 @@ namespace DotNetCore.CAP.Dashboard.GatewayProxy DownstreamRequest.RequestUri = uriBuilder.Uri; } - private static void AddHeaderIfDoesntExist(HttpContext context, + private static void AddHeaderIfDoesntExist(HttpContext context, KeyValuePair> httpResponseHeader) { if (!context.Response.Headers.ContainsKey(httpResponseHeader.Key)) diff --git a/src/DotNetCore.CAP/Dashboard/JsonDispatcher.cs b/src/DotNetCore.CAP/Dashboard/JsonDispatcher.cs index 2cfd092..621ab19 100644 --- a/src/DotNetCore.CAP/Dashboard/JsonDispatcher.cs +++ b/src/DotNetCore.CAP/Dashboard/JsonDispatcher.cs @@ -42,7 +42,7 @@ namespace DotNetCore.CAP.Dashboard } context.Response.ContentType = "application/json"; - await context.Response.WriteAsync(serialized); + await context.Response.WriteAsync(serialized ?? string.Empty); } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP/IQueueExecutor.Subscibe.cs b/src/DotNetCore.CAP/IQueueExecutor.Subscibe.cs index 2e3cc72..b1bdad1 100644 --- a/src/DotNetCore.CAP/IQueueExecutor.Subscibe.cs +++ b/src/DotNetCore.CAP/IQueueExecutor.Subscibe.cs @@ -124,7 +124,6 @@ namespace DotNetCore.CAP { var retryBehavior = RetryBehavior.DefaultRetry; - var now = DateTime.Now; var retries = ++message.Retries; if (retries >= retryBehavior.RetryCount) {