HttpServer有多个响应者

时间:2014-12-19 17:36:42

标签: java rpc avro httpserver

在我的应用程序(java)中,我需要支持两种类型的RPC: 管理RPC和用户RPC。我正在使用avro来创建这些RPC,目前我正在使用类似于以下代码的每个RPC打开两个HttpServers:

Server serverAdmins = new HttpServer(new ReflectResponder(AdministrationRPC.class, arpcImpl), adminRpcPort);
Server serverUsers = new HttpServer(new ReflectResponder(UsersRPC.class, urpcImpl), usersRpcPort);
...

这很有效,但对我来说这看起来很浪费 - 我正在寻找一种方法,在同一个端口上使用一个带有两个端口或两个url的http服务器(这些选项中的任何一个对我都有好处),类似于:

Server server = new new HttpServer(new ReflectResponder(AdministrationRPC.class, arpcImpl), adminRpcPort);
server.addResponder(new ReflectResponder(UsersRPC.class, urpcImpl), usersRpcPort);
...

addResponder方法当然不存在,唯一看起来相似的方法是addConnector方法 - 但即使经过彻底的谷歌搜索,我也找不到如何将它应用到我的需要..

有没有办法在同一个http服务器上启动两个avro响应程序?

1 个答案:

答案 0 :(得分:0)

我设法通过编写一个允许我想要的简单类来解决这个问题,请参阅下面的代码。

import java.io.IOException;
import java.net.URL;
import org.apache.avro.ipc.HttpTransceiver;
import org.apache.avro.ipc.Responder;
import org.apache.avro.ipc.ResponderServlet;
import org.apache.avro.ipc.reflect.ReflectRequestor;
import org.apache.avro.ipc.reflect.ReflectResponder;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

/**
 *
 * @author bennyl
 */
public class MultiResponderHttpServer {

    private final Context context;
    private final Server server;
    private final int port;

    public MultiResponderHttpServer(int port) {
        this.port = port;
        server = new Server(port);
        context = new Context(server, "/", Context.SESSIONS);
    }

    public void addResponder(String baseUri, Responder responder) throws IOException {
        ResponderServlet servlet = new ResponderServlet(responder);
        ServletHolder holder = new ServletHolder(servlet);
        context.addServlet(holder, baseUri);
    }

    public int getPort() {
        return port;
    }

    public void close() throws Exception {
        server.stop();
    }

    public void start() throws Exception {
        server.start();
    }

    public void join() throws InterruptedException {
        server.join();
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        MultiResponderHttpServer server = new MultiResponderHttpServer(8888);
        server.addResponder("/test_a/*", new ReflectResponder(TestProtocol.class,
                (TestProtocol) why -> "a received a message: '" + why + "'"));
        server.addResponder("/test_b/*", new ReflectResponder(TestProtocol.class,
                (TestProtocol) why -> "b received a message: '" + why + "'"));

        server.start();

        HttpTransceiver atrans = new HttpTransceiver(new URL("http://localhost:" + server.getPort() + "/test_a/"));
        HttpTransceiver btrans = new HttpTransceiver(new URL("http://localhost:" + server.getPort() + "/test_b/"));

        System.out.println(ReflectRequestor.getClient(TestProtocol.class, atrans).go("message to a"));
        System.out.println(ReflectRequestor.getClient(TestProtocol.class, btrans).go("message to b"));

        server.close();
        server.join();
    }

    public interface TestProtocol {

        String go(String why);
    }
}