grpc生成java代码_Grpc Java 代码生成

在游戏服务器中,我们通常会通过管理后台修改游戏数据,比如发奖励,给某个用户加物品等,免不了要和游戏服务器打交道,因为你的数据在游戏服务器是有缓存的,如果直接修改数据库的话,是不会在游戏中立刻生效的。

所以为了方便gm管理和游戏服务器的交互,采用这种rpc的模式。方便快捷。

一,首先创建一个新的工程

IDE:eclipse4.6.1

Grpc:1.9.0

二,工程创建完成之后,添加资源文件夹proto,用于存放编写的proto文件

12b83e99255a?from=timeline&isappinstalled=0

图1

三,配置pom.xml文件

12b83e99255a?from=timeline&isappinstalled=0

2

12b83e99255a?from=timeline&isappinstalled=0

3

12b83e99255a?from=timeline&isappinstalled=0

4

不知道为什么,简书不让粘xml,所以只能截图了。

创建一个proto文件,把它放在src/main/proto里面

syntax = "proto3";

option java_multiple_files = true;

option java_package = "com.xinyue.grpc.gamemanager";

option java_outer_classname = "GameManagerRpc";

service GameManger {

rpc AddRoomCard (AddRoomCardRequest) returns (AddRoomCardResult) {}

}

message AddRoomCardRequest {

int64 userId = 1;

int32 roomCard =2;

}

message AddRoomCardResult {

bool result = 1;

}

配置完上述的pom.xml之后,在eclipse中执行mvn install,就会在target文件中生成rpc的代码了,把代码复制到src/main/java中就可以了。

12b83e99255a?from=timeline&isappinstalled=0

5

如果生成的过程中报这个错误,不需要管它,这是因为在你的项目的src/main/java中已经存在这些生成的类了。

12b83e99255a?from=timeline&isappinstalled=0

6

  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值