In the previous aritile, we have created the schema. In this article, I will introduce how to generate the stub and server code.
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.
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.
~/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.
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.
mvn package
Start your service.
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)
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有