All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rpcclient.RpcClient Maven / Gradle / Ivy

There is a newer version: 0.2.2
Show newest version
package com.rpcclient;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.gen.*;
import org.apache.thrift.TException;
import org.apache.thrift.TServiceClient;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class RpcClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(RpcClient.class);

    private TTransport transport;

    private String host = "";

    private int port = 0;

    public RpcClient(String host, int port){
        this.host = host;
        this.port = port;
        this.transport = new TSocket(host, port);
        LOGGER.info("设置RPC接口请求目标地址:Ip={}, Port={}", host, port);
    }

    public  T initClient(Class tClass) throws TTransportException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        this.transport.open();
        LOGGER.info("TTransport打开。");
        TProtocol protocol = new TBinaryProtocol(this.transport);
        Constructor constructor = tClass.getConstructor(org.apache.thrift.protocol.TProtocol.class);
        return constructor.newInstance(protocol);
    }

    public void closeTTransport(){
        this.transport.close();
        String status = this.transport.isOpen() ? "开启" : "关闭";
        LOGGER.info("RpcClient({},{})的TTransport的状态={}", this.host, this.port, status);
    }

    /* 下方main函数为调试使用,并非测试使用 */

    public static void main(String[] args) {
        TTransport transport = new TSocket("10.225.119.87", 9496);
        try {
            transport.open();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
        TProtocol protocol = new TBinaryProtocol(transport);
        UnionAccountService.Client client = new UnionAccountService.Client(protocol);
        LOGGER.info("请求方法==>" + "CreateApp");
        LOGGER.info("通过JSON创建入参对象。");
        String req = "{\n" +
                "    \"AppInfo\": {\n" +
                "        \"AppCnName\": \"中文名\",\n" +
                "        \"AppEnName\": \"English Name\",\n" +
                "        \"AppId\": 10001542,\n" +
                "        \"ProductId\": 348,\n" +
                "        \"SiteId\": 50030393,\n" +
                "        \"UserId\": 45507\n" +
                "    },\n" +
                "    \"Base\": {\n" +
                "        \"Extra\": {\n" +
                "            \"env\": \"\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        LOGGER.info("请求参数==>{}", req);
        CreateAppRequest createAppRequest_old = JSON.parseObject(req, CreateAppRequest.class);
        LOGGER.info(JSON.toJSONString(createAppRequest_old));
        //注:使用fastjson转object,不会调用setAppId这种setter,导致isSetXXX=false。Thrift要求"出现并设置"(如下行)才为真,才会计算hash值。最终"setAppId":false。
        // boolean this_present_AppId = true && this.isSetAppId();

        LOGGER.info("===上面是Fastjson转Object====下嘛是JavaBean Setter===");

        AppInfo appInfo = new AppInfo();
        appInfo.setAppCnName("中文名");
        appInfo.setAppEnName("English Name");
        appInfo.setAppId(10001542L);
        appInfo.setProductId(348L);
        appInfo.setSiteId(50030393L);
        appInfo.setUserId(45507L);

        Base base = new Base();

        CreateAppRequest createAppRequest = new CreateAppRequest();
        createAppRequest.setAppInfo(appInfo);
        createAppRequest.setBase(base);
        LOGGER.info(JSON.toJSONString(createAppRequest));

        CreateAppResponse createAppResponse = null;
        try {
//            createAppResponse = client.CreateApp(createAppRequest_old);
            createAppResponse = client.CreateApp(createAppRequest);
        } catch (TException e) {
            LOGGER.error(e.getMessage(), e);
        }
        assert createAppResponse != null;

        /* 开始解析返回结果 */
        LOGGER.info("返回结果{}.toString()==>为: {}", "createAppResponse",createAppResponse.toString());
        LOGGER.info("返回结果{} Fastjson美化输出==>为: {}", "createAppResponse", JSON.toJSONString(createAppResponse, SerializerFeature.PrettyFormat));

        transport.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy