目录
  • 1. gRPC介绍
  • 2. 核心概念
  • 3. gRPC Java入门示例
    • 3.1 maven依赖
    • 3.2 定义proto
    • 3.3 生成代码
    • 3.4 gRPC Server端编码
    • 3.5 gRPC Client端编码
    • 3.6 启动测试

1. gRPC介绍

随着云原生时代的到来:

  • K8s作为事实上的标准Pass底座
  • Istio作为未来的微服务框架(Service Mesh)
  • Go作为流行的云原生语言(K8s、Istio)
  • Istio对Http、gRPC的原生支持
  • 以及最近的Dubbo3 Triple协议全面兼容gRPC

是时候注意到gRPC这个关键词了,
gRPC起源于Google的微服务RPC框架Stubby,后在2015年3月由Google开源为当前的gRPC

  • 目前支持11种程序开发语言(跨语言)
  • 使用proto作为IDL接口定义语言(即通过proto定义接口及数据,官方推荐proto3)
  • 基于HTTP2实现(原生支持Http2 双向流通信)
  • 支持插件式的auth, tracing, load balancing, health checking

gRPC通过Protobuf(官方推荐proto3)定义RPC服务的相关接口:

  • 服务
  • 方法
  • 参数类型
  • 返回结果类型

使用proto3定义gRPC服务示例(GreeterService.proto):

// RPC服务定义
service HelloService {
  //RPC方法定义
  rpc SayHello (HelloRequest) returns (HelloResponse);
}

// 请求参数定义
message HelloRequest {
  string name = 1;
}

//响应结果定义
message HelloReply {
  string message = 1;
}

然后使用protoc(需安装gRPC插件)或者 后文提到的maven插件 根据预先定义的*.proto文件生成:

  • RPC Client端代码(Stub) - 用于客户端调用
  • RPC Server端代码 - 服务端需实现接口逻辑,并且启动gRPC server
  • 参数、返回结果的相关Protobuf对象
  • 支持Synchronous vs. asynchronous两种模式

gRPC支持4种方法类型

  • Unary RPCs - 一元的RPC,单独的request和response

rpc SayHello(HelloRequest) returns (HelloResponse);

  • Server streaming RPCs - 服务端流RPC,仅发送一个request,然后由Server端返回response流(源源不断的response)直到再无response,由gRPC保证response消息顺序。

rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);

  • Client streaming RPCs - 客户端流RPC,Client端发送request消息流直到再无请request,然后Server端仅返回一个response消息,由gRPC保证request消息顺序

rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);

  • Bidirectional streaming RPCs - 双向流RPC,双向的读写(request, response)流,且request和response流可各自独立保持消息顺序,例如Server端在接受全部request后才统一发送response,或者接到一条request后就发送response,又或者其他任意组合。

rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);


gRPC目前支持的语言见下表:

Language OS Compilers / SDK
C/C++ Linux, Mac GCC 4.9+, Clang 3.4+
C/C++ Windows 7+ Visual Studio 2015+
C# Linux, Mac .NET Core, Mono 4+
C# Windows 7+ .NET Core, NET 4.5+
Dart Windows, Linux, Mac Dart 2.12+
Go Windows, Linux, Mac Go 1.13+
Java Windows, Linux, Mac JDK 8 recommended (Jelly Bean+ for Android)
Kotlin Windows, Linux, Mac Kotlin 1.3+
Node.js Windows, Linux, Mac Node v8+
Objective-C macOS 10.10+, iOS 9.0+ Xcode 7.2+
PHP Linux, Mac PHP 7.0+
Python Windows, Linux, Mac Python 3.5+
Ruby Windows, Linux, Mac Ruby 2.3+

proto3支持的field类型见下表:

