前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gRPC rocks build your first gRPC service(part 2)

gRPC rocks build your first gRPC service(part 2)

作者头像
jimmyhzhao
发布2022-04-10 12:04:07
2280
发布2022-04-10 12:04:07
举报
文章被收录于专栏:SkemaloopSkemaloop

In the previous aritile, we have created the schema. In this article, I will introduce how to generate the stub and server code.

Publish Schema

After click the create schema, the protobuf has been pushed to the sandbox repo of my github.

github repo

The path follows the rules we have defined in the previous article. After the schema is published to the GitHub repo, the next step is to generate the stub and server code according to the schema.

Publish Stub

Click the Next Step button to generate the stub code.

generate stub

By default, skemakit supports java snd golang. You can review the code generated. The stub code can be used by both he client and server to serialize and deserialize the messages between server and clients.

golang and java stub

Generate Server Code

Next we can generate the server code boilerplate, which is a java or golang project that you will add your business logic into.

Then you can check your prototol list to download your server boilerplate code.

download server code

Let`s take java as an example.

Download the java server code and unzip to a folder. The code structure is like below.

代码语言:javascript
复制
 ~/Downloads/ cd sample_package_grpc-java_draft 
 ~/Downloads/sample_package_grpc-java_draft/ ls
pom.xml               sample_package       sample_package-client
 ~/Downloads/sample_package_grpc-java_draft/ tree
.
├── pom.xml
├── sample_package
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── example
│   │   │   │           └── server
│   │   │   │               ├── SayHelloService.java
│   │   │   │               ├── SayHiService.java
│   │   │   │               └── ServiceApplication.java
│   │   │   └── resources
│   │   │       └── application.yaml
│   │   └── test
│   │       └── java
│   │           └── README.md
│   └── target
│       └── README.md
└── sample_package-client
  ├── pom.xml
  ├── src
  │   ├── main
  │   │   ├── java
  │   │   │   └── com
  │   │   │       └── example
  │   │   │           └── client
  │   │   │               ├── ClientApplication.java
  │   │   │               ├── controller
  │   │   │               │   └── ClientController.java
  │   │   │               └── service
  │   │   │                   ├── ClientService.java
  │   │   │                   └── impl
  │   │   │                       └── ClientServiceImpl.java
  │   │   └── resources
  │   │       └── application.yaml
  │   └── test
  │       └── java
  │           └── README.md
  └── target
      └── README.md
​
25 directories, 16 files
​
​

Let`s edit the SayHiService.java to respond a request. The service will echo the message I send through the request.

代码语言:javascript
复制
package com.example.server;
​
import com.skemaloop.test.SayHiGrpc;
import com.skemaloop.test.SayHiOuterClass;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
​
@GrpcService
public class SayHiService extends SayHiGrpc.SayHiImplBase {
   @Override
   // SayHello
   public void sayHello(SayHiOuterClass.HelloRequest request,
   StreamObserver<SayHiOuterClass.HelloReply> responseObserver) {
       // PbServiceOuterClass.HelloReply reply = PbServiceOuterClass.HelloReply.newBuilder().setMsg("Msg: Hello," + request.getMsg() + "\n").build();
       SayHiOuterClass.HelloReply reply =SayHiOuterClass.HelloReply.newBuilder().build();
       // add a business logic here
       responseObserver.onNext(reply);
       responseObserver.onCompleted();
  }
​
}
​

Build your project by maven.

代码语言:javascript
复制
mvn package

Start your service.

代码语言:javascript
复制
java -jar sample_package-0.0.1-SNAPSHOT.jar 
​
.   ____         _           __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::               (v2.4.0)
​
2022-04-02 12:48:24.162 INFO 80519 --- [           main] com.example.server.ServiceApplication   : Starting ServiceApplication using Java 11.0.12 on JIMMYHZHAO-MB3 with PID 80519 (/Users/huizhao/Downloads/sample_package_grpc-java_v1.0.1/sample_package/target/sample_package-0.0.1-SNAPSHOT.jar started by huizhao in /Users/huizhao/Downloads/sample_package_grpc-java_v1.0.1/sample_package/target)
2022-04-02 12:48:24.164 INFO 80519 --- [           main] com.example.server.ServiceApplication   : No active profile set, falling back to default profiles: default
2022-04-02 12:48:24.702 INFO 80519 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-04-02 12:48:24.708 INFO 80519 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-04-02 12:48:24.708 INFO 80519 --- [           main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39]
2022-04-02 12:48:24.739 INFO 80519 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-04-02 12:48:24.739 INFO 80519 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 538 ms
2022-04-02 12:48:24.831 INFO 80519 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2022-04-02 12:48:24.940 INFO 80519 --- [           main] g.s.a.GrpcServerFactoryAutoConfiguration : Detected grpc-netty-shaded: Creating ShadedNettyGrpcServerFactory
2022-04-02 12:48:25.018 INFO 80519 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-04-02 12:48:25.075 INFO 80519 --- [           main] n.d.b.g.s.s.AbstractGrpcServerFactory   : Registered gRPC service: sample_module.sample_package.SayHi, bean: sayHiService, class: com.example.server.SayHiService
2022-04-02 12:48:25.076 INFO 80519 --- [           main] n.d.b.g.s.s.AbstractGrpcServerFactory   : Registered gRPC service: grpc.health.v1.Health, bean: grpcHealthService, class: io.grpc.protobuf.services.HealthServiceImpl
2022-04-02 12:48:25.076 INFO 80519 --- [           main] n.d.b.g.s.s.AbstractGrpcServerFactory   : Registered gRPC service: grpc.reflection.v1alpha.ServerReflection, bean: protoReflectionService, class: io.grpc.protobuf.services.ProtoReflectionService
2022-04-02 12:48:25.159 INFO 80519 --- [           main] n.d.b.g.s.s.GrpcServerLifecycle         : gRPC Server started, listening on address: *, port: 9999
2022-04-02 12:48:25.166 INFO 80519 --- [           main] com.example.server.ServiceApplication   : Started ServiceApplication in 1.963 seconds (JVM running for 2.272)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-04-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Publish Schema
  • Publish Stub
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档