I config gradle and maven setting
- add default setting like
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.bsj.models";
message Die {
int32 value = 1;
}
message Player {
string name = 1;
int32 position = 2;
}
message GameState {
repeated Player player = 1;
}
service GameService {
rpc roll(stream Die) returns (stream GameState);
}
- Generate Proto
./gradlew clean
./gradlew generateProto
- You can Check generated File
- Implementation proto file like
public class GameService extends GameServiceGrpc.GameServiceImplBase {
@Override
public StreamObserver<Die> roll(StreamObserver<GameState> responseObserver) {
Player client = Player.newBuilder()
.setName("client")
.setPosition(0)
.build();
Player server = Player.newBuilder()
.setName("server")
.setPosition(0)
.build();
return new DieStreamingRequest(client, server, responseObserver);
}
}
- Start Server
public class GrpcServer {
public static void main(String[] args) throws IOException, InterruptedException {
Server server = ServerBuilder.forPort(6565)
.addService(new GameService())
.build();
server.start();
server.awaitTermination();
}
}