java 服务接口定义_java – gRPC不为服务生成接口,只为服务类生成接口

在尝试使用gRPC和.proto文件定义服务时,作者发现编译后服务类并没有生成接口,只有消息类实现了接口。他们疑惑如何在服务器中正确实现服务接口。proto文件中定义了一个名为MyService的服务,包含两个RPC方法。Maven配置使用了protobuf-maven-plugin和os-maven-plugin来编译.proto文件。
摘要由CSDN通过智能技术生成

我是gRPC的新手并遇到这个问题:我用rpc服务定义创建了一个.proto.编译后我得到生成的源:所有消息都有一个实现接口的类.但是,服务本身并不实现任何接口 – 它根本就不会生成.这就是我应该在我的服务器中实现的接口.我究竟做错了什么?我很确定gRPC文档没有说明这个问题.

我的.proto服务:

syntax = "proto3";

option java_multiple_files = true;

option java_package = "com.blah.my.rpc.api";

option java_outer_classname = "MyServiceProto";

option objc_class_prefix = "Pb";

package com.blah.my.rpc.api;

service MyService

{

rpc connect(PbEmptyMessage) returns (PbParameterGroup){}

rpc getParams(PbGenList) returns (PbParameterGroup){}

}

message PbEmptyMessage

{

}

message PbGenId

{

string paramName = 1;

string systemName = 2;

string sName = 3;

string sId = 4;

}

message PbParameterGroup

{

bytes sParameters = 2;

fixed64 time = 3;

}

我在maven中的插件定义:

kr.motd.maven

os-maven-plugin

1.4.0.Final

org.xolstice.maven.plugins

protobuf-maven-plugin

0.5.0

com.google.protobuf:protoc:3.0.0-beta-2:exe:${os.detected.classifier}

grpc-java

io.grpc:protoc-gen-grpc-java:0.14.0:exe:${os.detected.classifier}

${basedir}/src/main/resources

${basedir}/target/generated-sources

generate-sources

compile

test-compile

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java + gRPC + grpc-gateway 的实践主要分为以下几个步骤: 1. 定义 proto 文件proto 文件中定义需要调用的服务以及方法,同时指定请求和响应的数据型。例如: ``` syntax = "proto3"; package example; service ExampleService { rpc ExampleMethod (ExampleRequest) returns (ExampleResponse) {} } message ExampleRequest { string example_field = 1; } message ExampleResponse { string example_field = 1; } ``` 2. 使用 protoc 编译 proto 文件 使用 protoc 编译 proto 文件生成 Java 代码。例如: ``` protoc --java_out=./src/main/java ./example.proto ``` 3. 实现 gRPC 服务Java 代码中实现定义的 gRPC 服务,例如: ``` public class ExampleServiceImpl extends ExampleServiceGrpc.ExampleServiceImplBase { @Override public void exampleMethod(ExampleRequest request, StreamObserver<ExampleResponse> responseObserver) { // 实现具体逻辑 ExampleResponse response = ExampleResponse.newBuilder().setExampleField("example").build(); responseObserver.onNext(response); responseObserver.onCompleted(); } } ``` 4. 启动 gRPC 服务器 使用 gRPC 提供的 ServerBuilder 构建 gRPC 服务器,并启动服务器。例如: ``` Server server = ServerBuilder.forPort(8080).addService(new ExampleServiceImpl()).build(); server.start(); ``` 5. 集 grpc-gateway 使用 grpc-gateway 可以将 gRPC 服务转换为 HTTP/JSON API。在 proto 文件中添加以下内容: ``` import "google/api/annotations.proto"; service ExampleService { rpc ExampleMethod (ExampleRequest) returns (ExampleResponse) { option (google.api.http) = { post: "/example" body: "*" }; } } ``` 在 Java 代码中添加以下内容: ``` Server httpServer = ServerBuilder.forPort(8081).addService(new ExampleServiceImpl()).build(); httpServer.start(); String grpcServerUrl = "localhost:8080"; String httpServerUrl = "localhost:8081"; ProxyServerConfig proxyConfig = new ProxyServerConfig(grpcServerUrl, httpServerUrl, "/example"); HttpProxyServer httpProxyServer = new HttpProxyServer(proxyConfig); httpProxyServer.start(); ``` 6. 测试 使用 HTTP/JSON API 调用 gRPC 服务,例如: ``` POST http://localhost:8081/example Content-Type: application/json { "example_field": "example" } ``` 以上就是 Java + gRPC + grpc-gateway 的实践步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值