问题
I'm studying Avro RPC and I'm trying to create a simple example to better understand it.
But I'm facing a difficulty: I can't run a server with more than one Responder
, as the NettyServer constructor only allows me to use one:
public NettyServer(Responder responder, InetSocketAddress addr)
So, if I have more than one IDL, like this:
@namespace("foo.bar")
protocol FooProtocol {
void foo();
}
@namespace("foo.bar")
protocol BarProtocol {
void bar();
}
I'm not able to add both to my NettyServer (as in listening to the same port):
object FooProtocolImpl : FooProtocol {
override fun foo(): Void? {return null}
}
object BarProtocolImpl : BarProtocol {
override fun bar(): Void? {return null}
}
val server = NettyServer(SpecificResponder(FooProtocol.PROTOCOL, FooProtocolImpl), InnetSocketAddress(9090))
How to add BarProtocol to my server? How to make this NettyServer useful for both Protocols?
回答1:
A post by Doug Cutting in GrokBase states:
A separate Responder is currently required for each protocol. If HTTP is used, different ResponderServlet's can be configured so that different protocols are run at different URLs. For NettyServer and SaslSocketServer one must run different protocols on different ports.
Note however that one can create a protocol that's the combination of several other protocols and serve that. For example, with Java reflection, if you have Java interface A that's one protocol and Java interface B that's another then you can implement "interface C extends A, B" and serve that protocol. Clients that speak either A, B or C can then connect. A similar effect can be accomplished with Java's specific compiler by importing various client protocols into the server's protocol.
http://avro.apache.org/docs/current/idl.html#imports
So an alternative is to create a protocol
that "implements" all your protocols, such as:
@namespace("foo.bar")
protocol AllProtocols {
import idl "foo.avdl"
import idl "bar.avdl"
}
and creating your class that implements this protocol:
object AllProtocolsImpl : AllProtocols {
override fun foo(): Void? {return null}
override fun bar(): Void? {return null}
}
then creating your server that serves this protocol:
val server = NettyServer(SpecificResponder(AllProtocols.PROTOCOL, AllProtocolsImpl), InetSocketAddress(9090))
Any client requiring Foo or Bar may connect to this server and use its protocol.
来源:https://stackoverflow.com/questions/51165405/avro-rpc-multiple-responders-for-one-nettyserver