.proto Type Notes C++ Type Java/Kotlin Type[1] Python Type[3] Go Type Ruby Type C# Type PHP Type Dart Type
double double double float float64 Float double float double
float float float float float32 Float float float double
int32 Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. int32 int int int32 Fixnum or Bignum (as required) int integer int
int64 Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. int64 long int/long[4] int64 Bignum long integer/string[6] Int64
uint32 Uses variable-length encoding. uint32 int[2] int/long[4] uint32 Fixnum or Bignum (as required) uint integer int
uint64 Uses variable-length encoding. uint64 long[2] int/long[4] uint64 Bignum ulong integer/string[6] Int64
sint32 Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. int32 int int int32 Fixnum or Bignum (as required) int integer int
sint64 Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. int64 long int/long[4] int64 Bignum long integer/string[6] Int64
fixed32 Always four bytes. More efficient than uint32 if values are often greater than 228. uint32 int[2] int/long[4] uint32 Fixnum or Bignum (as required) uint integer int
fixed64 Always eight bytes. More efficient than uint64 if values are often greater than 256. uint64 long[2] int/long[4] uint64 Bignum ulong integer/string[6] Int64
sfixed32 Always four bytes. int32 int int int32 Fixnum or Bignum (as required) int integer int
sfixed64 Always eight bytes. int64 long int/long[4] int64 Bignum long integer/string[6] Int64
bool bool boolean bool bool TrueClass/FalseClass bool boolean bool
string A string must always contain UTF-8 encoded or 7-bit ASCII text, and cannot be longer than 232. string String str/unicode[5] string String (UTF-8) string string String
bytes May contain any arbitrary sequence of bytes no longer than 232. string ByteString str (Python 2)bytes (Python 3) []byte String (ASCII-8BIT) ByteString string List

2. 核心概念

RPC(Remote Procedure Call)
远程过程调用,客户端就像调用本地方法一样调用远程服务,例如通过接口定义进行调用。

IDL(Interface Definition Language)
接口定义语言,定义服务:

  • 服务
  • 方法
  • 参数类型
  • 返回结果类型

Protobuf(protocol buffers)
一种结构化数据的系列化方法,
在gRPC中可用于服务接口及方法的参数和返回值,
亦可用于网络编程中的数据通信。

Stub
特定语言的Client端,用于调用Server端服务,与Server端具有相同方法定义

在这里插入图片描述


3. gRPC Java入门示例

结合官方提供的HelloWorld入门示例,并丰富gRPC方法类型示例,
按照如下步骤创建入门示例grpc-demo,源码可参见:https://gitee.com/luoex/grpc-demo.git

3.1 maven依赖

gRPC运行依赖:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <grpc.version>1.44.0</grpc.version>
    <protobuf.version>3.19.2</protobuf.version>
    <protoc.version>3.19.2</protoc.version>
    <gson.version>2.8.9</gson.version>
</properties>

