Search in sources :

Example 1 with Codec

use of io.grpc.Codec in project opentelemetry-java by open-telemetry.

the class DefaultGrpcExporterBuilder method build.

@Override
public GrpcExporter<T> build() {
    ManagedChannel channel = this.channel;
    if (channel == null) {
        ManagedChannelBuilder<?> managedChannelBuilder = ManagedChannelBuilder.forTarget(endpoint.getAuthority());
        if (endpoint.getScheme().equals("https")) {
            managedChannelBuilder.useTransportSecurity();
        } else {
            managedChannelBuilder.usePlaintext();
        }
        if (metadata != null) {
            managedChannelBuilder.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata));
        }
        if (trustedCertificatesPem != null) {
            try {
                ManagedChannelUtil.setTrustedCertificatesPem(managedChannelBuilder, trustedCertificatesPem);
            } catch (SSLException e) {
                throw new IllegalStateException("Could not set trusted certificates for gRPC TLS connection, are they valid " + "X.509 in PEM format?", e);
            }
        }
        if (retryPolicy != null) {
            managedChannelBuilder.defaultServiceConfig(toServiceConfig(grpcServiceName, retryPolicy));
        }
        channel = managedChannelBuilder.build();
    }
    Codec codec = compressionEnabled ? new Codec.Gzip() : Codec.Identity.NONE;
    MarshalerServiceStub<T, ?, ?> stub = stubFactory.apply(channel).withCompression(codec.getMessageEncoding());
    return new DefaultGrpcExporter<>(type, channel, stub, meterProvider, timeoutNanos);
}
Also used : Codec(io.grpc.Codec) ManagedChannel(io.grpc.ManagedChannel) SSLException(javax.net.ssl.SSLException)

Example 2 with Codec

use of io.grpc.Codec in project grpc-spring-boot-starter by yidongnan.

the class AnnotationGrpcCodecDiscoverer method findGrpcCodecs.

@Override
public Collection<GrpcCodecDefinition> findGrpcCodecs() {
    if (this.definitions == null) {
        log.debug("Searching for codecs...");
        final String[] beanNames = this.applicationContext.getBeanNamesForAnnotation(GrpcCodec.class);
        final ImmutableList.Builder<GrpcCodecDefinition> builder = ImmutableList.builder();
        for (final String beanName : beanNames) {
            final Codec codec = this.applicationContext.getBean(beanName, Codec.class);
            final GrpcCodec annotation = this.applicationContext.findAnnotationOnBean(beanName, GrpcCodec.class);
            builder.add(new GrpcCodecDefinition(codec, annotation.advertised(), annotation.codecType()));
            log.debug("Found gRPC codec: {}, bean: {}, class: {}", codec.getMessageEncoding(), beanName, codec.getClass().getName());
        }
        this.definitions = builder.build();
        log.debug("Done");
    }
    return this.definitions;
}
Also used : Codec(io.grpc.Codec) ImmutableList(com.google.common.collect.ImmutableList)

Example 3 with Codec

use of io.grpc.Codec in project hummer-framework by hummer-team.

the class GrpcCommonCodecAutoConfiguration method defaultDecompressorRegistry.

@ConditionalOnBean(GrpcCodecDiscoverer.class)
@ConditionalOnMissingBean
@Bean
public DecompressorRegistry defaultDecompressorRegistry(final GrpcCodecDiscoverer codecDiscoverer) {
    log.debug("Found GrpcCodecDiscoverer -> Creating custom DecompressorRegistry");
    DecompressorRegistry registry = DecompressorRegistry.getDefaultInstance();
    for (final GrpcCodecDefinition definition : codecDiscoverer.findGrpcCodecs()) {
        if (definition.getCodecType().isForDecompression()) {
            final Codec codec = definition.getCodec();
            final boolean isAdvertised = definition.isAdvertised();
            log.debug("Registering {} decompressor: '{}' ({})", isAdvertised ? "advertised" : "", codec.getMessageEncoding(), codec.getClass().getName());
            registry = registry.with(codec, isAdvertised);
        }
    }
    return registry;
}
Also used : GrpcCodecDefinition(com.hummer.grpc.common.codec.GrpcCodecDefinition) Codec(io.grpc.Codec) DecompressorRegistry(io.grpc.DecompressorRegistry) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with Codec

