CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-grpc--grpc-core

Core gRPC library containing transport implementation, channel abstraction, load balancing, name resolution, and other fundamental gRPC functionalities for Java

Pending
Overview
Eval results
Files

service-providers.mddocs/

Service Providers

gRPC Core exposes its primary functionality through Java's Service Provider Interface (SPI) mechanism. This allows automatic discovery and registration of core gRPC functionality without requiring direct API calls.

Capabilities

Load Balancer Provider Registration

Registers the default pick-first load balancing strategy with the gRPC framework.

/**
 * Service registration: META-INF/services/io.grpc.LoadBalancerProvider
 * Implementation: io.grpc.internal.PickFirstLoadBalancerProvider
 */
class PickFirstLoadBalancerProvider extends LoadBalancerProvider {
    /**
     * Indicates if this provider is available for use
     * @return true if the provider can be used
     */
    public boolean isAvailable();
    
    /**
     * Gets the priority of this provider relative to others
     * @return priority value (higher numbers indicate higher priority)
     */
    public int getPriority();
    
    /**
     * Gets the policy name for this load balancer
     * @return "pick_first" - the name used in service config
     */
    public String getPolicyName();
    
    /**
     * Creates a new load balancer instance
     * @param helper Helper providing access to channel functionality
     * @return PickFirstLoadBalancer instance
     */
    public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper);
}

Usage:

import java.util.ServiceLoader;
import io.grpc.LoadBalancerProvider;

// Automatic discovery via SPI
ServiceLoader<LoadBalancerProvider> providers = ServiceLoader.load(LoadBalancerProvider.class);
for (LoadBalancerProvider provider : providers) {
    if ("pick_first".equals(provider.getPolicyName())) {
        LoadBalancer lb = provider.newLoadBalancer(helper);
        break;
    }
}

Name Resolver Provider Registration

Registers DNS-based name resolution functionality with the gRPC framework.

/**
 * Service registration: META-INF/services/io.grpc.NameResolverProvider
 * Implementation: io.grpc.internal.DnsNameResolverProvider
 */
class DnsNameResolverProvider extends NameResolverProvider {
    /**
     * Indicates if this provider is available for use
     * @return true if DNS resolution is available
     */
    protected boolean isAvailable();
    
    /**
     * Gets the priority of this provider relative to others
     * @return priority value for DNS resolution (5 for DNS)
     */
    protected int priority();
    
    /**
     * Gets the default URI scheme handled by this provider
     * @return "dns" - the scheme for DNS-based name resolution
     */
    public String getDefaultScheme();
    
    /**
     * Creates a new name resolver for the given target URI
     * @param targetUri The target URI to resolve (e.g., dns:///example.com:443)
     * @param args Arguments containing channel configuration
     * @return DnsNameResolver instance or null if URI is not supported
     */
    public NameResolver newNameResolver(URI targetUri, NameResolver.Args args);
    
    /**
     * Returns the socket address types this provider produces
     * @return Collection containing InetSocketAddress.class
     */
    public Collection<Class<? extends SocketAddress>> getProducedSocketAddressTypes();
}

Usage:

import java.net.URI;
import java.net.SocketAddress;
import java.util.Collection;
import java.util.ServiceLoader;
import io.grpc.NameResolverProvider;

// Automatic discovery via SPI
URI targetUri = URI.create("dns:///example.com:443");
ServiceLoader<NameResolverProvider> providers = ServiceLoader.load(NameResolverProvider.class);

for (NameResolverProvider provider : providers) {
    NameResolver resolver = provider.newNameResolver(targetUri, args);
    if (resolver != null) {
        resolver.start(listener);
        break;
    }
}

Service Discovery Process

gRPC automatically discovers and loads service providers during framework initialization:

  1. Classpath Scanning: gRPC scans for META-INF/services/ files matching interface names
  2. Provider Loading: Service providers are loaded using ServiceLoader.load()
  3. Priority Ordering: Providers are ordered by priority (higher numbers first)
  4. Availability Check: Only available providers are considered for use
  5. Default Selection: The highest priority available provider becomes the default

Provider Interface Contracts

LoadBalancerProvider Contract

abstract class LoadBalancerProvider {
    /**
     * Whether this provider is available for use
     */
    public abstract boolean isAvailable();
    
    /**
     * A priority, from 0 to 10 that this provider should be used, higher priority wins
     */
    public abstract int getPriority();
    
    /**
     * The policy name for this provider, which makes it selectable via service config
     */
    public abstract String getPolicyName();
    
    /**
     * Creates a LoadBalancer for the given helper
     */
    public abstract LoadBalancer newLoadBalancer(LoadBalancer.Helper helper);
    
    /**
     * Parses the given LoadBalancingPolicy into configuration for this provider
     */
    public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawLoadBalancingPolicyConfig);
}

NameResolverProvider Contract

abstract class NameResolverProvider {
    /**
     * Whether this provider is available for use
     */
    protected abstract boolean isAvailable();
    
    /**
     * A priority, from 0 to 10 that this provider should be used, higher priority wins
     */
    protected abstract int priority();
    
    /**
     * Returns the default scheme for this provider
     */
    public abstract String getDefaultScheme();
    
    /**
     * Creates a NameResolver for the given target URI, or null if not supported
     */
    public abstract NameResolver newNameResolver(URI targetUri, NameResolver.Args args);
    
    /**
     * Returns the socket address types this provider produces
     */
    public Collection<Class<? extends SocketAddress>> getProducedSocketAddressTypes();
}

Error Handling

Service providers handle errors gracefully:

  • Unavailable Provider: isAvailable() returns false if provider cannot be used
  • Unsupported URI: newNameResolver() returns null for unsupported target URIs
  • Configuration Errors: Provider configuration errors are wrapped in ConfigOrError objects
  • Runtime Errors: Runtime errors during provider operations are propagated as gRPC Status errors

Install with Tessl CLI

npx tessl i tessl/maven-io-grpc--grpc-core

docs

buffer-management.md

builders-factories.md

core-utilities.md

index.md

load-balancing-name-resolution.md

service-providers.md

transport-layer.md

tile.json