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;

    /**
     * Construct client for accessing HelloWorld server using the existing channel.
     */
    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<String> subscriptionSet = new HashSet<>();

    /**
     * Say hello to server.
     */
    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<ExportMetricValue> requestObserver = exportStub.export(new StreamObserver<ExportResponse>() {
            @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();

    }


    /**
     * Greet server. If provided, the first element of {@code args} is the name to use in the
     * greeting. The second argument is the target server.
     */
    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);
        }
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值