syntax = "proto3"; package com.example; service ExampleService { rpc SayHello(HelloRequest) returns (HelloResponse) {} } message HelloRequest { string name = 1; } message HelloResponse { string message = 1; } import com.example.ExampleService; public class ExampleServiceImpl implements ExampleService { @Override public HelloResponse SayHello(HelloRequest request) { String message = "Hello, " + request.getName() + "!"; HelloResponse response = HelloResponse.newBuilder().setMessage(message).build(); return response; } } public class ExampleServer { public static void main(String[] args) { String zkAddr = "localhost:2181"; RpcServer.registerService(new ExampleServiceImpl()); RpcServer.start(zkAddr); } } import com.example.ExampleService; public class ExampleClient { public static void main(String[] args) { String zkAddr = "localhost:2181"; ExampleService exampleService = RpcClient.getStub(ExampleService.class, zkAddr); HelloRequest request = HelloRequest.newBuilder().setName("World").build(); HelloResponse response = exampleService.SayHello(request); System.out.println(response.getMessage()); } } zkAddr=localhost:2181


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