rpc-service 工程
interface
package com.qzq.lhy.rpc.service;
public interface LoginServiceInterface {
public static final long versionID = 1L;
public String login(String username,String password);
}
interfaceIimpl
package com.qzq.lhy.rpc.service;
public class LoginServiceImpl implements LoginServiceInterface {
public String login(String username, String password) {
return username + password;
}
}
interface service bind
package com.qzq.lhy.rpc.service;
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.Server;
public class ServerStart {
public static void main(String[] args) throws HadoopIllegalArgumentException, IOException {
Configuration conf = new Configuration();
Builder builder = new RPC.Builder(conf);
builder.setInstance(new LoginServiceImpl()).setBindAddress("localhost").setPort(9000).setProtocol(LoginServiceInterface.class);
Server server = builder.build();
server.start();
}
}
rpc-controller
Interface package name 应该保持一致
package com.qzq.lhy.rpc.service;
public interface LoginServiceInterface {
public static final long versionID = 1L;
public String login(String username,String password);
}
Controller
package com.qzq.lhy.rpc.controller;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.RPC;
import com.qzq.lhy.rpc.service.LoginServiceInterface;
public class UserLoginController {
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
LoginServiceInterface loginServiceImpl = RPC.getProxy(LoginServiceInterface.class, 1L, new InetSocketAddress("localhost",9000), conf);
String res = loginServiceImpl.login("1z", "system");
System.out.println(res);
}
}