栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

gRPC-client

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

gRPC-client


import com.aaa.service.grpc.*;
import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;


public class SkyClient {
    private static final Logger logger = Logger.getLogger(SkyClient.class.getName());

    private final MetricExportServiceGrpc.MetricExportServiceBlockingStub blockingStub;
    private final MetricExportServiceGrpc.MetricExportServiceStub exportStub;

    
    public SkyClient(Channel channel) {
        // 'channel' here is a Channel, not a ManagedChannel, so it is not this code's responsibility to
        // shut it down.

        // Passing Channels to code makes code easier to test and makes it easier to reuse Channels.
        blockingStub = MetricExportServiceGrpc.newBlockingStub(channel);
        exportStub = MetricExportServiceGrpc.newStub(channel);
    }

    Set subscriptionSet = new HashSet<>();

    
    public void sub() {
        SubscriptionsResp response;
        try {
            response =
                    blockingStub.withDeadlineAfter(10, TimeUnit.SECONDS)
                            .subscription(SubscriptionReq.newBuilder().build());
            response.getMetricNamesList().forEach(subscriptionSet::add);
            logger.log(Level.INFO, "Get exporter subscription list, {}", subscriptionSet);
        } catch (StatusRuntimeException e) {
            logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
            return;
        }
        logger.info("Greeting: " + response.getMetricNamesList());
    }


    public void export() {
        StreamObserver requestObserver = exportStub.export(new StreamObserver() {
            @Override
            public void onNext(ExportResponse exportResponse) {

            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                onNext(ExportResponse.newBuilder().build());
            }
        });
        String entityName = "/order/list";
        String metricName = "endpoint_cpm";
        for (int i = 0; i < 10; i++) {
            ExportMetricValue value = ExportMetricValue.newBuilder()
                    .setMetricName(metricName)
                    .setEntityName(entityName)
                    .setEntityId("1231321")
                    .setLongValue(i)
                    .setTimeBucket(202020201)
                    .build();
            requestObserver.onNext(value);
        }
        requestObserver.onCompleted();

    }


    
    public static void main(String[] args) throws Exception {
        // Access a service running on the local machine on port 50051
        String target = "localhost:9898";
        ManagedChannel channel = ManagedChannelBuilder.forTarget(target)
                .usePlaintext(true)
                .build();
        try {
            SkyClient client = new SkyClient(channel);
            client.sub();
            Thread.sleep(5000);
            client.export();
        } finally {
            // ManagedChannels use resources like threads and TCP connections. To prevent leaking these
            // resources the channel should be shut down when it will no longer be used. If it may be used
            // again leave it running.
            channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
        }
    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/274025.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号