thrift namespace java com.example.xyz service MyService { string getData(1: i32 id) } struct MyData { 1: i32 id, 2: string name } public class MyServiceImpl implements MyService.Iface { @Override public String getData(int id) throws TException { return "Data for id: " + id; } } public class Server { public static void main(String[] args) { try { MyService.Iface serviceImpl = new MyServiceImpl(); MyService.Processor<MyService.Iface> processor = new MyService.Processor<>(serviceImpl); TServerTransport serverTransport = new TServerSocket(9090); TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport) .processor(processor)); System.out.println("Starting the server..."); server.serve(); } catch (Exception e) { e.printStackTrace(); } } } public class Client { public static void main(String[] args) { try { TTransport transport = new TSocket("localhost", 9090); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); MyService.Client client = new MyService.Client(protocol); String result = client.getData(123); System.out.println("Result: " + result); transport.close(); } catch (Exception e) { e.printStackTrace(); } } }


上一篇:
下一篇:
切换中文