<!-- gRPC公共依赖管理 -->
<dependencyManagement>
    <dependencies>
        <!-- gRPC bom -->
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-bom</artifactId>
            <version>${grpc.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- protobuf依赖 -->
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java-util</artifactId>
            <version>${protobuf.version}</version>
        </dependency>
        <!-- prevent downgrade via protobuf-java-util -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- gRPC依赖 -->
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-netty-shaded</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-protobuf</artifactId>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-stub</artifactId>
    </dependency>

    <!-- protobuf依赖 -->
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java-util</artifactId>
    </dependency>
    <!-- prevent downgrade via protobuf-java-util -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
    </dependency>
</dependencies>


gRPC生成代码插件:

<build>
    <extensions>
        <!-- 兼容eclipse和netbeans中protobuf代码生成插件-->
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.6.2</version>
        </extension>
    </extensions>
    <plugins>
        <!-- grpc代码生成插件 -->
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.6.1</version>
            <configuration>
                <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
                <pluginId>grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.4.1</version>
            <executions>
                <execution>
                    <id>enforce</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <requireUpperBoundDeps/>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

使用如上protobuf-maven-plugin插件定义,即可:

  • 在执行mvn compile时,
  • 自动扫描代码目中src/main/proto下的proto文件,
  • 然后自动生成gRPC相关代码到target/generated-sources/protobuf目录下。

3.2 定义proto

通过proto3定义gRPC服务,覆盖之前提到的几种方法类型:

方法类型 方法名
Unary sayHello
Server Streaming sayHelloServerStream
Client Streaming sayHelloClientStream
BiDirection Streaming sayHelloBiStream

具体proto文件src/main/proto/hello.proto定义如下:

/*
* HelloWorld入门示例
*/

//使用proto3语法
syntax = "proto3";

//proto包名
package hello;
//生成多个Java文件
option java_multiple_files = true;
//指定Java包名
option java_package = "com.luo.demo.grpc.hello";
//指定Java输出类名
option java_outer_classname = "HelloProto";

//gRPC服务定义
service Hello {
  //gRPC服务方法定义 - Unary
  rpc sayHello (HelloRequest) returns (HelloReply) {}

  //gRPC服务方法定义 - Server Streaming - 服务端流
  rpc sayHelloServerStream (HelloRequest) returns (stream HelloReply) {}

  //gRPC服务方法定义 - Client Streaming - 客户端流
  rpc sayHelloClientStream (stream HelloRequest) returns (HelloReply) {}

  //gRPC服务方法定义 - BiDirection Streaming - 双向流
  rpc sayHelloBiStream (stream HelloRequest) returns (stream HelloReply) {}
}

//请求参数定义
message HelloRequest {
  string name = 1;
}

//响应结果定义
message HelloReply {
  string message = 1;
}


3.3 生成代码

执行mvn compile后,会自动根据src/main/proto/hello.proto生成相关代码如下图:
在这里插入图片描述

将绿框中的代码(即grpc-java和java目录下)拷贝到程序中代码对应的包目录,如下图:
在这里插入图片描述
其中HelloGrpc即为gRPC服务的代码定义,其中包括:

  • HelloGrpc.HelloImplBase - 服务端继承实现该类,对应具体的服务逻辑
  • HelloGrpc.newBlockingSub, HelloGrpc.newStub - 用于客户端生成stub,即调用端

其他HelloRequest、HelloReply等即为具体的参数、结果的protobuf相关代码(提供Builder模式用于快速构建对象)。

3.4 gRPC Server端编码

首先Server端需要实现具体的服务逻辑,即继承实现HelloGrpc.HelloImplBase类,
然后启动gRPC Server,并注册服务端实现类。

对应不同的gRPC方法类型,具体HelloGrpcImpl实现代码如下:

import com.luo.demo.grpc.hello.HelloGrpc;
import com.luo.demo.grpc.hello.HelloReply;
import com.luo.demo.grpc.hello.HelloRequest;
import io.grpc.stub.StreamObserver;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Hello gRPC服务 - 实现类
 *
 * @author luohq
 * @date 2022-02-06 13:46
 */
public class HelloGrpcImpl extends HelloGrpc.HelloImplBase {

    @Override
    public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
        System.out.printf("[sayHello]recv: %s", request);
        //构造返回结果
        HelloReply helloReply = HelloReply.newBuilder()
                .setMessage("Hello " + request.getName())
                .build();
        System.out.printf("[sayHello]resp: %s", helloReply);
        //输出响应
        responseObserver.onNext(helloReply);
        //结束响应
        System.out.println("[sayHello]resp completed!");
        responseObserver.onCompleted();
    }

    @Override
    public void sayHelloServerStream(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
        System.out.printf("[sayHelloServerStream]recv: %s", request);
        //服务端输出响应流(多次onNext输出响应结果)
        IntStream.range(0, 5).forEach(index -> {
            //构造返回结果
            HelloReply helloReply = HelloReply.newBuilder()
                    .setMessage(String.format("Hello_%d %s", index, request.getName()))
                    .build();
            System.out.printf("[sayHelloServerStream]resp: %s", helloReply);
            //输出响应
            responseObserver.onNext(helloReply);
        });

        //结束响应
        System.out.println("[sayHelloServerStream]resp completed!");
        responseObserver.onCompleted();
    }

    @Override
    public StreamObserver<HelloRequest> sayHelloClientStream(StreamObserver<HelloReply> responseObserver) {
        //实现StreamObserver接受客户端流
        return new StreamObserver<HelloRequest>() {
            //name列表
            private List<String> nameList = new ArrayList<>();

            @Override
            public void onNext(HelloRequest request) {
                //接受请求
                nameList.add(request.getName());
                System.out.printf("[sayHelloClientStream]recv_%d: %s\n", nameList.size(), request.getName());
            }

            @Override
            public void onError(Throwable t) {
                //处理错误
                System.err.println("[sayHelloClientStream]recv error!");
                t.printStackTrace();
            }

            @Override
            public void onCompleted() {
                //构造返回结果
                String nameListStr = nameList.stream().collect(Collectors.joining(","));
                HelloReply helloReply = HelloReply.newBuilder().setMessage(String.format("Hello %s", nameListStr)).build();
                System.out.printf("[sayHelloClientStream]resp: %s", helloReply);
                //输出响应
                responseObserver.onNext(helloReply);
                System.out.println("[sayHelloClientStream]resp completed!");
                //结束响应
                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public StreamObserver<HelloRequest> sayHelloBiStream(StreamObserver<HelloReply> responseObserver) {
        //实现StreamObserver接受客户端流
        return new StreamObserver<HelloRequest>() {
            @Override
            public void onNext(HelloRequest request) {
                //接受请求
                System.out.printf("[sayHelloBiStream]recv: %s\n", request.getName());
                //构造返回结果
                HelloReply helloReply = HelloReply.newBuilder()
                        .setMessage("Hello " + request.getName())
                        .build();
                System.out.printf("[sayHelloBiStream]resp: %s", helloReply);
                //输出响应
                responseObserver.onNext(helloReply);
            }

            @Override
            public void onError(Throwable t) {
                //处理错误
                System.err.println("[sayHelloBiStream]recv error!");
                t.printStackTrace();
            }

            @Override
            public void onCompleted() {
                System.out.println("[sayHelloBiStream]resp completed!");
                //结束响应
                responseObserver.onCompleted();
            }
        };
    }
}


Server端核心启动代码如下:

//启动gRPC Server
Integer port = 50051;
Server server = ServerBuilder.forPort(port)
         //注册服务端实现类
        .addService(new HelloGrpcImpl())
        .build()
        .start();
System.out.println("gRPC Server started, listening on " + port);

3.5 gRPC Client端编码

客户端即构建连接服务端的channel,然后构建客户端调用stub,

核心代码如下:

//服务端地址
String target = "localhost:50051";
//构建channel
ManagedChannel managedChannel = ManagedChannelBuilder.forTarget(target)
                .usePlaintext()
                .build();
//阻塞Hello客户端(仅支持Unary、Server Stream)
HelloGrpc.HelloBlockingStub helloBlockingStub = HelloGrpc.newBlockingStub(managedChannel);
//非阻塞Hello客户端(全部仅支持Unary、Server Stream、Client Stream、BiDirection Stream)
HelloGrpc.HelloStub helloStub = HelloGrpc.newStub(managedChannel);

客户端的详细构建及调用逻辑实现代码HelloClient内容如下:

import com.luo.demo.grpc.hello.HelloGrpc;
import com.luo.demo.grpc.hello.HelloReply;
import com.luo.demo.grpc.hello.HelloRequest;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.util.Iterator;

/**
 * Hello gRPC服务 - 客户端
 *
 * @author luohq
 * @date 2022-02-06 13:45
 */
public class HelloClient {

    public static void main(String[] args) throws Exception {
        String target = "localhost:50051";

        //初始化client stub
        HelloClient helloClient = new HelloClient();
        helloClient.init(target);

        //调用服务测试
        helloClient.callSayHello();
        helloClient.callSayHelloServerStream();
        helloClient.callSayHelloClientStream();
        helloClient.callSayHelloBiStream();

        //阻塞主线程,等待gRPC客户端异步调用完成
        Thread.sleep(10000);
    }

    /**
     * 阻塞Hello客户端(仅支持Unary、Server Stream)
     */
    private HelloGrpc.HelloBlockingStub helloBlockingStub;
    /**
     * 非阻塞Hello客户端(全部仅支持Unary、Server Stream、Client Stream、BiDirection Stream)
     */
    private HelloGrpc.HelloStub helloStub;

    /**
     * 客户端初始化
     *
     * @param target 服务端连接目标
     */
    public void init(String target) {
        ManagedChannel managedChannel = ManagedChannelBuilder.forTarget(target)
                .usePlaintext()
                .build();
        this.helloBlockingStub = HelloGrpc.newBlockingStub(managedChannel);
        this.helloStub = HelloGrpc.newStub(managedChannel);
    }

    /**
     * 调用sayHello
     */
    public void callSayHello() {
        //构建请求参数
        HelloRequest helloRequest = HelloRequest.newBuilder()
                .setName("luo")
                .build();

        //阻塞API
        HelloReply helloReply = this.helloBlockingStub.sayHello(helloRequest);
        System.out.printf("[callSayHello]blocking resp: %s", helloReply);

        //非阻塞API
        this.helloStub.sayHello(helloRequest, new StreamObserver<HelloReply>() {
            @Override
            public void onNext(HelloReply value) {
                System.out.printf("[callSayHello]resp: %s", value);
            }

            @Override
            public void onError(Throwable t) {
                System.err.println("[callSayHello]error");
            }

            @Override
            public void onCompleted() {
                System.out.println("[callSayHello]complete");
            }
        });
    }

    /**
     * 调用sayHelloServerStream
     */
    public void callSayHelloServerStream() {
        //构建请求参数
        HelloRequest helloRequest = HelloRequest.newBuilder()
                .setName("luo")
                .build();

        //阻塞API
        Iterator<HelloReply> helloReplyIterator = this.helloBlockingStub.sayHelloServerStream(helloRequest);
        while (helloReplyIterator.hasNext()) {
            HelloReply helloReply = helloReplyIterator.next();
            System.out.printf("[callSayHelloServerStream]blocking resp: %s", helloReply);
        }

        //非阻塞API
        this.helloStub.sayHelloServerStream(helloRequest, new StreamObserver<HelloReply>() {
            @Override
            public void onNext(HelloReply value) {
                System.out.printf("[callSayHelloServerStream]resp: %s", value);
            }

            @Override
            public void onError(Throwable t) {
                System.err.println("[callSayHelloServerStream]error");
            }

            @Override
            public void onCompleted() {
                System.out.println("[callSayHelloServerStream]complete");
            }
        });
    }

    /**
     * 调用sayHelloClientStream
     */
    public void callSayHelloClientStream() {
        //仅支持非阻塞API
        StreamObserver<HelloRequest> requestObserver = this.helloStub.sayHelloClientStream(new StreamObserver<HelloReply>() {
            @Override
            public void onNext(HelloReply value) {
                System.out.printf("[callSayHelloClientStream]resp: %s", value);
            }

            @Override
            public void onError(Throwable t) {
                System.err.println("[callSayHelloClientStream]error");
            }

            @Override
            public void onCompleted() {
                System.out.println("[callSayHelloClientStream]complete");
            }
        });

        //发送请求
        requestObserver.onNext(HelloRequest.newBuilder().setName("luo1-c").build());
        //连续发送请求
        requestObserver.onNext(HelloRequest.newBuilder().setName("luo2-c").build());
        //连续发送请求
        requestObserver.onNext(HelloRequest.newBuilder().setName("luo3-c").build());

        //结束发送请求
        requestObserver.onCompleted();

    }

    /**
     * 调用sayHelloBiStream
     */
    public void callSayHelloBiStream() {
        //仅支持非阻塞API
        StreamObserver<HelloRequest> requestObserver = this.helloStub.sayHelloBiStream(new StreamObserver<HelloReply>() {
            @Override
            public void onNext(HelloReply value) {
                System.out.printf("[callSayHelloBiStream]resp: %s", value);
            }

            @Override
            public void onError(Throwable t) {
                System.err.println("[callSayHelloBiStream]error");
            }

            @Override
            public void onCompleted() {
                System.out.println("[callSayHelloBiStream]complete");
            }
        });

        //发送请求
        requestObserver.onNext(HelloRequest.newBuilder().setName("luo1-b").build());
        //连续发送请求
        requestObserver.onNext(HelloRequest.newBuilder().setName("luo2-b").build());
        //连续发送请求
        requestObserver.onNext(HelloRequest.newBuilder().setName("luo3-b").build());

        //结束发送请求
        requestObserver.onCompleted();
    }
}


3.6 启动测试

即先启动Server端HelloServer
然后启动Client端HelloClient
在这里插入图片描述

HelloServer控制台输出如下:

[sayHello]recv: name: "luo"
[sayHello]resp: message: "Hello luo"
[sayHello]resp completed!
[sayHello]recv: name: "luo"
[sayHello]resp: message: "Hello luo"
[sayHello]resp completed!
[sayHelloServerStream]recv: name: "luo"
[sayHelloServerStream]resp: message: "Hello_0 luo"
[sayHelloServerStream]resp: message: "Hello_1 luo"
[sayHelloServerStream]resp: message: "Hello_2 luo"
[sayHelloServerStream]resp: message: "Hello_3 luo"
[sayHelloServerStream]resp: message: "Hello_4 luo"
[sayHelloServerStream]resp completed!
[sayHelloServerStream]recv: name: "luo"
[sayHelloServerStream]resp: message: "Hello_0 luo"
[sayHelloServerStream]resp: message: "Hello_1 luo"
[sayHelloServerStream]resp: message: "Hello_2 luo"
[sayHelloServerStream]resp: message: "Hello_3 luo"
[sayHelloServerStream]resp: message: "Hello_4 luo"
[sayHelloServerStream]resp completed!
[sayHelloClientStream]recv_1: luo1-c
[sayHelloClientStream]recv_2: luo2-c
[sayHelloClientStream]recv_3: luo3-c
[sayHelloBiStream]recv: luo1-b
[sayHelloBiStream]resp: message: "Hello luo1-b"
[sayHelloClientStream]resp: message: "Hello luo1-c,luo2-c,luo3-c"
[sayHelloClientStream]resp completed!
[sayHelloBiStream]recv: luo2-b
[sayHelloBiStream]resp: message: "Hello luo2-b"
[sayHelloBiStream]recv: luo3-b
[sayHelloBiStream]resp: message: "Hello luo3-b"
[sayHelloBiStream]resp completed!

HelloClient控制台输出如下:

[callSayHello]blocking resp: message: "Hello luo"
[callSayHello]resp: message: "Hello luo"
[callSayHello]complete
[callSayHelloServerStream]blocking resp: message: "Hello_0 luo"
[callSayHelloServerStream]blocking resp: message: "Hello_1 luo"
[callSayHelloServerStream]blocking resp: message: "Hello_2 luo"
[callSayHelloServerStream]blocking resp: message: "Hello_3 luo"
[callSayHelloServerStream]blocking resp: message: "Hello_4 luo"
[callSayHelloServerStream]resp: message: "Hello_0 luo"
[callSayHelloServerStream]resp: message: "Hello_1 luo"
[callSayHelloClientStream]resp: message: "Hello luo1-c,luo2-c,luo3-c"
[callSayHelloClientStream]complete
[callSayHelloBiStream]resp: message: "Hello luo1-b"
[callSayHelloServerStream]resp: message: "Hello_2 luo"
[callSayHelloBiStream]resp: message: "Hello luo2-b"
[callSayHelloServerStream]resp: message: "Hello_3 luo"
[callSayHelloServerStream]resp: message: "Hello_4 luo"
[callSayHelloServerStream]complete
[callSayHelloBiStream]resp: message: "Hello luo3-b"
[callSayHelloBiStream]complete


参考:
https://www.grpc.io/docs/
https://www.grpc.io/docs/languages/java/
https://github.com/grpc/grpc-java
https://developers.google.cn/protocol-buffers
https://developers.google.cn/protocol-buffers/docs/proto3

Java开发的就业市场正在经历结构性调整,竞争日益激烈

传统纯业务开发岗位(如仅完成增删改查业务的后端工程师)的需求,特别是入门级岗位,正显著萎缩。随着企业技术需求升级,市场对Java人才的要求已从通用技能转向了更深入的领域经验(如云原生、微服务)或前沿的AI集成能力。这也导致岗位竞争加剧,在一、二线城市,求职者不仅面临技术内卷,还需应对学历与项目经验的高门槛。

大模型为核心的AI领域正展现出前所未有的就业热度与人才红利

2025年,AI相关新发岗位数量同比激增543%,单月增幅最高超过11倍,大模型算法工程师位居热门岗位前列。行业顶尖人才的供需严重失衡,议价能力极强,跳槽薪资涨幅可达30%-50%。值得注意的是,市场并非单纯青睐算法研究员,而是急需能将大模型能力落地于复杂业务系统的工程人才。这使得具备企业级架构思维和复杂系统整合经验的Java工程师,在向“Java+大模型”复合人才转型时拥有独特优势,成为企业竞相争夺的对象,其薪资天花板也远高于传统Java岗位。

在这里插入图片描述

说真的,这两年看着身边一个个搞Java、C++、前端、数据、架构的开始卷大模型,挺唏嘘的。大家最开始都是写接口、搞Spring Boot、连数据库、配Redis,稳稳当当过日子。

结果GPT、DeepSeek火了之后,整条线上的人都开始有点慌了,大家都在想:“我是不是要学大模型,不然这饭碗还能保多久?”

先给出最直接的答案:一定要把现有的技术和大模型结合起来,而不是抛弃你们现有技术!掌握AI能力的Java工程师比纯Java岗要吃香的多。

即使现在裁员、降薪、团队解散的比比皆是……但后续的趋势一定是AI应用落地!大模型方向才是实现职业升级、提升薪资待遇的绝佳机遇!

如何学习AGI大模型?

作为一名热心肠的互联网老兵,我决定把宝贵的AI知识分享给大家。 至于能学习到多少就看你的学习毅力和能力了 。我已将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

因篇幅有限,仅展示部分资料,需要点击下方链接即可前往获取

2025最新版CSDN大礼包:《AGI大模型学习资源包》免费分享**

一、2025最新大模型学习路线

一个明确的学习路线可以帮助新人了解从哪里开始,按照什么顺序学习,以及需要掌握哪些知识点。大模型领域涉及的知识点非常广泛,没有明确的学习路线可能会导致新人感到迷茫,不知道应该专注于哪些内容。

我们把学习路线分成L1到L4四个阶段,一步步带你从入门到进阶,从理论到实战。

L1级别:AI大模型时代的华丽登场

L1阶段:我们会去了解大模型的基础知识,以及大模型在各个行业的应用和分析;学习理解大模型的核心原理,关键技术,以及大模型应用场景;通过理论原理结合多个项目实战,从提示工程基础到提示工程进阶,掌握Prompt提示工程。

L2级别:AI大模型RAG应用开发工程

L2阶段是我们的AI大模型RAG应用开发工程,我们会去学习RAG检索增强生成:包括Naive RAG、Advanced-RAG以及RAG性能评估,还有GraphRAG在内的多个RAG热门项目的分析。

L3级别:大模型Agent应用架构进阶实践

L3阶段:大模型Agent应用架构进阶实现,我们会去学习LangChain、 LIamaIndex框架,也会学习到AutoGPT、 MetaGPT等多Agent系统,打造我们自己的Agent智能体;同时还可以学习到包括Coze、Dify在内的可视化工具的使用。

L4级别:大模型微调与私有化部署

L4阶段:大模型的微调和私有化部署,我们会更加深入的探讨Transformer架构,学习大模型的微调技术,利用DeepSpeed、Lamam Factory等工具快速进行模型微调;并通过Ollama、vLLM等推理部署框架,实现模型的快速部署。

整个大模型学习路线L1主要是对大模型的理论基础、生态以及提示词他的一个学习掌握;而L3 L4更多的是通过项目实战来掌握大模型的应用开发,针对以上大模型的学习路线我们也整理了对应的学习视频教程,和配套的学习资料。

二、大模型经典PDF书籍

书籍和学习文档资料是学习大模型过程中必不可少的,我们精选了一系列深入探讨大模型技术的书籍和学习文档,它们由领域内的顶尖专家撰写,内容全面、深入、详尽,为你学习大模型提供坚实的理论基础(书籍含电子版PDF)

三、大模型视频教程

对于很多自学或者没有基础的同学来说,书籍这些纯文字类的学习教材会觉得比较晦涩难以理解,因此,我们提供了丰富的大模型视频教程,以动态、形象的方式展示技术概念,帮助你更快、更轻松地掌握核心知识

四、大模型项目实战

学以致用 ,当你的理论知识积累到一定程度,就需要通过项目实战,在实际操作中检验和巩固你所学到的知识,同时为你找工作和职业发展打下坚实的基础。

五、大模型面试题

面试不仅是技术的较量,更需要充分的准备。

在你已经掌握了大模型技术之后,就需要开始准备面试,我们将提供精心整理的大模型面试题库,涵盖当前面试中可能遇到的各种技术问题,让你在面试中游刃有余。


因篇幅有限,仅展示部分资料,需要点击下方链接即可前往获取

2025最新版CSDN大礼包:《AGI大模型学习资源包》免费分享

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