3

服务织物样品如单词计数的Web应用程序监听端口在这样的子路径部署在Azure服务织物Asp.Net核心应用:如何使用子路径在集群上共享相同的端口

http://localhost:8081/wordcount

这种配置的代码是:(见GitHub上https://github.com/Azure-Samples/service-fabric-dotnet-getting-started/blob/master/Services/WordCount/WordCount.WebService/WordCountWebService.cs文件)

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners() 
     { 
      return new[] 
      { 
       new ServiceInstanceListener(initParams => new OwinCommunicationListener("wordcount", new Startup(), initParams)) 
      }; 
     } 

有了这个配置,我们可以使用相同的端口在同一个集群上部署的其他Web应用程序(8081)

http://localhost:8081/wordcount

http://localhost:8081/app1

http://localhost:8081/app2

等。

但Asp.Net Core项目模板是不同的,我不知道如何添加侦听器配置的子路径。

下面的代码是我们所拥有的项目模板(Program.cs中类WebHostingService):

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners() 
      { 
       return new[] { new ServiceInstanceListener(_ => this) }; 
      } 

Task<string> ICommunicationListener.OpenAsync(CancellationToken cancellationToken) 
      { 
       var endpoint = FabricRuntime.GetActivationContext().GetEndpoint(_endpointName); 

       string serverUrl = $"{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}"; 

       _webHost = new WebHostBuilder().UseKestrel() 
               .UseContentRoot(Directory.GetCurrentDirectory()) 
               .UseStartup<Startup>() 
               .UseUrls(serverUrl) 
               .Build(); 

       _webHost.Start(); 

       return Task.FromResult(serverUrl); 
      } 

语义是有点不同,但都在同一个点结束。 的问题是,即使我想补充的子路径在的serverUrl结束但这并没有工作和web应用程序总是响应根http://localhost:8081/

见我如何下面的代码片段尝试:

string serverUrl = $"{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}/app1"; 

如何使用asp.net核心实现与“经典”网络应用程序相同的结果?

我们的目标是在Azure上发布的80端口,让用户像一个更好的体验:

http://mywebsite.com/app1

http://mywebsite.com/app2

谢谢你很多!

+0

https://docs.microsoft。com/en-us/azure/service-fabric/service-fabric-reliable-services-communication-aspnetcore#服务结构集成中间件 – LaPuyaLoca

+0

伟大的加法@LaPuyaLoca –

回答

1

红隼不支持URL前缀或多个应用程序之间的端口共享。你必须使用WebListener代替:

using Microsoft.AspNetCore.Hosting ... _webHost = new WebHostBuilder().UseWebListener()

+0

您可以帮助一个示例吗?没有我尝试过的组合工作...非常感谢你 –

1

我还没有这样做,但是这个GitHub存储库有用吗?

https://github.com/weidazhao/Hosting

有关样本

此示例演示:

1.How ASP.NET核心可以在无状态/有状态服务的通信侦听器来使用。今天,我们启用的场景是将ASP.NET Core Web应用程序作为Service Fabric的无状态服务托管。我们想要点亮人们也可以使用ASP.NET Core作为无状态服务和有状态服务中的通信监听器的场景,类似于OwinCommunicationListener。

2。如何构建一个API网关服务,以将请求转发到其后的多个微服务器,并使用可重用的模块化组件。 Service Fabric是构建微服务的绝佳平台。网关中间件(Microsoft.ServiceFabric.AspNetCore.Gateway)试图为人们提供构建基块,以便轻松实现Service Fabric上的微服务的API网关模式。有几篇很好的文章阐述了API网关模式,例如http://microservices.io/patterns/apigateway.html,http://www.infoq.com/articles/microservices-intro等。有关微服务的更多信息,请查看https://azure.microsoft.com/en-us/blog/microservices-an-application-revolution-powered-by-the-cloud/,http://martinfowler.com/articles/microservices.html

+0

通过示例方法可以在同一个服务上运行多个服务 HTTP:使用他们的名字,如端口//本地主机:20000 /服务1 <--- SVC的应用1 的http://本地主机:20000 /服务2 <--- SVC的应用1 这是可能的,因为在那里网关服务将URI中的地址service1和service2映射到正确的服务。 但我找不到在同一端口上运行2个不同应用程序的方法。 这可能吗? http:// localhost:20000/service1 <---应用程序1中的Svc http:// localhost:20000/service2 <---应用程序2中的Svc –

1

@Nick兰德尔

与样品的方法可以使用自己的姓名相同的端口上运行的若干服务:

http://localhost:20000/service1 < --- SVC的应用1

http://localhost:20000/service2 < ---应用程序中的Svc

这是可能的,因为有一个网关服务,米将URI中的地址service1和service2映射到正确的服务。

但我找不到在同一端口上运行2个不同应用程序的方法。

可能吗?

http://localhost:20000/service1 < --- SVC的应用1

http://localhost:20000/service2 < --- SVC在应用2

4

由于@Vaclav说,有必要通过UseWebListener改变UseKestrel。

但问题是WebListener绑定到地址是不同的。

看看这个线程的详细信息https://github.com/aspnet/Hosting/issues/749

需要使用+而不是在本地主机的serverUrl或其他计算机名称。

所以,改变德模板代码:

  Task<string> ICommunicationListener.OpenAsync(CancellationToken cancellationToken) 
     { 
      var endpoint = FabricRuntime.GetActivationContext().GetEndpoint(_endpointName); 


      string serverUrl = $"{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}/service1"; 

      _webHost = new WebHostBuilder().UseKestrel() 
              .UseContentRoot(Directory.GetCurrentDirectory()) 
              .UseStartup<Startup>() 
              .UseUrls(serverUrl) 
              .Build(); 

      _webHost.Start(); 

      return Task.FromResult(serverUrl); 
     } 

  Task<string> ICommunicationListener.OpenAsync(CancellationToken cancellationToken) 
      { 
       var endpoint = FabricRuntime.GetActivationContext().GetEndpoint(_endpointName); 

       string serverUrl = $"{endpoint.Protocol}://+:{endpoint.Port}/service1"; 

       _webHost = new WebHostBuilder().UseWebListener() 
               .UseContentRoot(Directory.GetCurrentDirectory()) 
               .UseStartup<Startup>() 
               .UseUrls(serverUrl) 
               .Build(); 

       _webHost.Start(); 

       return Task.FromResult(serverUrl); 
      } 

而且它workd非常好。

相关问题