栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

grpc HelloWorld

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

grpc HelloWorld

grpc HelloWorld
    • 一.创建 maven 项目
      • 版本信息
      • pom.xml
    • 二.HelloWorld 快速上手
      • 1.编写 protocol buffers 定义接口
      • 2.maven 编译项目
      • 3.编写服务端代码
      • 4.编写客户端代码
      • 5.测试

一.创建 maven 项目 版本信息

    1.8
    UTF-8
    1.34.1
    3.12.0
    3.12.0
    1.8
    1.8

pom.xml


    4.0.0

    a.b.c
    grpc-java-learning
    1.0-SNAPSHOT

    
        1.8
        UTF-8
        1.34.1
        3.12.0
        3.12.0
        1.8
        1.8
    
    
        
            
                io.grpc
                grpc-bom
                ${grpc.version}
                pom
                import
            
        
    
    
        
            io.grpc
            grpc-netty-shaded
            runtime
        
        
            io.grpc
            grpc-protobuf
        
        
            io.grpc
            grpc-stub
        
        
            com.google.protobuf
            protobuf-java-util
            ${protobuf.version}
        
    
    
        
            
                kr.motd.maven
                os-maven-plugin
                1.6.2
            
        
        
            
            
                org.xolstice.maven.plugins
                protobuf-maven-plugin
                0.6.1
                
                    com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
                    grpc-java
                    io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
                   
                    src/main/proto
                
                
                    
                        
                            compile
                            compile-custom
                        
                    
                
            
						
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                
            
        
    


二.HelloWorld 快速上手 1.编写 protocol buffers 定义接口

创建.proto 文件 src/main/proto/helloworld.proto,定义服务输入和输出的数据结构

syntax = "proto3";

option java_multiple_files = true;
// 编译生成的 java 包名
option java_package = "com.grpc.examples.helloworld";
// 编译生成的 java 类名
option java_outer_classname = "HelloWorldProto";



// 定义一个服务
service HelloWordServer {
  // 定义两个服务的输入和输出数据结构
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// 请求的数据结构
message HelloRequest {
  string name = 3;
}

// 相应的数据结构
message HelloReply {
  string message = 1;
}

2.maven 编译项目

在target/generated-sources/protobuf/grpc-java/com/grpc/examples/helloworld/HelloWordServerGrpc.java目录下生成com.grpc.examples.helloworld.HelloWordServerGrpc类

3.编写服务端代码

要求实现继承 HelloWordServerGrpc.HelloWordServerImplbase ,重写 sayHello 方法

package grpc.examples.helloworld;

import com.grpc.examples.helloworld.HelloReply;
import com.grpc.examples.helloworld.HelloRequest;
import com.grpc.examples.helloworld.HelloWordServerGrpc;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;


public class HelloWordServer extends HelloWordServerGrpc.HelloWordServerImplbase {

    @Override
    public void sayHello(HelloRequest request, StreamObserver responseObserver) {

        String name = request.getName();
        String message = syaHello(name);

        responseObserver.onNext(HelloReply.newBuilder().setMessage(message).build());
        responseObserver.onCompleted();

    }

    private String syaHello(String name) {
        return "hello " + name + ", I am HelloWordServer.";
    }

    public static void main(String[] args) throws Exception {
        ServerBuilder.forPort(9999)
                .addService(new HelloWordServer())
                .build()
                .start();
        System.out.println("server start at 9999");

        Thread.sleep(9999999);
    }
}

4.编写客户端代码

要求 ip 和 port 和服务端一致

package grpc.examples.helloworld;


import com.grpc.examples.helloworld.HelloReply;
import com.grpc.examples.helloworld.HelloRequest;
import com.grpc.examples.helloworld.HelloWordServerGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class HelloWorldClient {
  HelloWordServerGrpc.HelloWordServerBlockingStub stub;
  ManagedChannel channel;

  public static void main(String[] args) {

    HelloWorldClient client = new HelloWorldClient();

    HelloReply reply = client.stub.sayHello(HelloRequest.newBuilder().setName("张三").build());

    System.out.println(reply.getMessage());
  }

  public HelloWorldClient(){
    channel = ManagedChannelBuilder
        .forAddress("127.0.0.1",9999)
        .usePlaintext()
        .build();
    stub =
        HelloWordServerGrpc.newBlockingStub(channel);
  }
}
5.测试

启动服务端代码,输出server start at 9999

启动客户端代码,输出hello 张三, I am HelloWordServer.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271479.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号