欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

gRPC在Java中的實(shí)現(xiàn)與應(yīng)用詳解

 更新時(shí)間:2024年06月20日 12:00:45   作者:晴天  
gRPC是由Google開發(fā)的高性能、開源的通用遠(yuǎn)程過程調(diào)用(RPC)框架,本文將詳細(xì)介紹如何在Java中使用gRPC,包括服務(wù)定義、服務(wù)器端實(shí)現(xiàn)、客戶端調(diào)用以及一些高級(jí)特性,我們將通過代碼示例來幫助理解gRPC的工作原理,需要的朋友可以參考下

引言

gRPC是由Google開發(fā)的高性能、開源的通用遠(yuǎn)程過程調(diào)用(RPC)框架,它基于HTTP/2標(biāo)準(zhǔn)設(shè)計(jì),提供了多語(yǔ)言支持,包括Java、C++、Python等。gRPC特別適合微服務(wù)架構(gòu),因?yàn)樗С蛛p向流和流控制,同時(shí)提供了負(fù)載均衡、跟蹤、健康檢查和身份驗(yàn)證等特性。

本文將詳細(xì)介紹如何在Java中使用gRPC,包括服務(wù)定義、服務(wù)器端實(shí)現(xiàn)、客戶端調(diào)用以及一些高級(jí)特性。我們將通過代碼示例來幫助理解gRPC的工作原理。

gRPC基礎(chǔ)

服務(wù)定義

gRPC使用Protocol Buffers(protobuf)作為接口定義語(yǔ)言(IDL)。首先,我們需要定義服務(wù)和消息類型。

// 文件:helloworld.proto
syntax = "proto3";
 
package helloworld;
 
// 定義服務(wù)
service Greeter {
  // 請(qǐng)求和響應(yīng)消息
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}
 
// 請(qǐng)求消息
message HelloRequest {
  string name = 1;
}
 
// 響應(yīng)消息
message HelloReply {
  string message = 1;
}

生成Java代碼

使用protobuf編譯器protoc生成Java代碼:

protoc --proto_path=src --java_out=build/gen src/helloworld.proto

這將生成HelloRequest、HelloReplyGreeterGrpc.java等類。

服務(wù)器端實(shí)現(xiàn)

// 文件:GreeterImpl.java
import io.grpc.stub.StreamObserver;
import helloworld.GreeterGrpc;
import helloworld.HelloRequest;
import helloworld.HelloReply;
 
public class GreeterImpl extends GreeterGrpc.GreeterImplBase {
 
    @Override
    public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
        HelloReply reply = HelloReply.newBuilder().setMessage("Hello, " + req.getName()).build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }
}

服務(wù)器啟動(dòng)代碼:

// 文件:Server.java
import io.grpc.Server;
import io.grpc.ServerBuilder;
 
public class Server {
    private Server server;
 
    private void start() throws IOException {
        server = ServerBuilder.forPort(8080)
                .addService(new GreeterImpl())
                .build()
                .start();
        System.out.println("Server started, listening on 8080");
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.err.println("*** shutting down gRPC server since JVM is shutting down");
                Server.this.stop();
                System.err.println("*** server shut down");
            }
        });
    }
 
    private void stop() {
        if (server != null) {
            server.shutdown();
        }
    }
 
    private void blockUntilShutdown() throws InterruptedException {
        if (server != null) {
            server.awaitTermination();
        }
    }
 
    public static void main(String[] args) throws IOException, InterruptedException {
        final Server server = new Server();
        server.start();
        server.blockUntilShutdown();
    }
}

客戶端調(diào)用

// 文件:Client.java
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import helloworld.GreeterGrpc;
import helloworld.HelloRequest;
import helloworld.HelloReply;
 
public class Client {
    private final ManagedChannel channel;
    private final GreeterGrpc.GreeterBlockingStub blockingStub;
 
    public Client(String host, int port) {
        channel = ManagedChannelBuilder.forAddress(host, port)
                .usePlaintext() // 為了簡(jiǎn)單,使用明文通信
                .build();
        blockingStub = GreeterGrpc.newBlockingStub(channel);
    }
 
    public void greet(String name) {
        HelloRequest request = HelloRequest.newBuilder().setName(name).build();
        HelloReply response = blockingStub.sayHello(request);
        System.out.println("Greeting: " + response.getMessage());
    }
 
    public void shutdown() throws InterruptedException {
        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }
 
    public static void main(String[] args) throws InterruptedException {
        Client client = new Client("localhost", 8080);
        client.greet("world");
        client.shutdown();
    }
}

高級(jí)特性

gRPC還支持雙向流、服務(wù)器端流和客戶端端流等高級(jí)特性。這些特性可以通過定義不同的RPC方法來實(shí)現(xiàn)。

雙向流

rpc SayHelloStream(stream HelloRequest) returns (stream HelloReply);

