项目作者: cooperise

项目描述 :
基于Netty实现的轻量级RPC框架
高级语言: Java
项目地址: git://github.com/cooperise/Netty-RPC.git
创建时间: 2019-09-08T15:07:14Z
项目社区:https://github.com/cooperise/Netty-RPC

开源协议:

下载


Netty-RPC

基于Netty HTTP协议实现的轻量级RPC框架

1 集成说明

1.1 在builde.gradle(Project)下添加仓库地址:

  1. allprojects {
  2. repositories {
  3. maven {
  4. url uri('https://raw.githubusercontent.com/kyle-android/Netty-RPC/master/repo')
  5. }
  6. }
  7. }

1.2 在builde.gradle(Module)下添加依赖:

  1. implementation 'cn.kyle.support:rpc-core:1.0.0'
  2. annotationProcessor 'cn.kyle.support:rpc-compiler:1.0.2'

2 使用说明

2.1 定义RPC接口文件

  1. @RpcService(ip = "127.0.0.1", port = 8094)
  2. public interface UiAutomator {
  3. @RpcMethod
  4. boolean uiKeyClick(@RpcParam int x, @RpcParam int y) throws RemoteException;
  5. @RpcMethod
  6. @RpcReturnType(type = String.class)
  7. String getUserPhone(@RpcParam(type = String.class) String userId) throws RemoteException;
  8. @RpcMethod
  9. @RpcReturnType(type = List.class, generic = String.class)
  10. List<String> getUserPhoneList(@RpcParam(type = List.class, generic = String.class) List<String> userIds) throws RemoteException;
  11. }

2.2 使用编译生成的RPC文件实现服务端

  1. public class UiAutomatorImpl extends UiAutomatorRpc.Stub {
  2. @Override
  3. public boolean uiKeyClick(int x, int y) throws RemoteException {
  4. System.out.println("UiAutomatorImpl execute uiKeyClick");
  5. return true;
  6. }
  7. @Override
  8. public String getUserPhone(String userId) throws RemoteException {
  9. System.out.println("UiAutomatorImpl execute getUserPhone");
  10. return "Kyle";
  11. }
  12. @Override
  13. public List<String> getUserPhoneList(List<String> userIds) throws RemoteException {
  14. System.out.println("UiAutomatorImpl execute getUserPhoneList");
  15. return Arrays.asList("Kyle");
  16. }
  17. }

2.3 启动服务端

  1. NettyService.getDefault().addService(new UiAutomatorImpl()).start(8094);

2.4 客户端发起RPC调用

  1. UiAutomator uiAutomator = NettyRpc.create(UiAutomator.class);
  2. try {
  3. boolean result = uiAutomator.uiKeyClick(2, 3);
  4. } catch (RemoteException e) {
  5. e.printStackTrace();
  6. }