Hadoop中的RPC应用实例2

1.服务端

接口

public interface LoginServiceInterface {

public static final long versionID=1L;

public String login(String username,String password);

}


实现类

public class LoginServiceImpl implements LoginServiceInterface {

@Override

public String login(String username, String password) {

return username + " logged in successfully!";

}

}


开启服务

import java.io.IOException;
import org.apache.hadoop.HadoopIllegalArgumentException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import org.apache.hadoop.ipc.RPC.Builder;
import org.apache.hadoop.ipc.RPC.Server;


public class Starter {

public static void main(String[] args) throws HadoopIllegalArgumentException, IOException {

Builder builder = new RPC.Builder(new Configuration());

builder.setBindAddress("weekend110").setPort(10000).setProtocol(LoginServiceInterface.class).setInstance(new LoginServiceImpl());

Server server = builder.build();

server.start();

}

}


2. 客户端

接口

public interface LoginServiceInterface {
public static final long versionID=1L;
public String login(String username,String password);
}

客户端controller

import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;

public class LoginController {

public static void main(String[] args) throws Exception {
LoginServiceInterface proxy = RPC.getProxy(LoginServiceInterface.class, 1L, new InetSocketAddress("weekend110", 10000), new Configuration());

String result = proxy.login("mijie", "123456");

System.out.println(result);
}
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页