标签:
最近在学习传智播客吴超老师的Hadoop视频,里面他在讲解RPC通信原理的过程中给了一个RPC的小例子,但是自己编写的过程中遇到一个小错误,整理如下:
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy4.login(Unknown Source)
at cn.edu.nupt.Hadoop.rpc.LoginController.main(LoginController.java:13)
Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.ipc.RpcServerException): Unknown protocol: cn.edu.nupt.Hadoop.rpc.LoginServiceInterface
at org.apache.hadoop.ipc.WritableRpcEngine$Server$WritableRpcInvoker.call(WritableRpcEngine.java:493)
at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:969)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2049)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2045)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1657)
at org.apache.hadoop.ipc.Server$Handler.run(Server.java:2043)
at org.apache.hadoop.ipc.Client.call(Client.java:1475)
at org.apache.hadoop.ipc.Client.call(Client.java:1412)
at org.apache.hadoop.ipc.WritableRpcEngine$Invoker.invoke(WritableRpcEngine.java:243)
... 2 more
导致此类问题的原因主要是由于命名空间的问题(包的命名不一致)。在我的环境中我的linux中的包命名为cn.edu.nupt.hadoop.rpc,而在我的windows工程中我的包的命名格式为cn.edu.nupt.Hadoop.rpc,很清楚的看到我的命名格式不一致。改好后运行成功。因为我的windows中的项目是通过maven创建的,所以不太好修改报名,直接统一改成了cn.edu.nupt.Hadoop.rpc格式。
几点说明:
我的namenode的ip对应的“域名”为master;
采用的端口号监听是:10000
抽象接口(协议):接口
实例:具体实现类
几个重要的类如下:
1、Linux端
(1)接口:LoginServiceInterface
package cn.edu.nupt.Hadoop.rpc; public interface LoginServiceInterface { public static final long versionID=1L; public String login(String username,String passwd); }
(2)实现类:LoginServiceImplement
package cn.edu.nupt.Hadoop.rpc; public class LoginServiceImplement implements LoginServiceInterface{ @Override public String login(String username, String passwd) { // TODO Auto-generated method stub System.out.println("haha"); return username+"Login Successful!"; } }
(3)程序入口:start
package cn.edu.nupt.Hadoop.rpc; import java.io.IOException; import org.apache.hadoop.HadoopIllegalArgumentException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.ipc.RPC; /** * * @author hadoop * */ public class start { public static void main(String[] args) throws HadoopIllegalArgumentException, IOException { RPC.Builder builder = new RPC.Builder(new Configuration()); builder.setBindAddress("master").setPort(10000).setProtocol(LoginServiceInterface.class) .setInstance(new LoginServiceImplement()); org.apache.hadoop.ipc.RPC.Server server = builder.build(); server.start(); } }
2、windows端
(1)抽象类:loginServiceInterface
package cn.edu.nupt.Hadoop.rpc; public interface LoginServiceInterface { public static final long versionID=1l; public String login(String username,String passwd); }
(2)测试类:LoginController
package cn.edu.nupt.Hadoop.rpc; import java.io.IOException; 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 IOException { LoginServiceInterface proxy = RPC.getProxy(LoginServiceInterface.class, 1L, new InetSocketAddress("master", 10000), new Configuration()); String result=proxy.login("Angelababy ", "123456"); System.out.println(result); } }
Hadoop中RPC协议小例子报错java.lang.reflect.UndeclaredThrowableException解决方法
标签:
原文地址:http://www.cnblogs.com/xiangyangzhu/p/5731286.html