资python - TApplicationException:无效方法名

2024-10-01 15:29:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个在同一个thrift文件中定义的服务,并共享一个端口。我可以使用serviceA中的任何方法,但是每当我试图调用ServiceB的任何方法时,我都会得到异常。 这是我的节俭档案(service-a.thrift):

service ServiceA extends common.CommonService {
    list<i64> getByIds(1: list<i64> ids)
    ...
}

service ServiceB extends common.CommonService {
    list<i64> getByIds(1: list<i64> ids)
    ...
}

注意事项:

  • 我正在使用python客户机
  • Thrift版本0.8.0

有什么想法吗?在


Tags: 文件方法端口ids客户机定义service档案
2条回答

据我所知,如果不将此字段添加到TMessage并重新编译thrift,没有一种直接的方法可以将多个服务绑定到单个端口。如果你想让两个服务使用同一个服务器,你应该重新实现Thrift服务器,这似乎不是一个容易的任务

我们也有这个需求,通过编写一个新的TProcessor实现来解决这个问题,它创建了一个多处理器的映射。唯一的问题是,对于这个实现,你需要确保没有方法名重叠-即不要在不同的服务器上使用像Run()这样的好的泛型名称。很抱歉没有把C转换成Python。。。在

示例类:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Thrift;
using Thrift.Protocol;

/// <summary>
/// Processor that allows for multiple services to run under one roof.  Requires no method name conflicts across services.
/// </summary>
public class MultiplexProcessor : TProcessor {

    public MultiplexProcessor(IEnumerable<TProcessor> processors) {
        ProcessorMap = new Dictionary<string, Tuple<TProcessor, Delegate>>();
        foreach (var processor in processors) {
            var processMap = (IDictionary) processor.GetType().GetField("processMap_", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(processor);
            foreach (string pmk in processMap.Keys) {
                var imp = (Delegate) processMap[pmk];
                try {
                    ProcessorMap.Add(pmk, new Tuple<TProcessor, Delegate>(processor, imp));
                }
                catch (ArgumentException) {
                    throw new ArgumentException(string.Format("Method already exists in process map: {0}", pmk));
                }
            }
        }
    }

    protected readonly Dictionary<string, Tuple<TProcessor, Delegate>> ProcessorMap;

    internal protected Dictionary<string, Tuple<TProcessor, Delegate>> GetProcessorMap() {
        return new Dictionary<string, Tuple<TProcessor, Delegate>>(ProcessorMap);
    }

    public bool Process(TProtocol iprot, TProtocol oprot) {
        try {
            TMessage msg = iprot.ReadMessageBegin();
            Tuple<TProcessor, Delegate> fn;
            ProcessorMap.TryGetValue(msg.Name, out fn);
            if (fn == null) {
                TProtocolUtil.Skip(iprot, TType.Struct);
                iprot.ReadMessageEnd();
                var x = new TApplicationException(TApplicationException.ExceptionType.UnknownMethod, "Invalid method name: '" + msg.Name + "'");
                oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID));
                x.Write(oprot);
                oprot.WriteMessageEnd();
                oprot.Transport.Flush();
                return true;
            }

            Console.WriteLine("Invoking service method {0}.{1}", fn.Item1, fn.Item2);
            fn.Item2.Method.Invoke(fn.Item1, new object[] {msg.SeqID, iprot, oprot});
        }
        catch (IOException) {
            return false;
        }
        return true;
    }
}

用法示例:

^{pr2}$

相关问题 更多 >

    热门问题