8

如何找出终点,我应该以触发GetAccounts被请求?如何在服务结构上浏览应用程序?

我有我的本地集群上运行的应用程序:enter image description here

enter image description here

布/服务是一个具有以下配置的网络API应用程序:

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[] 
      { 
       new ServiceInstanceListener(serviceContext => new OwinCommunicationListener(Startup.ConfigureApp, 
        serviceContext, ServiceEventSource.Current, "ServiceEndpoint")) 
      }; 
     } 
    } 

启动配置,像这样:

public static class Startup 
{ 
    // This code configures Web API. The Startup class is specified as a type 
    // parameter in the WebApp.Start method. 

    public static void ConfigureApp(IAppBuilder appBuilder) 
    { 
     // Configure Web API for self-host. 
     var config = new HttpConfiguration(); 
     //config.Routes.MapHttpRoute(
     // name: "DefaultApi", 
     // routeTemplate: "api/{controller}/{id}", 
     // defaults: new { id = RouteParameter.Optional } 
     //); 
     config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); 
     config.MapHttpAttributeRoutes(); 
     var container = new UnityContainer(); 
     container.RegisterType<IAccountService, AccountService>(new HierarchicalLifetimeManager()); 
     config.DependencyResolver = new UnityResolver(container); 

     appBuilder.UseWebApi(config); 
    } 
} 

最后的服务清单:

<?xml version="1.0" encoding="utf-8"?> 

<ServiceManifest Name="WebPkg" 
       Version="1.0.0" 
       xmlns="http://schemas.microsoft.com/2011/01/fabric" 
       xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <ServiceTypes> 
    <!-- This is the name of your ServiceType. 
     This name must match the string used in RegisterServiceType call in Program.cs. --> 
    <StatelessServiceType ServiceTypeName="WebType" /> 
    </ServiceTypes> 

    <!-- Code package is your service executable. --> 
    <CodePackage Name="Code" Version="1.0.0"> 
    <EntryPoint> 
     <ExeHost> 
     <Program>removed...........Accounts.Web.exe</Program> 
     <WorkingFolder>CodePackage</WorkingFolder> 
     </ExeHost> 
    </EntryPoint> 
    </CodePackage> 

    <!-- Config package is the contents of the Config directoy under PackageRoot that contains an 
     independently-updateable and versioned set of custom configuration settings for your service. --> 
    <ConfigPackage Name="Config" Version="1.0.0" /> 

    <Resources> 
    <Endpoints> 
     <!-- This endpoint is used by the communication listener to obtain the port on which to 
      listen. Please note that if your service is partitioned, this port is shared with 
      replicas of different partitions that are placed in your code. --> 
     <Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" /> 
    </Endpoints> 
    </Resources> 
</ServiceManifest> 

而且我的控制器:

[HttpGet] 
    [Route("accounts", Name = "GetAccounts")] 
    public async Task<IHttpActionResult> GetAccounts(){//dostuff} 

如何找出终点,我应该以触发GetAccounts被请求?

回答

2

我想这个网页API暴露给外界? 您使用托管它的无状态服务具有动态端口启用。 对于面向外部的服务,最好给它一个固定的端口。

在服务清单文件,你可以在端点定义添加端口号:

<Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" Port="80"> 

查看更多信息请参见this link

一旦你有,你可以在http://localhost:80/api/[controller]/accounts

访问Web API,然后您可以查找在浏览器中的实际端口号,您是否使用动态端口或没有端口号。

要查看端点的端口号,浏览到一个节点服务这样的下面: enter image description here

(?见终点在右侧)

注意,如果端点包含的知识产权您需要集群的ip或FQDN。但是现在看起来好像你正在使用本地主机。

+0

彼得,非常感谢您的帮助。我确实按照你的建议做了,但没有去:https://lh3.googleusercontent.com/-QqFCDujEYys/WSiH1wpAaSI/AAAAAAAAC3E/iijNazjURZw3TCJQHSFH1UKovtYErkHrwCHM/s0/2017-05-26_14-53-42.gif –

+0

@MeggieLuski那个错误表示在端口80处已经有其他东西在监听。尝试首先更改端口号81以查看它的驾驶室工作,然后查找其他过程。你还有IIS还是在localhost上运行的东西? –

+0

我已经更改为81,82,83等,并且结果相同。 –

4

在非本地集群可以使用reverse proxy。反向代理允许您使用动态端口(如.gif所示)。使用反向代理时,您可以使用反向代理的端口号呼叫您的服务。在您的示例中,将使用以下服务调用服务: http://clusterAddress:19081/Service/Web/api/controller/accounts/GetAccounts

4

在Service Fabric中,服务运行在Service Fabric群集中的某处,通常分布在多个VM中。它可以从一个地方移到另一个地方,无论是由服务所有者还是由服务结构自动完成。服务不是静态绑定到特定的机器或地址。

服务结构应用程序通常由许多不同的服务组成,其中每个服务执行专门的任务。这些服务可以相互通信以形成完整的功能,例如渲染Web应用程序的不同部分。还有一些客户端应用程序可以连接到服务并与之通信。

例如,要接受端口80上的外部流量,必须配置以下内容: 编写一个侦听端口80的服务。在服务的ServiceManifest.xml中配置端口80,并在服务中打开一个侦听器例如,一个自托管的Web服务器。

XML

<Resources> 
    <Endpoints> 
     <Endpoint Name="WebEndpoint" Protocol="http" Port="80" /> 
    </Endpoints> 
</Resources> 

C#

class HttpCommunicationListener : ICommunicationListener 
    { 
     ... 

     public Task<string> OpenAsync(CancellationToken cancellationToken) 
     { 
      EndpointResourceDescription endpoint = 
       serviceContext.CodePackageActivationContext.GetEndpoint("WebEndpoint"); 

      string uriPrefix = $"{endpoint.Protocol}://+:{endpoint.Port}/myapp/"; 

      this.httpListener = new HttpListener(); 
      this.httpListener.Prefixes.Add(uriPrefix); 
      this.httpListener.Start(); 

      string publishUri = uriPrefix.Replace("+", FabricRuntime.GetNodeContext().IPAddressOrFQDN); 
      return Task.FromResult(publishUri); 
     } 

     ... 
    } 

class WebService : StatelessService 
    { 
     ... 

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

     ... 
    } 

本文讨论如何设置了和你的服务在服务织物之间的通信:

Connect and communicate with services in Service Fabric

+0

是否可以从应用程序清单中覆盖servicemanifest中的资源部分? –

相关问题