Browse Source

集成Ocelot网关,Consul 服务注册和发现

pull/8/head
王军 4 years ago
parent
commit
d38c1bdf81
  1. 40
      aspnet-core/gateways/CompanyName.ProjectName.WebGateway/Controllers/WeatherForecastController.cs
  2. 15
      aspnet-core/gateways/CompanyName.ProjectName.WebGateway/WeatherForecast.cs
  3. 1
      aspnet-core/gateways/CompanyName.ProjectName.WebGateway/WebGatewayModule.cs
  4. 17
      aspnet-core/gateways/CompanyName.ProjectName.WebGateway/appsettings.Development.json
  5. 2
      aspnet-core/services/host/CompanyName.ProjectName.HttpApi.Host/ProjectNameHttpApiHostModule.cs
  6. 4
      vben271/.env.development
  7. 2
      vben271/src/hooks/web/useSignalR.ts

40
aspnet-core/gateways/CompanyName.ProjectName.WebGateway/Controllers/WeatherForecastController.cs

@ -1,40 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace CompanyName.ProjectName.WebGateway.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]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}

15
aspnet-core/gateways/CompanyName.ProjectName.WebGateway/WeatherForecast.cs

@ -1,15 +0,0 @@
using System;
namespace CompanyName.ProjectName.WebGateway
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string Summary { get; set; }
}
}

1
aspnet-core/gateways/CompanyName.ProjectName.WebGateway/WebGatewayModule.cs

@ -41,6 +41,7 @@ namespace CompanyName.ProjectName.WebGateway
});
app.UseConfiguredEndpoints();
app.UseWebSockets();
app.UseOcelot().Wait();
}

17
aspnet-core/gateways/CompanyName.ProjectName.WebGateway/appsettings.Development.json

@ -47,6 +47,23 @@
"Put",
"Delete"
]
},
{
"DownstreamPathTemplate": "/{url}",
"DownstreamScheme": "ws",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 44315
}
],
"UpstreamPathTemplate": "/ws/{url}",
"UpstreamHttpMethod": [
"Get",
"Post",
"Put",
"Delete"
]
}
]
}

2
aspnet-core/services/host/CompanyName.ProjectName.HttpApi.Host/ProjectNameHttpApiHostModule.cs

@ -122,7 +122,7 @@ namespace CompanyName.ProjectName
Authorization = new[] { new CustomHangfireAuthorizeFilter() },
IgnoreAntiforgeryToken = true
});
if (configuration.GetValue<bool>("Consul:Enabled", false))
{
app.UseConsul();

4
vben271/.env.development

@ -28,4 +28,6 @@ VITE_AUTH_URL= http://localhost:44354
# 接口地址
VITE_API_URL= http://localhost:44314/gateway
VITE_API_URL= http://localhost:44314/gateway
VITE_WEBSOCKE_URL= http://localhost:44314

2
vben271/src/hooks/web/useSignalR.ts

@ -22,7 +22,7 @@ export function useSignalR() {
const userStore = useUserStoreWithOut();
const token = userStore.getToken;
const url = (import.meta.env.VITE_API_URL as string) + '/signalr/notification';
const url = (import.meta.env.VITE_WEBSOCKE_URL as string) + '/ws/signalr/notification';
const connection = new signalR.HubConnectionBuilder()
.withUrl(url, {
accessTokenFactory: () => token,

Loading…
Cancel
Save