服務(wù)器和客戶端可以同時(shí)發(fā)送和接收消息,實(shí)現(xiàn)真正的雙向通信。

結(jié)語(yǔ)

gRPC是一個(gè)強(qiáng)大的RPC框架,它通過HTTP/2和protobuf提供了高效、跨語(yǔ)言的服務(wù)調(diào)用。本文通過簡(jiǎn)單的示例介紹了gRPC在Java中的基本使用,包括服務(wù)定義、服務(wù)器實(shí)現(xiàn)和客戶端調(diào)用。希望這些內(nèi)容能幫助你快速上手gRPC,并在實(shí)際項(xiàng)目中發(fā)揮其強(qiáng)大的功能。

以上就是gRPC在Java中的實(shí)現(xiàn)與應(yīng)用詳解的詳細(xì)內(nèi)容,更多關(guān)于Java gRPC使用與實(shí)現(xiàn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 解決lombok 父類和子類builder不兼容的問題

    解決lombok 父類和子類builder不兼容的問題

    這篇文章主要介紹了解決lombok 父類和子類builder不兼容的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Springboot中Instant時(shí)間傳參及序列化詳解

    Springboot中Instant時(shí)間傳參及序列化詳解

    這篇文章主要介紹了Springboot中Instant時(shí)間傳參及序列化詳解,Instant是Java8引入的一個(gè)精度極高的時(shí)間類型,可以精確到納秒,但實(shí)際使用的時(shí)候不需要這么高的精確度,通常到毫秒就可以了,需要的朋友可以參考下
    2023-11-11
  • SpringMVC+Shiro的基本使用及功能介紹

    SpringMVC+Shiro的基本使用及功能介紹

    本文給大家介紹SpringMVC+Shiro的基本使用,Apache?Shiro是Java的一個(gè)安全框架,Shiro本身無(wú)法知道所持有令牌的用戶是否合法,因?yàn)槌隧?xiàng)目的設(shè)計(jì)人員恐怕誰(shuí)都無(wú)法得知,本文只介紹最常見也最重要的一種實(shí)現(xiàn)方式數(shù)據(jù)庫(kù)查詢
    2022-04-04
  • Spring核心容器之Bean創(chuàng)建過程詳解

    Spring核心容器之Bean創(chuàng)建過程詳解

    這篇文章主要介紹了Spring核心容器之Bean創(chuàng)建過程詳解,獲取?Bean?的方法是?getBean,其來自?BeanFactory?繼承的AbstractAutowireCapableBeanFactory?抽象類繼承的AbstractBeanFactory?抽象類中,需要的朋友可以參考下
    2023-11-11
  • 用Java制作用戶登錄界面超詳細(xì)圖文教程

    用Java制作用戶登錄界面超詳細(xì)圖文教程

    很多人學(xué)習(xí)Java的第一個(gè)任務(wù)是使用Java設(shè)計(jì)客戶端登錄界面中,希望我的學(xué)習(xí)方法與總結(jié)能幫助到需要的朋友,這篇文章主要給大家介紹了關(guān)于用Java制作用戶登錄界面的相關(guān)資料,需要的朋友可以參考下
    2024-06-06
  • 淺談mybatis中SQL語(yǔ)句給boolean類型賦值問題

    淺談mybatis中SQL語(yǔ)句給boolean類型賦值問題

    這篇文章主要介紹了淺談mybatis中SQL語(yǔ)句給boolean類型賦值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 在mybatis中使用mapper進(jìn)行if條件判斷

    在mybatis中使用mapper進(jìn)行if條件判斷

    這篇文章主要介紹了在mybatis中使用mapper進(jìn)行if條件判斷,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 一篇文章帶你入門java泛型

    一篇文章帶你入門java泛型

    這篇文章主要介紹了java泛型基礎(chǔ)知識(shí)及通用方法,從以下幾個(gè)方面介紹一下java的泛型: 基礎(chǔ), 泛型關(guān)鍵字, 泛型方法, 泛型類和接口,感興趣的可以了解一下
    2021-08-08
  • java解析xml常用的幾種方式總結(jié)

    java解析xml常用的幾種方式總結(jié)

    這篇文章主要介紹了java解析xml常用的幾種方式總結(jié),有需要的朋友可以參考一下
    2013-11-11
  • Java實(shí)現(xiàn)企業(yè)發(fā)放的獎(jiǎng)金根據(jù)利潤(rùn)提成問題

    Java實(shí)現(xiàn)企業(yè)發(fā)放的獎(jiǎng)金根據(jù)利潤(rùn)提成問題

    這篇文章主要介紹了請(qǐng)利用數(shù)軸來分界,定位。注意定義時(shí)需把獎(jiǎng)金定義成長(zhǎng)整型,需要的朋友可以參考下
    2017-02-02

最新評(píng)論