簡體   English   中英

如何瀏覽服務結構上的應用程序?

[英]How to browse application on service fabric?

如何找出我應該請求哪個端點才能觸發 GetAccounts?

我在本地集群上運行了兩個應用程序: 在此處輸入圖像描述

在此處輸入圖像描述

Fabric/Service是一個 web 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?

Service Fabric 提供了一個內置的反向代理 它在您的本地開發集群中默認啟用。 反向代理允許您使用動態端口(如 .gif 中所示)。 使用反向代理時,您可以使用反向代理的端口號(默認為 19081)調用您的服務。 您的用例中的地址格式(具有單例分區的無狀態服務)為: protocol://clusterAddress:reverseProxyPort/applicationName/serviceName

在您的示例中,該服務將被調用: http://clusterAddress:19081/Service/Web/api/controller/accounts/GetAccounts

我想這個 Web Api 是暴露給外界的? 您用來托管它的無狀態服務啟用了動態端口。 對於面向外部的服務,最好給它一個固定端口。

在服務清單文件中,您可以在端點定義中添加端口號:

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

有關更多信息,請參閱此鏈接

獲得端口號后,您可以通過http://localhost:80/api/[controller]/accounts訪問 Web api

然后,無論您是否使用動態端口,您都可以在資源管理器中查找實際端口號。

要查看端點端口號,請瀏覽到服務下方的節點,如下所示: 在此處輸入圖像描述

(看到右邊的端點了嗎?)

請注意,如果端點包含特定節點的 ip,則需要集群的 ip 或 FQDN。 但是現在看起來還可以,因為您使用的是 localhost。

在 Service Fabric 中,服務在 Service Fabric 群集中的某個位置運行,通常分布在多個 VM 上。 它可以由服務所有者或由 Service Fabric 自動從一個地方移動到另一個地方。 服務不會靜態地綁定到特定的機器或地址。

Service Fabric 應用程序通常由許多不同的服務組成,其中每個服務執行一項專門的任務。 這些服務可以相互通信以形成完整的功能,例如渲染 Web 應用程序的不同部分。 還有一些客戶端應用程序連接到服務並與之通信。

例如,為了接受 80 端口上的外部流量,必須配置以下內容: 編寫一個監聽 80 端口的服務。在服務的 ServiceManifest.xml 中配置 80 端口,並在服務中打開一個監聽器,例如,一個自托管網絡服務器。

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))};
        }

        ...
    }

本文檔討論如何在 Service Fabric 中設置與服務之間的通信:

連接 Service Fabric 中的服務並與之通信

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM