com.sitewhere.grpc.client.GrpcChannel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sitewhere-grpc-client Show documentation
Show all versions of sitewhere-grpc-client Show documentation
SiteWhere gRPC Client Components
/*
* Copyright (c) SiteWhere, LLC. All rights reserved. http://www.sitewhere.com
*
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package com.sitewhere.grpc.client;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import com.sitewhere.grpc.client.spi.IGrpcChannel;
import com.sitewhere.microservice.configuration.model.instance.infrastructure.GrpcConfiguration;
import com.sitewhere.microservice.lifecycle.TenantEngineLifecycleComponent;
import com.sitewhere.microservice.util.MarshalUtils;
import com.sitewhere.spi.SiteWhereException;
import com.sitewhere.spi.microservice.IFunctionIdentifier;
import com.sitewhere.spi.microservice.grpc.IGrpcServiceIdentifier;
import com.sitewhere.spi.microservice.instance.IInstanceSettings;
import com.sitewhere.spi.microservice.lifecycle.ILifecycleProgressMonitor;
import io.grpc.ManagedChannel;
import io.grpc.netty.NettyChannelBuilder;
/**
* Management wrapper for a GRPC channel.
*
* @param
* @param
*/
public abstract class GrpcChannel extends TenantEngineLifecycleComponent implements IGrpcChannel {
/** Number of seconds between DNS checks */
protected static final int DNS_CHECK_INTERVAL_SECS = 5;
/** Numer of retries for DNS checks */
protected static final int DNS_CHECK_RETRIES = 8;
/** Instance settings */
protected IInstanceSettings instanceSettings;
/** Function identifier */
protected IFunctionIdentifier functionIdentifier;
/** gRPC service identifier */
protected IGrpcServiceIdentifier grpcServiceIdentifier;
/** Remote host */
protected String hostname;
/** Remote port */
protected int port;
/** GRPC managed channe */
protected ManagedChannel channel;
/** Blocking stub */
protected B blockingStub;
/** Asynchronous stub */
protected A asyncStub;
/** Indicates whether DNS is currently verifying */
protected CountDownLatch dnsVerifying = new CountDownLatch(2);
/** Tracks whether DNS is verification succeeded */
protected AtomicBoolean dnsVerified = new AtomicBoolean(false);
/** Client interceptor for adding JWT from Spring Security context */
protected JwtClientInterceptor jwtInterceptor;
/** Thread for DNS resolution */
protected ExecutorService dnsExecutor = Executors.newSingleThreadExecutor();
public GrpcChannel(IInstanceSettings instanceSettings, IFunctionIdentifier functionIdentifier,
IGrpcServiceIdentifier grpcServiceIdentifier, int port) {
this.instanceSettings = instanceSettings;
this.functionIdentifier = functionIdentifier;
this.grpcServiceIdentifier = grpcServiceIdentifier;
this.hostname = GrpcChannel.computeHostname(instanceSettings, functionIdentifier);
this.port = port;
this.jwtInterceptor = new JwtClientInterceptor();
}
/**
* Compute service hostname based on instance settings and functional
* indentifier.
*
* @param settings
* @param identifier
* @return
*/
public static String computeHostname(IInstanceSettings settings, IFunctionIdentifier identifier) {
return String.format("%s-%s-svc", settings.getProductId(), identifier.getPath());
}
/*
* @see
* com.sitewhere.server.lifecycle.LifecycleComponent#start(com.sitewhere.spi
* .server.lifecycle.ILifecycleProgressMonitor)
*/
@Override
public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException {
try {
NettyChannelBuilder builder = NettyChannelBuilder.forAddress(getHostname(), getPort());
builder.defaultServiceConfig(buildServiceConfiguration()).enableRetry().disableServiceConfigLookUp();
builder.usePlaintext().intercept(getJwtInterceptor());
this.channel = builder.build();
this.blockingStub = createBlockingStub();
this.asyncStub = createAsyncStub();
getLogger().info(
String.format("Creating gRPC client channel connected to %s:%d ...", getHostname(), getPort()));
} catch (Throwable t) {
throw new SiteWhereException("Unhandled exception starting gRPC channel.", t);
}
}
/**
* Build service configuration that enables retry support.
*
* @return
*/
protected Map buildServiceConfiguration() {
Map serviceConfig = new HashMap<>();
serviceConfig.put("methodConfig", Collections.