Writing REST Services for the gRPC-curious - Johan Brandhorst, Buf
ฝัง
- เผยแพร่เมื่อ 6 ก.พ. 2025
- Writing REST Services for the gRPC-curious - Johan Brandhorst, Buf
Introducing the gRPC-Gateway, which makes it possible to expose a JSON/REST interface while maintaining many of the benefits of using gRPC.
The first 10 or so minutes will be spent laying the technical ground for the talk by talking about JSON/REST, giving a quick intro into gRPC and [protocol buffers](developers.goo..., then introducing the gRPC-Gateway as a way to integrate gRPC into existing architectures with strict JSON/REST interface requirements.
There will then be a live demo showing the power of this workflow by iterating on a simple design. The audience will be able to follow along on their own by cloning the [boilerplate repository](github.com/joh....
Gradually more complicated topics like PATCH requests, middleware, authentication, working with browser clients, cookies, WebSockets and more will be introduced.
How can i support grpc gateway for Java grpc server
would like to know that too, every video i find on this topic is go...
Did you found any solution?
@Sunil Kumar this is what i did
Define the protobuf service definition: First, you need to define your service using the Protocol Buffer (protobuf) format. This defines the structure of your service and the methods it provides.
java
Copy code
syntax = "proto3";
service HelloService {
rpc sayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string message = 1;
}
Generate the Java classes: Use the protobuf compiler to generate the Java classes from the protobuf definition. These classes will include the gRPC service interface and the request and response classes.
Implement the gRPC service: Next, you need to implement the gRPC service by creating a class that implements the generated service interface. This class will define the implementation for the sayHello method.
java
Copy code
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
@Override
public void sayHello(HelloRequest request, StreamObserver responseObserver) {
String greeting = "Hello, " + request.getName();
HelloResponse response = HelloResponse.newBuilder().setMessage(greeting).build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
Start the gRPC server: In order to make the service available, you need to start a gRPC server and bind the service to it.
java
Copy code
public class Server {
public static void main(String[] args) throws IOException, InterruptedException {
int port = 50051;
Server server = ServerBuilder.forPort(port)
.addService(new HelloServiceImpl())
.build()
.start();
System.out.println("Server started, listening on " + port);
server.awaitTermination();
}
}
Implement the gRPC gateway: The gRPC gateway is a reverse proxy that provides a RESTful API for the gRPC service. To implement the gateway, you need to write a class that extends the generated gRPC gateway class and define the REST endpoints for the service.
java
Copy code
public class HelloGateway extends HelloServiceGrpc.HelloServiceVertxImplBase {
@Override
public void sayHello(HelloRequest request, Future future) {
String name = request.getName();
String greeting = "Hello, " + name;
HelloResponse response = HelloResponse.newBuilder().setMessage(greeting).build();
future.complete(response);
}
}
Start the gateway: Finally, you need to start the gateway server to make the REST API available.
java
Copy code
public class GatewayServer {
public static void main(String[] args) {
int port = 8080;
Vertx vertx = Vertx.vertx();
Router router = Router.router(vertx);
Router gateway = Router.router(vertx);
gateway.route("/").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response