Service-Fabric 绑定到多个端点
Service-Fabric bind to multiple endpoints
是否可以绑定一个 Service Fabric 应用程序以侦听多个端口?
基本上我正在尝试拥有一个面向 public 的服务,该服务侦听 http:80 和 https:443,并将任何 http 请求重定向到 https。
我创建了一个新的 ASP.net 核心服务,它单独运行良好。 IE。使用 SSL 443 或非 SSL 80,但是当我同时添加两者时 ServiceInstanceListeners
它就失败了!
Service Fabric Explorer 在多次超时后显示以下错误:
Unhealthy event: SourceId='System.RA', Property='ReplicaOpenStatus', HealthState='Warning', ConsiderWarningAsError=false.
Replica had multiple failures in API call: IStatelessServiceInstance.Open(); Error = System.Fabric.FabricElementAlreadyExistsException (-2146233088)
Unique Name must be specified for each listener when multiple communication listeners are used
at Microsoft.ServiceFabric.Services.Communication.ServiceEndpointCollection.AddEndpointCallerHoldsLock(String listenerName, String endpointAddress)
at Microsoft.ServiceFabric.Services.Communication.ServiceEndpointCollection.AddEndpoint(String listenerName, String endpointAddress)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__0.MoveNext()
这很奇怪,因为两个听众的名字不同——看起来是这样。有什么地方我应该设置我错过的侦听器名称吗?
我正在为此使用 Asp.net 核心模板。我的无状态服务代码如下:
internal sealed class Web : StatelessService
{
public Web(StatelessServiceContext context)
: base(context)
{ }
/// <summary>
/// Optional override to create listeners (like tcp, http) for this service instance.
/// </summary>
/// <returns>The collection of listeners.</returns>
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[]
{
new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, "ServiceEndpointHttps", url =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
return new WebHostBuilder()
.UseWebListener()
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(url)
.Build();
})),
new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, "ServiceEndpointHttp", url =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
return new WebHostBuilder()
.UseWebListener()
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(url)
.Build();
}))
};
}
}
我需要在具有构造函数
的ServiceInstanceListener
上设置名称
public ServiceInstanceListener(Func<StatelessServiceContext, ICommunicationListener> createCommunicationListener, string name = "");
我没有意识到它有额外的参数:)
您可以使用下面的代码自动执行所有这些操作,并在需要的地方记录所需的信息。
var currentEndpoint = "";
try
{
IList<ServiceInstanceListener> listeners = new List<ServiceInstanceListener>();
var endpoints = FabricRuntime.GetActivationContext().GetEndpoints();
foreach (var endpoint in endpoints)
{
currentEndpoint = endpoint.Name;
logger.LogInformation("Website trying to LISTEN : " + currentEndpoint);
var webListner = new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, endpoint.Name, (url, listener) =>
{
url = endpoint.Protocol + "://+:" + endpoint.Port;
logger.LogInformation("Website Listening : " + currentEndpoint);
return new WebHostBuilder().UseWebListener() .UseContentRoot(Directory.GetCurrentDirectory())
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseStartup<Startup>()
.UseUrls(url)
.Build();
}), endpoint.Name.ToString());
listeners.Add(webListner);
}
return listeners;
}
catch (Exception ex)
{
logger.LogError("Exception occured while listening endpoint: " + currentEndpoint, ex);
throw;
}
是否可以绑定一个 Service Fabric 应用程序以侦听多个端口?
基本上我正在尝试拥有一个面向 public 的服务,该服务侦听 http:80 和 https:443,并将任何 http 请求重定向到 https。
我创建了一个新的 ASP.net 核心服务,它单独运行良好。 IE。使用 SSL 443 或非 SSL 80,但是当我同时添加两者时 ServiceInstanceListeners
它就失败了!
Service Fabric Explorer 在多次超时后显示以下错误:
Unhealthy event: SourceId='System.RA', Property='ReplicaOpenStatus', HealthState='Warning', ConsiderWarningAsError=false.
Replica had multiple failures in API call: IStatelessServiceInstance.Open(); Error = System.Fabric.FabricElementAlreadyExistsException (-2146233088)
Unique Name must be specified for each listener when multiple communication listeners are used
at Microsoft.ServiceFabric.Services.Communication.ServiceEndpointCollection.AddEndpointCallerHoldsLock(String listenerName, String endpointAddress)
at Microsoft.ServiceFabric.Services.Communication.ServiceEndpointCollection.AddEndpoint(String listenerName, String endpointAddress)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__0.MoveNext()
这很奇怪,因为两个听众的名字不同——看起来是这样。有什么地方我应该设置我错过的侦听器名称吗?
我正在为此使用 Asp.net 核心模板。我的无状态服务代码如下:
internal sealed class Web : StatelessService
{
public Web(StatelessServiceContext context)
: base(context)
{ }
/// <summary>
/// Optional override to create listeners (like tcp, http) for this service instance.
/// </summary>
/// <returns>The collection of listeners.</returns>
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[]
{
new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, "ServiceEndpointHttps", url =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
return new WebHostBuilder()
.UseWebListener()
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(url)
.Build();
})),
new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, "ServiceEndpointHttp", url =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting WebListener on {url}");
return new WebHostBuilder()
.UseWebListener()
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(url)
.Build();
}))
};
}
}
我需要在具有构造函数
的ServiceInstanceListener
上设置名称
public ServiceInstanceListener(Func<StatelessServiceContext, ICommunicationListener> createCommunicationListener, string name = "");
我没有意识到它有额外的参数:)
您可以使用下面的代码自动执行所有这些操作,并在需要的地方记录所需的信息。
var currentEndpoint = "";
try
{
IList<ServiceInstanceListener> listeners = new List<ServiceInstanceListener>();
var endpoints = FabricRuntime.GetActivationContext().GetEndpoints();
foreach (var endpoint in endpoints)
{
currentEndpoint = endpoint.Name;
logger.LogInformation("Website trying to LISTEN : " + currentEndpoint);
var webListner = new ServiceInstanceListener(serviceContext =>
new WebListenerCommunicationListener(serviceContext, endpoint.Name, (url, listener) =>
{
url = endpoint.Protocol + "://+:" + endpoint.Port;
logger.LogInformation("Website Listening : " + currentEndpoint);
return new WebHostBuilder().UseWebListener() .UseContentRoot(Directory.GetCurrentDirectory())
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseStartup<Startup>()
.UseUrls(url)
.Build();
}), endpoint.Name.ToString());
listeners.Add(webListner);
}
return listeners;
}
catch (Exception ex)
{
logger.LogError("Exception occured while listening endpoint: " + currentEndpoint, ex);
throw;
}