use of io.grpc.Codec in project hummer-framework by hummer-team.

the class AnnotationGrpcCodecDiscoverer method findGrpcCodecs.

@Override
public Collection<GrpcCodecDefinition> findGrpcCodecs() {
    if (this.definitions == null) {
        log.debug("Searching for codecs...");
        final String[] beanNames = this.applicationContext.getBeanNamesForAnnotation(GrpcCodec.class);
        final ImmutableList.Builder<GrpcCodecDefinition> builder = ImmutableList.builder();
        for (final String beanName : beanNames) {
            final Codec codec = this.applicationContext.getBean(beanName, Codec.class);
            final GrpcCodec annotation = this.applicationContext.findAnnotationOnBean(beanName, GrpcCodec.class);
            builder.add(new GrpcCodecDefinition(codec, annotation.advertised(), annotation.codecType()));
            log.debug("Found gRPC codec: {}, bean: {}, class: {}", codec.getMessageEncoding(), beanName, codec.getClass().getName());
        }
        this.definitions = builder.build();
        log.debug("Done");
    }
    return this.definitions;
}
Also used : Codec(io.grpc.Codec) ImmutableList(com.google.common.collect.ImmutableList)

Example 5 with Codec

use of io.grpc.Codec in project opentelemetry-java by open-telemetry.

the class DefaultGrpcServiceBuilder method build.

@Override
public GrpcService<ReqT, ResT> build() {
    ManagedChannel channel = this.channel;
    if (channel == null) {
        ManagedChannelBuilder<?> managedChannelBuilder = ManagedChannelBuilder.forTarget(endpoint.getAuthority());
        if (endpoint.getScheme().equals("https")) {
            managedChannelBuilder.useTransportSecurity();
        } else {
            managedChannelBuilder.usePlaintext();
        }
        if (metadata != null) {
            managedChannelBuilder.intercept(MetadataUtils.newAttachHeadersInterceptor(metadata));
        }
        if (trustedCertificatesPem != null) {
            try {
                ManagedChannelUtil.setTrustedCertificatesPem(managedChannelBuilder, trustedCertificatesPem);
            } catch (SSLException e) {
                throw new IllegalStateException("Could not set trusted certificates for gRPC TLS connection, are they valid " + "X.509 in PEM format?", e);
            }
        }
        if (retryPolicy != null) {
            managedChannelBuilder.defaultServiceConfig(toServiceConfig(grpcServiceName, retryPolicy));
        }
        channel = managedChannelBuilder.build();
    }
    Codec codec = compressionEnabled ? new Codec.Gzip() : Codec.Identity.NONE;
    MarshalerServiceStub<ReqT, ResT, ?> stub = stubFactory.apply(channel).withCompression(codec.getMessageEncoding());
    return new DefaultGrpcService<>(type, channel, stub, timeoutNanos);
}
Also used : Codec(io.grpc.Codec) ManagedChannel(io.grpc.ManagedChannel) SSLException(javax.net.ssl.SSLException)

Aggregations

Codec (io.grpc.Codec)8 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)4 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)4 Bean (org.springframework.context.annotation.Bean)4 ImmutableList (com.google.common.collect.ImmutableList)2 GrpcCodecDefinition (com.hummer.grpc.common.codec.GrpcCodecDefinition)2 CompressorRegistry (io.grpc.CompressorRegistry)2 DecompressorRegistry (io.grpc.DecompressorRegistry)2 ManagedChannel (io.grpc.ManagedChannel)2 SSLException (javax.net.ssl.SSLException)2 GrpcCodecDefinition (net.devh.boot.grpc.common.codec.GrpcCodecDefinition)2