久久精品精选,精品九九视频,www久久只有这里有精品,亚洲熟女乱色综合一区
    分享

    Grpc系列一 第一個hello world 例子

     WindySky 2017-08-01

    1. 概念

    gRPC的定義:

    • 一個高性能、通用的開源RPC框架
    • 主要面向移動應用開發: gRPC提供了一種簡單的方法來精確地定義服務和為iOS、Android和后臺支持服務自動生成可靠性很強的客戶端功能庫。
    • 基于HTTP/2協議標準而設計,基于ProtoBuf(Protocol Buffers)序列化協議開發
    • 支持眾多開發語言

    2. Hello world Demo

    主要流程:
    1. 通過.proto文件定義服務
    2. 通過protocol buffer compiler插件生成客戶端和服務端
    3. 通過grpc API生成客戶端和服務端代碼

    2.1 定義RPC服務 proto

    // 如果使用此注釋,則使用proto3; 否則使用proto2
    syntax = "proto3";
    // 生成類的包名
    option java_package = "com.hry.spring.grpc.simple";
    //生成的數據訪問類的類名,如果沒有指定此值,則生成的類名為proto文件名的駝峰命名方法
    option java_outer_classname = "GreeterEntity";
    option java_multiple_files = true;
    
    service Greeter {
      rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    
    message HelloRequest {
      string name = 1;
    }
    
    message HelloReply {
      string message = 1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.2 生成基礎類

    通過proto里的方法生成基礎類
    通過Protobuf3 的第一個Java demo的步驟生成protobuf3相關代碼,如HelloReply,HelloReplyOrBuilder,HelloRequest,HelloRequestOrBuilder,詳細見github代碼
    生成GreeterGrpc

    a. 配置pom.xml,指定proto文件

    <!-- grpc 依賴jar -->
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-netty</artifactId>
        <version>1.3.0</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-protobuf</artifactId>
        <version>1.3.0</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-stub</artifactId>
        <version>1.3.0</version>
    </dependency>
    
    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.4.1.Final</version>
            </extension>
        </extensions>
    
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
    
            <!-- 根據proto文件生成java -->
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.0</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.3.0:exe:${os.detected.classifier}</pluginArtifact>
                    <protoSourceRoot>src/main/resources/com/hry/spring/grpc/simple/</protoSourceRoot>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    b. 執行pom.xml,在target/generated-sources/protobuf/grpc-Java/com/hry/spring/grpc/simple/ 目錄GreeterGrpc.java
    GreeterGrpc封裝基本的GRPC功能,后續的客戶端和服務端都從這個類引申出來。GreeterGrpc類見gibhub

    2.3 服務端

    服務端代碼

    public class HelloWorldServer {
        private static final Logger logger = LoggerFactory.getLogger(HelloWorldServer.class);
    
        private Server server;
    
        private void start() throws IOException {
            /* The port on which the server should run */
            int port = 50051;
            server = ServerBuilder.forPort(port).addService(new GreeterImpl()).build().start();
            logger.info("Server started, listening on " + port);
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    // Use stderr here since the logger may have been reset by its
                    // JVM shutdown hook.
                    System.err.println("*** shutting down gRPC server since JVM is shutting down");
                    HelloWorldServer.this.stop();
                    System.err.println("*** server shut down");
                }
            });
        }
    
        private void stop() {
            if (server != null) {
                server.shutdown();
            }
        }
    
        /**
         * Await termination on the main thread since the grpc library uses daemon
         * threads.
         */
        private void blockUntilShutdown() throws InterruptedException {
            if (server != null) {
                server.awaitTermination();
            }
        }
    
        /**
         * Main launches the server from the command line.
         */
        public static void main(String[] args) throws IOException, InterruptedException {
            final HelloWorldServer server = new HelloWorldServer();
            server.start();
            server.blockUntilShutdown();
        }
    
        static 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();
            }
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    2.4 客戶端

    客戶端主代碼

    public class HelloWorldClient {
        private static final Logger logger =  LoggerFactory.getLogger(HelloWorldClient.class);
        private final ManagedChannel channel;
        private final GreeterGrpc.GreeterBlockingStub blockingStub;
    
        /**
         * Construct client connecting to HelloWorld server at {@code host:port}.
         */
        public HelloWorldClient(String host, int port) {
            this(ManagedChannelBuilder.forAddress(host, port)
                    // Channels are secure by default (via SSL/TLS). For the example
                    // we disable TLS to avoid
                    // needing certificates.
                    .usePlaintext(true));
        }
    
        /**
         * Construct client for accessing RouteGuide server using the existing
         * channel.
         */
        HelloWorldClient(ManagedChannelBuilder<?> channelBuilder) {
            channel = channelBuilder.build();
            blockingStub = GreeterGrpc.newBlockingStub(channel);
        }
    
        public void shutdown() throws InterruptedException {
            channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
        }
    
        /** Say hello to server. */
        public void greet(String name) {
            logger.info("Will try to greet " + name + " ...");
            HelloRequest request = HelloRequest.newBuilder().setName(name).build();
            HelloReply response;
            try {
                response = blockingStub.sayHello(request);
            } catch (StatusRuntimeException e) {
                logger.error("RPC failed: {0}", e.getStatus());
                return;
            }
            logger.info("Greeting: " + response.getMessage());
        }
    
        public static void main(String[] args) throws Exception {
            HelloWorldClient client = new HelloWorldClient("localhost", 50051);
            try {
                String user = "world";
                if (args.length > 0) {
                    user = args[0]; 
                }
                client.greet(user);
            } finally {
                client.shutdown();
            }
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    2.4 運行

    先運行服務端,再運行客戶端,關鍵日志打印如下:

    21:35:35.208 [main] INFO com.hry.spring.grpc.simple.HelloWorldClient - Will try to greet world ...
    
    21:35:35.847 [main] INFO com.hry.spring.grpc.simple.HelloWorldClient - Greeting: Hello world
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3

    3. 代碼

    見github代碼

      本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發現有害或侵權內容,請點擊一鍵舉報。
      轉藏 分享 獻花(0

      0條評論

      發表

      請遵守用戶 評論公約

      類似文章 更多

      主站蜘蛛池模板: 国产精品无码av不卡| 国产精品中文字幕一区| 亚洲中文字幕在线二页| 久久亚洲精品情侣| 少妇被躁爽到高潮无码人狍大战| 亚洲中文字幕人妻系列| 久久人妻无码一区二区| 少妇熟女久久综合网色欲| 激情 自拍 另类 亚洲| 性一交一乱一伦| 一本一本久久A久久精品综合不卡| 精品亚洲麻豆1区2区3区| 中文字幕国产精品av| 国产亚洲精品国产福APP| 国产AV福利第一精品| 高清一卡二卡三卡四免费| 国产啪视频免费观看视频| 亚洲AV中文无码乱人伦在线视色| 久久精品国产福利一区二区| 久久久久国产精品免费免费搜索| 在线观看成人永久免费网站| 亚洲精品成人片在线观看精品字幕| 手机看片日本在线观看视频| 伊人久久无码大香线蕉综合| 亚洲成AV人片在线观高清| 日韩有码中文字幕av| 久久精品人妻无码一区二区三 | 亚洲人成网站77777在线观看| 国产精品老熟女露脸视频| 99RE8这里有精品热视频| 久久精品国产亚洲AV高清热| 日韩精品一区二区三区影院| 国产av午夜精品福利| 午夜无码片在线观看影院A| 欧美国产综合欧美视频| 精品一卡2卡三卡4卡乱码精品视频 | 精品国产乱码久久久久APP下载| 夜夜影院未满十八勿进| 亚洲欧美高清在线精品一区二区| 永久黄网站色视频免费直播 | 欧美老少配性行为|