java访问go,Java+Go+Submodule+Grpc 调用示例

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

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

技术说明

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 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

启动服务端同理

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值