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

l--*_*''' 9 .net c# asp.net-web-api azure-service-fabric visual-studio-2017

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

我在本地群集上运行了两个应用程序:在此输入图像描述

在此输入图像描述

所述织物/服务是具有以下结构的网页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"))
            };
        }
    }
Run Code Online (Sandbox Code Playgroud)

启动配置如下:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后服务清单:

<?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>
Run Code Online (Sandbox Code Playgroud)

而我的控制器:

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

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

Wou*_*r B 7

在非本地群集上,您可以使用反向代理.反向代理允许您使用动态端口(如.gif中所示).使用反向代理时,您可以使用反向代理的端口号呼叫您的服务.在您的示例中,将使用以下命令调用服务: protocol://clusterAddress:reverseProxyPort/applicationName/serviceName


Pet*_*ons 5

我想这个 Web Api 是暴露给外界的吗?您用来托管它的无状态服务已启用动态端口。对于面向外部的服务,最好为其提供固定端口。

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

<Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" Port="80">
Run Code Online (Sandbox Code Playgroud)

请参阅查看此链接以获取更多信息。

获得端口号后,您可以通过http://localhost:80/api/[controller]/accounts访问 Web api

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

要查看端点端口号,请浏览到服务下的节点,如下所示: 在此输入图像描述

(看到右边的端点了吗?)

请注意,如果端点包含特定节点的 ip,则需要集群的 ip 或 FQDN。但现在看来没问题,因为你使用的是本地主机。