从集群中的其他应用程序调用服务 [英] Calling services from other application in the cluster

查看:86
本文介绍了从集群中的其他应用程序调用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Service Fabric群集中将服务或参与者从一个应用程序调用到另一个应用程序?尝试时(使用带有适当Uri的ActorProxy.Create),我得到了找不到接口的MethodDispatcher"

Is it possible to call services or actors from one application to another in a Service Fabric Cluster ? When I tryed (using ActorProxy.Create with the proper Uri), I got a "No MethodDispatcher is found for interface"

推荐答案

是的,有可能.只要您有权使用服务(或ActorService)的Uri,您就可以使用定义服务或参与者的接口访问程序集,它与调用服务/参与者没有太大区别从同一应用程序中.如果您已为您启用了安全性服务,那么您还必须设置用于交换的证书.

Yes, it is possible. As long as you have the right Uri to the Service (or ActorService) and you have access to the assembly with the interface defining your service or actor the it should not be much different than calling the Service/Actor from within the same application. It you have enabled security for your service then you have to setup the certificates for the exchange as well.

如果我有一个简单的服务定义为:

If I have a simple service defined as:

public interface ICalloutService : IService
{
    Task<string> SayHelloAsync();
}

internal sealed class CalloutService : StatelessService, ICalloutService
{
    public CalloutService(StatelessServiceContext context)
        : base(context) { }

    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        yield return new ServiceInstanceListener(this.CreateServiceRemotingListener);
    }

    public Task<string> SayHelloAsync()
    {
        return Task.FromResult("hello");
    }
}

和一个简单的演员:

public interface ICalloutActor : IActor
{
    Task<string> SayHelloAsync();
}

[StatePersistence(StatePersistence.None)]
internal class CalloutActor : Actor, ICalloutActor
{
    public CalloutActor(ActorService actorService, ActorId actorId)
        : base(actorService, actorId) {}

    public Task<string> SayHelloAsync()
    {
        return Task.FromResult("hello");
    }
}

在这样的应用程序中运行:

running in a application like this:

然后,您可以从同一集群中的另一个应用程序调用它:

Then you can call it from another application within the same cluster:

        // Call the service
        var calloutServiceUri = new Uri(@"fabric:/ServiceFabric.SO.Answer._41655575/CalloutService");
        var calloutService = ServiceProxy.Create<ICalloutService>(calloutServiceUri);
        var serviceHello = await calloutService.SayHelloAsync();

        // Call the actor
        var calloutActorServiceUri = new Uri(@"fabric:/ServiceFabric.SO.Answer._41655575/CalloutActorService");
        var calloutActor = ActorProxy.Create<ICalloutActor>(new ActorId(DateTime.Now.Millisecond), calloutActorServiceUri);
        var actorHello = await calloutActor.SayHelloAsync();

如果单击服务并查看名称,则可以在Service Fabric资源管理器中找到正确的Uri.默认情况下,服务的Uri为:fabric:/{applicationName}/{serviceName}.

You can find the right Uri in the Service Fabric Explorer if you click the service and look at the name. By default the Uri of a service is: fabric:/{applicationName}/{serviceName}.

唯一棘手的部分是如何获得从外部服务到呼叫服务的接口?您可以简单地引用要调用的服务的内置.exe,也可以将包含接口的程序集打包为NuGet程序包,然后放入私有供稿.

The only tricky part is how do you get the interface from the external service to your calling service? You could simply reference the built .exe for the service you wish to call or you could package the assembly containing the interface as a NuGet package and put on a private feed.

如果您不这样做,而只是在Visual Studio解决方案之间共享代码,则Service Fabric会认为这是两个不同的接口,即使它们共享完全相同的签名也是如此.如果为服务执行此操作,则会得到NotImplementedException并显示接口ID'{xxxxxxxx}'不是由对象'{service}'实现的"",而如果为Actor执行此操作,则会得到KeyNotfoundException并显示找到接口ID为'-{xxxxxxxxxx}'的MethodDispatcher.

If you don't do this and you instead just share the code between your Visual Studio solutions the Service Fabric will think these are two different interfaces, even if they share the exact same signature. If you do it for a Service you get an NotImplementedException saying "Interface id '{xxxxxxxx}' is not implemented by object '{service}'" and if you do it for an Actor you get an KeyNotfoundException saying "No MethodDispatcher is found for interface id '-{xxxxxxxxxx}'".

因此,要解决您的问题,请确保在要调用的外部应用程序中引用您要调用的应用程序中的同一程序集.

So, to fix your problem, make sure you reference the same assembly that is in the application you want to call in the external application that is calling.

这篇关于从集群中的其他应用程序调用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