查看hadoop集群非JAVA进程_hadoop;RPC;调用接口;cmd的jps查看java进程;有main方法的类才能产生进程...

RPC(remote procedure call)不同java进程间的对象方法调用,一方称作服务端,一方称作客户端;被调用的对象的方法执行发生在server端

首先应该编写服务端MyServer,客户端MyClient,操作对象类MyBiz(根据服务端方法参数推测的),操作对象接口MyBizable(根据客户端方法参数推测的)

通过查看源码,一步步向里查看,直到没有return该方法出现

package com.kane.rpc;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.ipc.RPC;

import org.apache.hadoop.ipc.RPC.Server;

public class MyServer {

public static final String SERVER_ADDRESS="localhost";

public static final int SERVER_PORT=12345;

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

/** Construct an RPC server.

* @param instance the instance whose methods will be called实例化中方法会被调用

* @param conf the configuration to use

* @param bindAddress the address to bind on to listen for connection

* @param port the port to listen for connections on

* @param numHandlers the number of method handler threads to run

* @param verbose whether each call should be logged

*/

Server server = RPC.getServer(new MyBiz(), SERVER_ADDRESS, SERVER_PORT, new Configuration());

server.start();

}

}

package com.kane.rpc;

import java.net.InetSocketAddress;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.ipc.RPC;

import org.apache.hadoop.ipc.VersionedProtocol;

public class MyClient {

public static void main(String[] args) throws Exception {

/** Construct a client-side proxy object that implements the named protocol,

* talking to a server at the named address.

* 参数依次:实现协议的业务类(这要求我们的mybiz类实现该协议)版本号,服务端地址,配置 */

//final VersionedProtocol waitForProxy = RPC.waitForProxy(MyBiz.class, 1, new InetSocketAddress(MyServer.SERVER_ADDRESS, MyServer.SERVER_PORT), new Configuration());

//后来测试时报异常说业务类不是一个接口,所以采用一个接口 MyBizable继承 VersionedProtocol ,MyBiz实现 MyBizable

final MyBizable proxy =(MyBizable)RPC.waitForProxy(MyBizable.class,MyBizable.VERSION, new InetSocketAddress(MyServer.SERVER_ADDRESS, MyServer.SERVER_PORT), new Configuration());

final String str = proxy.hello("kane");

System.out.println(str);

RPC.stopProxy(proxy);

}

}

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

package com.kane.rpc;

import org.apache.hadoop.ipc.VersionedProtocol;

public interface MyBizable extends  VersionedProtocol{

public static long VERSION=1;//这里写的版本和client接收的 版本一样

public abstract String hello(String name);

}

package com.kane.rpc;

import java.io.IOException;

import org.apache.hadoop.ipc.VersionedProtocol;

public class MyBiz implements MyBizable{

/* (non-Javadoc)

* @see com.kane.rpc.MyBizable#hello(java.lang.String)

*/

@Override

public String hello(String name) {

return "hello"+name;

}

@Override

public long getProtocolVersion(String protocol, long clientVersion)

throws IOException {

// 这里返回 的版本和client写的版本一样

return MyBizable.VERSION;

}

}

先启动myserver,然后再运行myclient

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值