Search in sources :

Example 1 with ComputeInstanceMetadata

use of io.micronaut.discovery.cloud.ComputeInstanceMetadata in project micronaut-core by micronaut-projects.

the class NettyEmbeddedServerInstance method getMetadata.

@Override
public ConvertibleValues<String> getMetadata() {
    if (instanceMetadata == null) {
        Map<String, String> cloudMetadata = new HashMap<>();
        ComputeInstanceMetadata computeInstanceMetadata = beanLocator.findBean(ComputeInstanceMetadataResolver.class).flatMap(computeInstanceMetadataResolver -> computeInstanceMetadataResolver.resolve(environment)).orElse(null);
        if (computeInstanceMetadata != null) {
            cloudMetadata = computeInstanceMetadata.getMetadata();
        }
        if (CollectionUtils.isNotEmpty(metadataContributors)) {
            for (ServiceInstanceMetadataContributor metadataContributor : metadataContributors) {
                metadataContributor.contribute(this, cloudMetadata);
            }
        }
        Map<String, String> metadata = nettyHttpServer.getServerConfiguration().getApplicationConfiguration().getInstance().getMetadata();
        if (cloudMetadata != null) {
            cloudMetadata.putAll(metadata);
        }
        instanceMetadata = ConvertibleValues.of(cloudMetadata);
    }
    return instanceMetadata;
}
Also used : Environment(io.micronaut.context.env.Environment) ConvertibleValues(io.micronaut.core.convert.value.ConvertibleValues) Parameter(io.micronaut.context.annotation.Parameter) HashMap(java.util.HashMap) Internal(io.micronaut.core.annotation.Internal) ComputeInstanceMetadataResolver(io.micronaut.discovery.cloud.ComputeInstanceMetadataResolver) ComputeInstanceMetadata(io.micronaut.discovery.cloud.ComputeInstanceMetadata) List(java.util.List) CollectionUtils(io.micronaut.core.util.CollectionUtils) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) Map(java.util.Map) Prototype(io.micronaut.context.annotation.Prototype) URI(java.net.URI) BeanLocator(io.micronaut.context.BeanLocator) ServiceInstanceMetadataContributor(io.micronaut.discovery.metadata.ServiceInstanceMetadataContributor) EmbeddedServerInstance(io.micronaut.discovery.EmbeddedServerInstance) HashMap(java.util.HashMap) ServiceInstanceMetadataContributor(io.micronaut.discovery.metadata.ServiceInstanceMetadataContributor) ComputeInstanceMetadata(io.micronaut.discovery.cloud.ComputeInstanceMetadata)

Example 2 with ComputeInstanceMetadata

use of io.micronaut.discovery.cloud.ComputeInstanceMetadata in project micronaut-grpc by micronaut-projects.

the class GrpcEmbeddedServer method start.

@Override
public EmbeddedServer start() {
    if (running.compareAndSet(false, true)) {
        try {
            server.start();
            eventPublisher.publishEvent(new ServerStartupEvent(this));
            getApplicationConfiguration().getName().ifPresent(id -> {
                Map<String, String> metadata = new LinkedHashMap<>();
                if (computeInstanceMetadataResolver != null) {
                    final Optional<ComputeInstanceMetadata> cim = computeInstanceMetadataResolver.resolve(applicationContext.getEnvironment());
                    cim.ifPresent(computeInstanceMetadata -> metadata.putAll(computeInstanceMetadata.getMetadata()));
                }
                this.serviceInstance = new GrpcServerInstance(this, id, getURI(), metadata, metadataContributors, grpcConfiguration);
                applicationContext.publishEvent(new ServiceReadyEvent(serviceInstance));
            });
        } catch (IOException e) {
            throw new ApplicationStartupException("Unable to start GRPC server: " + e.getMessage(), e);
        }
    }
    return this;
}
Also used : ServiceReadyEvent(io.micronaut.discovery.event.ServiceReadyEvent) IOException(java.io.IOException) ServerStartupEvent(io.micronaut.runtime.server.event.ServerStartupEvent) ApplicationStartupException(io.micronaut.runtime.exceptions.ApplicationStartupException) ComputeInstanceMetadata(io.micronaut.discovery.cloud.ComputeInstanceMetadata) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ComputeInstanceMetadata (io.micronaut.discovery.cloud.ComputeInstanceMetadata)2 BeanLocator (io.micronaut.context.BeanLocator)1 Parameter (io.micronaut.context.annotation.Parameter)1 Prototype (io.micronaut.context.annotation.Prototype)1 Environment (io.micronaut.context.env.Environment)1 Internal (io.micronaut.core.annotation.Internal)1 ConvertibleValues (io.micronaut.core.convert.value.ConvertibleValues)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 EmbeddedServerInstance (io.micronaut.discovery.EmbeddedServerInstance)1 ComputeInstanceMetadataResolver (io.micronaut.discovery.cloud.ComputeInstanceMetadataResolver)1 ServiceReadyEvent (io.micronaut.discovery.event.ServiceReadyEvent)1 ServiceInstanceMetadataContributor (io.micronaut.discovery.metadata.ServiceInstanceMetadataContributor)1 ApplicationStartupException (io.micronaut.runtime.exceptions.ApplicationStartupException)1 EmbeddedServer (io.micronaut.runtime.server.EmbeddedServer)1 ServerStartupEvent (io.micronaut.runtime.server.event.ServerStartupEvent)1 IOException (java.io.IOException)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1