✨
RPC服务框架
  • RPC服务框架
  • Dubbo
    • Dubbo简介
    • Dubbo技术原理
    • Dubbo配置使用
    • Dubbo控制台
    • 测验
  • gRPC
    • gRPC简介
    • gRPC配置及使用
    • grpc架构及原理
    • 测验
  • Protocol Buffers
    • Protocol Buffers简介
  • Thrift
    • Thrift简介
    • Thrift环境搭建
    • Thrift项目实现
    • Thrift架构及原理
    • Thrift类型系统
    • 待整理
    • 测验
  • spring cloud
    • Spring Cloud简介
Powered by GitBook
On this page
  • Maven工程结构
  • service代码实现
  • server代码实现
  • client代码实现
  • 完整代码

Was this helpful?

  1. Thrift

Thrift项目实现

参考 https://my.oschina.net/wangmengjun/blog/910817

Maven工程结构

一共四个模块

thrift-rpc-interface

用来存放上一节thrift文件生成的代码

thrift-rpc-service

用来实现接口

thrift-rpc-server

简单的服务器

thrift-rpc-client

简单的客户端

service代码实现

package top.lilixin.thrift.service;

import org.apache.thrift.TException;

import java.util.logging.Logger;

/**
 * @Description TODO
 * @Author lilixin
 * @Date 2021/1/20 11:37 上午
 **/
public class GreetingServiceImpl implements GreetingService.Iface{
    private static final Logger logger =  Logger.getLogger(GreetingServiceImpl.class.getName());

    public String sayHello(String name) throws TException {
        logger.info(String.format("welcome to my world! name = {%s}", name));

        return "Hello, " + name;
    }
}

server代码实现

package top.lilixin.thrift.server;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
import top.lilixin.thrift.service.GreetingService;
import top.lilixin.thrift.service.GreetingServiceImpl;

import java.util.logging.Logger;

/**
 * @Description TODO
 * @Author lilixin
 * @Date 2021/1/20 11:42 上午
 **/
public class GreetingServer {
    private static final Logger logger = Logger.getLogger(GreetingServer.class.getName());

    public static void main(String[] args) {
        try {
            TServerSocket serverTransport = new TServerSocket(9999);
            TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory();

            /**
             * 关联处理器与GreetingService服务实现
             */
            GreetingService.Processor processor = new GreetingService.Processor(new GreetingServiceImpl());

            TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport);
            serverArgs.processor(processor);
            serverArgs.protocolFactory(factory);

            TServer server  = new TThreadPoolServer(serverArgs);
            logger.info("Start server on port 9999...");
            server.serve();

        } catch (TTransportException e) {
            e.printStackTrace();
        }

    }
}

client代码实现

package top.lilixin.thrift.client;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import top.lilixin.thrift.service.GreetingService;

import java.util.logging.Logger;

/**
 * @Description TODO
 * @Author lilixin
 * @Date 2021/1/20 11:48 上午
 **/
public class GreetingClient {
    private static final Logger logger = Logger.getLogger(GreetingClient.class.getName());

    public static void main(String[] args) {
        try {
            TTransport transport  = new TSocket("127.0.0.1",9999);
            transport.open();

            TBinaryProtocol tBinaryProtocol = new TBinaryProtocol(transport);

            GreetingService.Client client = new GreetingService.Client(tBinaryProtocol);

            String name = "lilixin";
            logger.info("client 请求参数name="+name);
            String result = client.sayHello(name);
            logger.info("server 返回结果result="+result);
            transport.close();
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        }

    }
}

完整代码

PreviousThrift环境搭建NextThrift架构及原理

Last updated 4 years ago

Was this helpful?

https://github.com/lilixin/learning-thrift/tree/main