gRPC初探

资源

[1] gRPC Java Example

关键词

高性能开源双向流式多开发语言支持Apache 2.0

示例

ProtoBuffer定义

protobuf-maven-plugin编译的默认路径

1566788979168.png
syntax = "proto3";

option java_multiple_files = true;
package com.wjg.grpc.helloworld;

message Person {
  string first_name = 1;
  string last_name = 2;
}

message Greeting {
  string message = 1;
}

service HelloWorldService {
  rpc sayHello (Person) returns (Greeting);
}

使用protobuf-maven-plugin生成stub

源:https://github.com/xolstice/protobuf-maven-plugin

grpc-spring-boot-starter@GRpcService使应用内嵌一个gRPC server。

os-maven-plugin:生成平台依赖的工程属性,ProtocolBuffer编译器依赖这些信息。即是说,protobuf-maven-plugin需要获取当前平台使用的正确的编译器。

sprint-boot-maven-plugin:可以构建独立可运行的jar。

pom.xml



  4.0.0

  com.codenotfound
  grpc-java-hello-world
  0.0.1-SNAPSHOT
  jar

  grpc-java-hello-world
  gRPC Java Example
  https://codenotfound.com/grpc-java-example.html

  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.0.RELEASE
     
  

  
    UTF-8
    UTF-8
    1.8
    3.0.0
    1.6.1
    0.6.1
  

  
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      io.github.lognet
      grpc-spring-boot-starter
      ${grpc-spring-boot-starter.version}
    
    
      org.springframework.boot
      spring-boot-starter-test
      test
    
  

  
    
      
        kr.motd.maven
        os-maven-plugin
        ${os-maven-plugin.version}
      
    
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
      
        org.xolstice.maven.plugins
        protobuf-maven-plugin
        ${protobuf-maven-plugin.version}
        
          com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}
          grpc-java
          io.grpc:protoc-gen-grpc-java:1.16.1:exe:${os.detected.classifier}
        
        
          
            
              compile
              compile-custom
            
          
        
      
    
  

mvn compile生成的类

1566788914309.png

server代码示例

GRpcService:自动配置gRPC服务在端口6565上发布。可以使用grpc.port=xxxx进行修改发布端口。

package com.wjg.wjggrpcstart.service;


import com.wjg.grpc.helloworld.Greeting;
import com.wjg.grpc.helloworld.HelloWorldServiceGrpc;
import com.wjg.grpc.helloworld.Person;
import io.grpc.stub.StreamObserver;
import org.lognet.springboot.grpc.GRpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * created by jingang.wu on 2019/8/26
 */
@GRpcService
public class HelloWorldServiceImpl
        extends HelloWorldServiceGrpc.HelloWorldServiceImplBase {

    private static final Logger LOGGER =
            LoggerFactory.getLogger(HelloWorldServiceImpl.class);

    @Override
    public void sayHello(Person request,
                         StreamObserver responseObserver) {
        LOGGER.info("server received {}", request);

        String message = "Hello " + request.getFirstName() + " "
                + request.getLastName() + "!";
        Greeting greeting =
                Greeting.newBuilder().setMessage(message).build();
        LOGGER.info("server responded {}", greeting);

        // 使用onNext返回greeting,调用onCompleted告诉gRPC响应写入完成
        responseObserver.onNext(greeting);
        responseObserver.onCompleted();
    }
}

client示例代码

调用gRPC服务,首先需要创建一个stub。stub的类型有:

  • 同步stub,阻塞等待server响应
  • 异步stub,响应异步返回

gRPC使用http/2传输消息。MessageChannel管理连接,隐藏了复杂性。

建立:

一个应用使用一个channel,并在service stubs中共享。

package com.wjg.wjggrpcstart.service;

import com.wjg.grpc.helloworld.Greeting;
import com.wjg.grpc.helloworld.HelloWorldServiceGrpc;
import com.wjg.grpc.helloworld.Person;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * created by jingang.wu on 2019/8/26
 */
@Component
public class HelloWorldClient {

    private static final Logger LOGGER =
            LoggerFactory.getLogger(HelloWorldClient.class);

    private HelloWorldServiceGrpc.HelloWorldServiceBlockingStub helloWorldServiceBlockingStub;

    @PostConstruct
    private void init() {
        //gRPC默认使用TLS安全连接,需要配置安全组件,这里示例不使用
        ManagedChannel managedChannel = ManagedChannelBuilder
                .forAddress("localhost", 6565).usePlaintext().build();
        
        // 这里使用同步调用
        helloWorldServiceBlockingStub =
                HelloWorldServiceGrpc.newBlockingStub(managedChannel);
        //异步调用HelloWorldServiceGrpc.newFutureStub(managedChannel);
    }

    public String sayHello(String firstName, String lastName) {
        Person person = Person.newBuilder().setFirstName(firstName)
                .setLastName(lastName).build();
        LOGGER.info("client sending {}", person);

        Greeting greeting =
                helloWorldServiceBlockingStub.sayHello(person);
        LOGGER.info("client received {}", greeting);

        return greeting.getMessage();
    }
}

unit test

@RunWith(SpringRunner.class)
@SpringBootTest
public class WjgGrpcStartApplicationTests {

    @Test
    public void contextLoads() {
    }

    @Autowired
    private HelloWorldClient helloWorldClient;

    @Test
    public void testSayHello() {
        assert(helloWorldClient.sayHello("John", "Doe").equals("Hello John Doe!"));
    }
}

你可能感兴趣的:(gRPC初探)