如何在Spring Boot中实现gRPC服务器?

huangapple go评论61阅读模式
英文:

How to implement grpc server in spring boot?

问题

如何在Spring Boot中实现gRPC服务器?我有一个演示应用程序的服务器和客户端两个类。以下是这两个类的代码:

GrpcClient:

package com.example.demo;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class GrpcClient {

    public static void main(String[] args) {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
                .usePlaintext()
                .build();

        HelloServiceGrpc.HelloServiceBlockingStub stub
                = HelloServiceGrpc.newBlockingStub(channel);

        HelloResponse helloResponse = stub.hello(HelloRequest.newBuilder()
                .setFirstName("Rafael")
                .setLastName("Fernando")
                .build());

        channel.shutdown();
    }
}

GrpcServer:

package com.example.demo;

import com.example.demo.services.HelloServiceImpl;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import java.io.IOException;

public class GrpcServer {
    public static void main(String[] args) throws IOException, InterruptedException {
        Server server = ServerBuilder
                .forPort(8080)
                .addService(new HelloServiceImpl()).build();

        server.start();
        server.awaitTermination();
    }
}

这两个类加上我的服务可以使它们正常工作。我的问题是如何在Spring Boot中实现gRPC服务器?

谢谢!

英文:

How to implement grpc server in spring boot? I have two classes the server and client of a demo application. below the two classes:

GrpcClient:

package com.example.demo;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class GrpcClient {

    public static void main(String[] args) {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
                .usePlaintext()
                .build();

        HelloServiceGrpc.HelloServiceBlockingStub stub
                = HelloServiceGrpc.newBlockingStub(channel);

        HelloResponse helloResponse = stub.hello(HelloRequest.newBuilder()
                .setFirstName("Rafael")
                .setLastName("Fernando")
                .build());


        channel.shutdown();
    }
}

GrpcServer:

package com.example.demo;

import com.example.demo.services.HelloServiceImpl;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import java.io.IOException;


public class GrpcServer {
    public static void main(String[] args) throws IOException, InterruptedException {
        Server server = ServerBuilder
                .forPort(8080)
                .addService(new HelloServiceImpl()).build();

        server.start();
        server.awaitTermination();

    }
}

The two classes plus my service I get this to work. My question is the following how to implement the grpc server in spring boot?

Thanks!

答案1

得分: 1

你可以使用 grpc-spring-boot-starter 或者 LogNet 的 grpc-spring-boot-starter 库来配置你的服务/客户端实现、拦截器以及与Spring的外观一致性。这两个库是官方 spring-boot-starters 列表 上唯一列出的选项。

如果你不关心 "Spring 的外观一致性",你可以只需将你的服务/存根实现为带有 @Service@Component 注解的类,注册它们和它们的拦截器(如果有的话),然后在你的 @SpringBootApplication 类中启动服务器。

英文:

You can use grpc-spring-boot-starter or LogNet's grpc-spring-boot-starter libraries to configure your service/client implementations, interceptors, and server with Spring's look and feel. These two are the only ones listed on the official spring-boot-starters list.

If you don't care about that "Spring look and feel" you can just implement your services/stubs as classes annotated with @Service and @Component, register them and their interceptors if any, and start the server at boot in your @SpringBootApplication class.

答案2

得分: 1

我已经启动了一个名为grpc-starter的项目。该项目的目标是实现gRPC生态系统与Spring Boot之间更顺畅的集成,它支持Spring Boot 3。主要特点包括:

  • gRPC服务器/客户端的自动配置
  • JSON转码器
  • proto-gen-validate的集成
  • 异常处理
  • 动态刷新

这里有一个简单的示例:

@SpringBootApplication
@EnableGrpcClients("io.grpc")
public class SimpleApp extends SimpleServiceGrpc.SimpleServiceImplBase {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SimpleApp.class)
                .properties("grpc.client.authority=127.0.0.1:9090")
                .run(args);
    }

    @Override
    public void unaryRpc(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) {
        SimpleResponse response = SimpleResponse.newBuilder()
                .setResponseMessage("Hello " + request.getRequestMessage())
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    @Bean
    ApplicationRunner runner(SimpleServiceGrpc.SimpleServiceBlockingStub stub) {
        return args -> {
            SimpleResponse response = stub.unaryRpc(SimpleRequest.newBuilder().setRequestMessage("World!").build());
            System.out.println(response.getResponseMessage());
        };
    }

}

值得一试!

英文:

I've launched a project called grpc-starter. The goal of this project is to enable smoother integration between the gRPC ecosystem and Spring Boot, it embraces Spring Boot 3. The main features include:

  • Autoconfiguration for gRPC server/client
  • JSON transcoder
  • Integration with proto-gen-validate
  • Exception handling
  • Dynamic refreshing

Here's a simple example:

@SpringBootApplication
@EnableGrpcClients(&quot;io.grpc&quot;)
public class SimpleApp extends SimpleServiceGrpc.SimpleServiceImplBase {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SimpleApp.class)
                .properties(&quot;grpc.client.authority=127.0.0.1:9090&quot;)
                .run(args);
    }

    @Override
    public void unaryRpc(SimpleRequest request, StreamObserver&lt;SimpleResponse&gt; responseObserver) {
        SimpleResponse response = SimpleResponse.newBuilder()
                .setResponseMessage(&quot;Hello &quot; + request.getRequestMessage())
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    @Bean
    ApplicationRunner runner(SimpleServiceGrpc.SimpleServiceBlockingStub stub) {
        return args -&gt; {
            SimpleResponse response = stub.unaryRpc(SimpleRequest.newBuilder().setRequestMessage(&quot;World!&quot;).build());
            System.out.println(response.getResponseMessage());
        };
    }

}

Worth a try!

huangapple
  • 本文由 发表于 2023年5月29日 08:35:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定