项目作者: kinwyb

项目描述 :
rpcx的原生java端
高级语言: Java
项目地址: git://github.com/kinwyb/jrpcx.git
创建时间: 2019-06-14T08:09:49Z
项目社区:https://github.com/kinwyb/jrpcx

开源协议:

下载


jrpcx

rpcx的原生java端

实现编码

  • msgpack (rpcx默认编码)
  • json

实现功能

  • 基本调用(TCP)
  • 服务注册发现(etcdv2)
  • 服务超时
  • 失败重试
  • 支持元数据(使用方法结果类继承MetaData)
  • 支持spring注解

使用方法

定义服务接口

  1. /**
  2. * 测试接口
  3. *
  4. * @author heldiam
  5. */
  6. public interface Arith {
  7. /**
  8. * 乘法
  9. *
  10. * @param request 请求参数
  11. * @return
  12. */
  13. ArithMulResponse Mul(ArithMulRequest request);
  14. }

请求结构

  1. /**
  2. *
  3. * @author heldiam
  4. */
  5. public class ArithMulRequest {
  6. private Integer A;
  7. private Integer B;
  8. public Integer getA() {
  9. return A;
  10. }
  11. public void setA(Integer A) {
  12. this.A = A;
  13. }
  14. public Integer getB() {
  15. return B;
  16. }
  17. public void setB(Integer B) {
  18. this.B = B;
  19. }
  20. }

返回结果结构

  1. /**
  2. * 返回结果
  3. * @author heldiam
  4. */
  5. public class ArithMulResponse implements Serializable{
  6. private Integer C;
  7. public Integer getC() {
  8. return C;
  9. }
  10. public void setC(Integer C) {
  11. this.C = C;
  12. }
  13. }

服务端

服务路径只取类名不包含包路径

  1. /**
  2. * @author kinwyb
  3. * @date 2019-06-15 09:07
  4. **/
  5. public class ServerMain {
  6. public static void main(String[] args) throws Exception {
  7. BasicConfigurator.configure();
  8. Arith arith = request -> {
  9. ArithMulResponse response = new ArithMulResponse();
  10. response.setC(request.getA() * request.getB());
  11. return response;
  12. };
  13. Service.registerService(arith, Arith.class);
  14. EtcdV2Discovery discovery = new EtcdV2Discovery("127.0.0.1:2379");
  15. discovery.setBasePath("/jrpcx");
  16. Server server = new Server("127.0.0.1:8972", discovery);
  17. new Thread(() -> server.Start()).run();
  18. System.out.println("定时关闭服务");
  19. Thread.sleep(30000);
  20. server.Close();
  21. }
  22. }

客户端

⚠️ 注意:执行是异步的,直接读取结果属性可能为空。必须通过get方法来获取结果.通过get方法获取该方法会一直阻塞到服务端返回结果

  1. /**
  2. * @author kinwyb
  3. * @date 2019-06-14 17:38
  4. **/
  5. public class ClientMain {
  6. public static void main(String[] args) throws Exception {
  7. BasicConfigurator.configure();
  8. EtcdV2Discovery discovery = new EtcdV2Discovery("127.0.0.1:2379");
  9. discovery.setBasePath("/jrpcx");
  10. Client client = Selector.WeightedRountRobin(discovery).buildClient();
  11. Arith arith = client.Proxy(Arith.class);
  12. ArithMulRequest req = new ArithMulRequest();
  13. req.setA(7);
  14. req.setB(8);
  15. for (int i = 0; i < 1000; i++) {
  16. ArithMulResponse rep = arith.Mul(req);
  17. System.out.println("结果:" + rep.getC());
  18. Thread.sleep(1000);
  19. }
  20. client.Close();
  21. }
  22. }

Spring注解支持

详情查看examples/spring

  1. @ComponentScan(value = {"com.heldiam"})
  2. @RpcxDiscovery //开启注册中心(必须)
  3. @RpcxServer //开启服务
  4. //@RpcxClient //开启客户端
  5. public class Main {
  6. public static void main(String[] args) {
  7. BasicConfigurator.configure();
  8. AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
  9. annotationConfigApplicationContext.register(Main.class);
  10. annotationConfigApplicationContext.refresh();
  11. SimpleComponent component = annotationConfigApplicationContext.getBean(SimpleComponent.class);
  12. System.out.println(component.getData());
  13. annotationConfigApplicationContext.close();
  14. }
  15. }