RPC 的 demo

在 windows 端與 Linux 的通信java

windows :apache

LoginControllerwindows

package cn.itcast.hadoop.rpc;

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("weekend101", 10000), new Configuration());
		
		String result = proxy.login("mijie", "123456");
		
		System.out.println(result);
	}
	
	
}

LoginServiceInterfaceide

package cn.itcast.hadoop.rpc;

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

}

Linux :oop

LoginServiceImplui

package cn.itcast.hadoop.rpc;

public class LoginServiceImpl implements LoginServiceInterface {

	@Override
	public String login(String username, String password) {
		
		return username + " logged in successfully!";
	}

}

LoginServiceInterface.net

package cn.itcast.hadoop.rpc;

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

}

Startercode

package cn.itcast.hadoop.rpc;

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();

	}

}
相關文章
相關標籤/搜索