Java + Go + Grpc + submodule 示例

2 篇文章 0 订阅
1 篇文章 0 订阅

# 简单的 Java 调用 Go Grpc 后端示例

通过 submodule 异构语言共用一套 proto 文件, 在 proto 中 import 其他的 proto 文件.

先参考 go 服务后端 [tanjunchen/grpc-test-demo](https://github.com/tanjunchen/grpc-test-demo)

## 技术说明

https://github.com/tanjunchen/grpc-test-demo

go + submodule(共享 git proto 文件库) + grpc

## *.pb.go 文件编译语句

protoc -I. --go_out=plugins=grpc,paths=source_relative:./gen/go/ your/service/v1/your_service.proto

在 go-grpc-proto/prod 终端下执行以下语句生成 prod.pb.go 文件, 其中 go-grpc-proto 是以 submodule 的形式共用第三方 git 库,
prod.proto 引用了 status/status.proto 文件, 故需要 --proto_path=../status/status.proto 编译参数.

protoc -I=. -I=../ --proto_path=../status/status.proto --go_out=plugins=grpc,paths=source_relative:../../src/prod/  prod.proto

在 go-grpc-proto/status 终端下执行以下语句生成 prod.pb.go 文件

protoc -I=.  --go_out=plugins=grpc,paths=source_relative:../../src/status status.proto

java 可以直接执行 proto-controller 下的 protobuf:compile 与 protobuf:compile-custom 命令.

## 目录结构

```
├── client      # go 测试客户端
│   └── main.go
├── go-grpc-proto       # 存放原始 proto 文件
│   ├── prod
│   │   └── prod.proto      # 说明 prod.proto 引用了 status 下的包
│   └── status
│       └── status.proto
├── go.mod      # mod 文件
├── README.md       # readme 文件
├── server      # go 服务器后端
│   └── main.go
├── service     # demo 示例
│   └── test_service.go
└── src     # 生成后的 *.pb.go 文件
    ├── prod
    │   └── prod.pb.go
    └── status
        └── status.pb.go
```

## 使用步骤

### go 到 go

git clone https://github.com/tanjunchen/grpc-test-demo.git
    
git submodule update --init --recursive

go mod tidy

cd server && go run main.go

```
Listen on 0.0.0.0:9999
```

server 端

```
Listen on 0.0.0.0:9999
211
```

client 端

```
prod_stock:211  status:{code:"200"  msg:"success"}
```

### java 到 go

参见 java [tanjunchen/Java-Go-Grpc-Demo](https://github.com/tanjunchen/Java-Go-Grpc-Demo)

启动服务端同理
 
git clone https://github.com/tanjunchen/Java-Go-Grpc-Demo.git
 
git submodule update --init --recursive
 
cd src/main/java/com/ctj/SpringBootGRPCApplication.java

终端响应如下:

```
xxxxxx  INFO 20752 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
xxxxxx  INFO 20752 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1696 ms
xxxxxx  INFO 20752 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
xxxxxx  INFO 20752 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9998 (http) with context path ''
xxxxxx  INFO 20752 --- [           main] com.ctj.SpringBootGRPCApplication        : Started SpringBootGRPCApplication in 2.513 seconds (JVM running for 2.879)
prod_stock: 1000
status {
  code: "200"
  msg: "success"
}

200
success
```

java 推荐使用 https://github.com/yidongnan/grpc-spring-boot-starter 与 grpc 服务提供者通信.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

远方的飞猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值