简体   繁体   English

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

[英]How to browse application on service fabric?

How do I find out what endpoint I should be requesting in order to trigger GetAccounts?如何找出我应该请求哪个端点才能触发 GetAccounts?

I've got two applications running on my local cluster:我在本地集群上运行了两个应用程序: 在此处输入图像描述

在此处输入图像描述

The fabric/Service is a web api application with the following configuration: 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"))
            };
        }
    }

The startup is configured like so:启动配置如下:

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

And finally the service manifest:最后是服务清单:

<?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>

And my controller:我的控制器:

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

How do I find out what endpoint I should be requesting in order to trigger GetAccounts?如何找出我应该请求哪个端点才能触发 GetAccounts?

Service Fabric provides a built-in reverse proxy . Service Fabric 提供了一个内置的反向代理 It is enabled by default in your local development cluster.它在您的本地开发集群中默认启用。 A reverse proxy allows you to use dynamic ports (as shown in your .gif).反向代理允许您使用动态端口(如 .gif 中所示)。 When using a reverse proxy you can call your service with the port number of your reverse proxy (19081 by default).使用反向代理时,您可以使用反向代理的端口号(默认为 19081)调用您的服务。 The address format in your use case, a stateless service with a singleton partition, is: protocol://clusterAddress:reverseProxyPort/applicationName/serviceName您的用例中的地址格式(具有单例分区的无状态服务)为: protocol://clusterAddress:reverseProxyPort/applicationName/serviceName

In your example the service would be called with: http://clusterAddress:19081/Service/Web/api/controller/accounts/GetAccounts在您的示例中,该服务将被调用: http://clusterAddress:19081/Service/Web/api/controller/accounts/GetAccounts

I suppose this Web Api is exposed to the outside world?我想这个 Web Api 是暴露给外界的? The stateless service you are using to host it in has dynamic port enabled.您用来托管它的无状态服务启用了动态端口。 For external facing services it is best to give it a fixed port.对于面向外部的服务,最好给它一个固定端口。

In the service manifest file you can add the port number in the endpoint definition:在服务清单文件中,您可以在端点定义中添加端口号:

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

See See this link for more info.有关更多信息,请参阅此链接

Once you have the portnumber you can access the web api at http://localhost:80/api/[controller]/accounts获得端口号后,您可以通过http://localhost:80/api/[controller]/accounts访问 Web api

You can then lookup the actual port number in the explorer, whether you are using dynamic ports or not.然后,无论您是否使用动态端口,您都可以在资源管理器中查找实际端口号。

To see the endpoint port number browse to a node beneath your service like this:要查看端点端口号,请浏览到服务下方的节点,如下所示: 在此处输入图像描述

(See the endpoint at the right side?) (看到右边的端点了吗?)

Note that if the endpoint contains the ip of a specific node, you need the ip or FQDN of the cluster.请注意,如果端点包含特定节点的 ip,则需要集群的 ip 或 FQDN。 But for now it seems ok since you are using localhost.但是现在看起来还可以,因为您使用的是 localhost。

In Service Fabric, a service runs somewhere in a Service Fabric cluster, typically distributed across multiple VMs.在 Service Fabric 中,服务在 Service Fabric 群集中的某个位置运行,通常分布在多个 VM 上。 It can be moved from one place to another, either by the service owner, or automatically by Service Fabric.它可以由服务所有者或由 Service Fabric 自动从一个地方移动到另一个地方。 Services are not statically tied to a particular machine or address.服务不会静态地绑定到特定的机器或地址。

A Service Fabric application is generally composed of many different services, where each service performs a specialized task. Service Fabric 应用程序通常由许多不同的服务组成,其中每个服务执行一项专门的任务。 These services may communicate with each other to form a complete function, such as rendering different parts of a web application.这些服务可以相互通信以形成完整的功能,例如渲染 Web 应用程序的不同部分。 There are also client applications that connect to and communicate with services.还有一些客户端应用程序连接到服务并与之通信。

For example, in order to accept external traffic on port 80, the following things must be configured: Write a service that listens on port 80. Configure port 80 in the service's ServiceManifest.xml and open a listener in the service, for example, a self-hosted web server.例如,为了接受 80 端口上的外部流量,必须配置以下内容: 编写一个监听 80 端口的服务。在服务的 ServiceManifest.xml 中配置 80 端口,并在服务中打开一个监听器,例如,一个自托管网络服务器。

XML XML

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

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

        ...
    }

This document discusses how to set up communication with and between services in Service Fabric:本文档讨论如何在 Service Fabric 中设置与服务之间的通信:

Connect and communicate with services in Service Fabric 连接 Service Fabric 中的服务并与之通信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM