weibocom / motan

A cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services.
Other
5.9k stars 1.78k forks source link

motan能否以client的形式运行于Android环境 #484

Closed sjy1991 closed 7 years ago

sjy1991 commented 7 years ago

后台要我们用motan框架来配合后端那边.虽然我也不知道为什么要这么做,他说手机微博也是这样用的. 在Android Studio上以java工程时运行是没有问题的. 但是在Android工程里用硬编码的方式配置测试,在最后一步连接服务器时出错了.状态码503.下面给错误信息和代码

Process: com.example.je.motan, PID: 24963
com.weibo.api.motan.exception.MotanServiceException: error_message: NettyChannel connect to server timeout url: motan://192.168.1.201:8002/com.example.motan_jdbc.weibo.motan.benchmark.BenchmarkService, cost: 1001, result: false, success: false, connected: false, status: 503, error_code: 10001,r=null
at com.weibo.api.motan.transport.netty.NettyChannel.open(NettyChannel.java:155)
at com.weibo.api.motan.transport.netty.NettyChannelFactory.makeObject(NettyChannelFactory.java:53)
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1188)
at com.weibo.api.motan.transport.AbstractPoolClient.borrowObject(AbstractPoolClient.java:75)
at com.weibo.api.motan.transport.netty.NettyClient.request(NettyClient.java:164)
at com.weibo.api.motan.transport.netty.NettyClient.request(NettyClient.java:120)
at com.weibo.api.motan.protocol.rpc.DefaultRpcProtocol$DefaultRpcReferer.doCall(DefaultRpcProtocol.java:171)
at com.weibo.api.motan.rpc.AbstractReferer.call(AbstractReferer.java:64)
at com.weibo.api.motan.cluster.ha.FailfastHaStrategy.call(FailfastHaStrategy.java:38)
at com.weibo.api.motan.cluster.support.ClusterSpi.call(ClusterSpi.java:73)
at com.weibo.api.motan.proxy.RefererInvocationHandler.invoke(RefererInvocationHandler.java:125)
at java.lang.reflect.Proxy.invoke(Proxy.java:397)
at $Proxy4.callService(Unknown Source)
at com.example.je.motan.MotanClient.test(MotanClient.java:74)
at com.example.je.motan.MainActivity$1.run(MainActivity.java:16)
at java.lang.Thread.run(Thread.java:818)

下面是代码

public  void test(){

        //MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
        RefererConfig<BenchmarkService> refererconfig = new RefererConfig<BenchmarkService>();
        // 设置接口及实现类
        refererconfig.setInterface(BenchmarkService.class);

        RegistryConfig zookeeperRegistry = new RegistryConfig();
        zookeeperRegistry.setRegProtocol("zookeeper");
        zookeeperRegistry.setName("my_zookeeper");
        zookeeperRegistry.setAddress("192.168.1.247");
        zookeeperRegistry.setPort(2181);
        refererconfig.setRegistry(zookeeperRegistry);

        // 配置RPC协议
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setId("benchmarkMotan");
        protocol.setName("motan");
        protocol.setDefault(true);
        protocol.setRequestTimeout(12202);
        protocol.setMaxClientConnection(20);
        protocol.setMinClientConnection(4);
        protocol.setHaStrategy("failfast");
        protocol.setLoadbalance("roundrobin");
        refererconfig.setProtocol(protocol);

        BasicRefererInterfaceConfig basicRefererInterfaceConfig=new BasicRefererInterfaceConfig();
        basicRefererInterfaceConfig.setRequestTimeout(12220);
        basicRefererInterfaceConfig.setGroup("motan-benchmark-rpc");
        basicRefererInterfaceConfig.setAccessLog("false");
        basicRefererInterfaceConfig.setShareChannel(true);
        basicRefererInterfaceConfig.setModule("motan-benchmark-rpc");
        basicRefererInterfaceConfig.setApplication("myMotanBenchmark");
        basicRefererInterfaceConfig.setThrowException(true);
        basicRefererInterfaceConfig.setId("motanClientBasicConfig");
        basicRefererInterfaceConfig.setApplication("myMotanBenchmark");
        basicRefererInterfaceConfig.setRegistry(zookeeperRegistry);
        basicRefererInterfaceConfig.setProtocol(protocol);
        basicRefererInterfaceConfig.setRetries(2);
        basicRefererInterfaceConfig.setCheck("true");
        refererconfig.setBasicReferer(basicRefererInterfaceConfig);

        // 配置服务的group以及版本号
        refererconfig.setGroup("motan-benchmark-rpc");
        refererconfig.setModule("motan-benchmark-rpc");
        //motanDemoServiceReferer.setVersion("1.0");
        refererconfig.setRequestTimeout(13000);
        refererconfig.setAccessLog("false");
        refererconfig.setCheck("true");
        refererconfig.setThrowException(true);
        refererconfig.setDirectUrl("192.168.1.201:8002");

        // 使用服务
        BenchmarkService service = refererconfig.getRef();
        String result = service.callService("我提交的参数为这样的"); // 这一步出错了
        System.out.println(result);

    }

我怀疑是Android不允许这样子调用连接网络. So.其实我相问的是motan这个框架可不可以用在Android当中

shiwolang commented 7 years ago

首先netty3x就不能很好的运行在Android上面,我们就是做了一个扩展类似motan中的yar扩展,使用的是json+http的方式