gRPC测试

参考文献:

  1. 示例及博客链接
  2. 优先推荐:带pom的简单示例
  3. 官网demo
  4. Intellij IDEA中使用Protobuf的正确姿势

github: https://github.com/whbing/grpc-java/tree/v1.20.0/examples

# 改动点
master分支
将pom中的改为
1.20.0
jdk1.8

目录结构如下。在proto未编译时,在Client或Server端会有类找不到。
gRPC测试_第1张图片

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <artifactId>pro-allartifactId>
        <groupId>cn.whbing.progroupId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <groupId>cn.whbing.rpcgroupId>
    <artifactId>examplesartifactId>
    <packaging>jarpackaging>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <grpc.version>1.20.0grpc.version>
        <protobuf.version>3.10.0protobuf.version>
        <protoc.version>3.10.0protoc.version>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.grpcgroupId>
                <artifactId>grpc-bomartifactId>
                <version>${grpc.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.grpcgroupId>
            <artifactId>grpc-allartifactId>
            <version>${grpc.version}version>
        dependency>
        <dependency>
            <groupId>com.google.protobufgroupId>
            <artifactId>protobuf-javaartifactId>
            <version>${protobuf.version}version>
        dependency>

        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-pool2artifactId>
            <version>2.4.2version>
        dependency>
    dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.mavengroupId>
                <artifactId>os-maven-pluginartifactId>
                <version>1.6.2version>
            extension>
        extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.pluginsgroupId>
                <artifactId>protobuf-maven-pluginartifactId>
                <version>0.6.1version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}protocArtifact>
                    <pluginId>grpc-javapluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}pluginArtifact>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compilegoal>
                            <goal>compile-customgoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-enforcer-pluginartifactId>
                <version>1.4.1version>
                <executions>
                    <execution>
                        <id>enforceid>
                        <goals>
                            <goal>enforcegoal>
                        goals>
                        <configuration>
                            <rules>
                                <requireUpperBoundDeps/>
                            rules>
                        configuration>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

1.定义服务

写.proto文件

// Copyright 2015 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

2.生成客户端和服务端代码

mvn compile
gRPC测试_第2张图片

定义了客户端和服务端所需的传输对象。
如client端:
gRPC测试_第3张图片

你可能感兴趣的:(#,jms,#,RPC)