如何浏览服务架构上的应用程序?

问题描述 投票:9回答:3

如何找出我应该请求的端点以触发GetAccounts?

我在我的本地集群上运行了两个应用程序:enter image description here

enter image description here

结构/服务是一个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?

c# .net asp.net-web-api visual-studio-2017 azure-service-fabric
3个回答
7
投票

Service Fabric提供内置的reverse proxy。默认情况下,它在本地开发群集中启用。反向代理允许您使用动态端口(如.gif中所示)。使用反向代理时,您可以使用反向代理的端口号调用您的服务(默认情况下为19081)。用例中的地址格式是一个带有单例分区的无状态服务,它是:protocol://clusterAddress:reverseProxyPort/applicationName/serviceName

在您的示例中,将使用以下命令调用服务:http://clusterAddress:19081/Service/Web/api/controller/accounts/GetAccounts


4
投票

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

Service Fabric应用程序通常由许多不同的服务组成,其中每个服务执行专门的任务。这些服务可以彼此通信以形成完整的功能,例如呈现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))};
        }

        ...
    }

本文档讨论如何在Service Fabric中设置与服务之间的通信:

Connect and communicate with services in Service Fabric


3
投票

我想这个Web Api暴露在外面的世界?您用于托管它的无状态服务已启用动态端口。对于面向外部的服务,最好给它一个固定的端口。

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

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

有关详细信息,请参阅this link

获得portnumber后,您可以访问http://localhost:80/api/[controller]/accounts上的web api

然后,您可以在资源管理器中查找实际端口号,无论您是否使用动态端口。

要查看端点端口号,请浏览服务下方的节点,如下所示:enter image description here

(参见右侧的端点?)

请注意,如果端点包含特定节点的IP,则需要群集的ip或FQDN。但是现在看起来好了,因为你使用的是localhost。

© www.soinside.com 2019 - 2024. All rights reserved.