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);
}
}
}
gRPC-client
最新推荐文章于 2023-05-25 14:08:33 发布