当前位置:   article > 正文

Hadoop 日常学习bug调试(二)_exception in thread "main" java.lang.runtimeexcept

exception in thread "main" java.lang.runtimeexception: org.apache.hadoop.ipc

RPC的使用

代码如下:

LoginServiceInterface.java
  1. package rpc;
  2. public interface LoginServiceInterface
  3. {
  4. public static final long versionID=1L;
  5. public String login(String userName,String pwd);
  6. }

注意该接口versionID版本号必须设置,否则客户端调用时报错

  1. Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchFieldException: versionID
  2. at org.apache.hadoop.ipc.RPC.getProtocolVersion(RPC.java:179)
  3. at org.apache.hadoop.ipc.WritableRpcEngine$Invocation.<init>(WritableRpcEngine.java:113)
  4. at org.apache.hadoop.ipc.WritableRpcEngine$Invoker.invoke(WritableRpcEngine.java:252)
  5. at com.sun.proxy.$Proxy4.login(Unknown Source)
  6. at rpc.LoginController.main(LoginController.java:18)
  7. Caused by: java.lang.NoSuchFieldException: versionID
  8. at java.lang.Class.getField(Class.java:1703)
  9. at org.apache.hadoop.ipc.RPC.getProtocolVersion(RPC.java:175)
  10. ... 4 more
LoginServiceImpl.java
  1. package rpc;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. public class LoginServiceImpl implements LoginServiceInterface
  5. {
  6. private static final Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);
  7. @Override
  8. public String login(String userName, String pwd)
  9. {
  10. return userName + " logged in successfully!";
  11. }
  12. }

服务端启动 Starter.java

  1. package rpc;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.ipc.RPC;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import java.io.IOException;
  7. public class Starter
  8. {
  9. private static final Logger logger = LoggerFactory.getLogger(Starter.class);
  10. public static void main(String[] args) throws IOException
  11. {
  12. RPC.Builder builder = new RPC.Builder(new Configuration());
  13. builder.setBindAddress("192.168.1.103").setPort(10000).setProtocol(LoginServiceInterface.class).setInstance(new LoginServiceImpl());
  14. RPC.Server server = builder.build();
  15. server.start();
  16. }
  17. }

客户端启动LoginController.java

  1. package rpc;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.ipc.RPC;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import java.io.IOException;
  7. import java.net.InetSocketAddress;
  8. public class LoginController
  9. {
  10. private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
  11. public static void main(String[] args) throws IOException
  12. {
  13. LoginServiceInterface loginServiceInterface = RPC.getProxy(LoginServiceInterface.class, 1L, new InetSocketAddress("192.168.1.103",10000), new Configuration());
  14. String result = loginServiceInterface.login("xuliang", "123");
  15. logger.debug("result: " + result);
  16. }
  17. }

客户端运行结果:

  欢迎大家私信博主,邀你进技术交流群

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/584854
推荐阅读
相关标签
  

闽ICP备14008679号