Java Code Examples for org.springframework.cloud.client.ServiceInstance#getMetadata()

The following examples show how to use org.springframework.cloud.client.ServiceInstance#getMetadata() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SurgicalRoutingRequestTransformer.java    From spring-cloud-services-starters with Apache License 2.0 6 votes vote down vote up
@Override
public HttpRequest transformRequest(HttpRequest request, ServiceInstance instance) {
	Map<String, String> metadata = instance.getMetadata();
	if (metadata.containsKey(CF_APP_GUID) && metadata.containsKey(CF_INSTANCE_INDEX)) {
		final String headerValue = String.format("%s:%s", metadata.get(CF_APP_GUID),
				metadata.get(CF_INSTANCE_INDEX));
		// request.getHeaders might be immutable, so return a wrapper
		return new HttpRequestWrapper(request) {
			@Override
			public HttpHeaders getHeaders() {
				HttpHeaders headers = new HttpHeaders();
				headers.putAll(super.getHeaders());
				headers.add(SURGICAL_ROUTING_HEADER, headerValue);
				return headers;
			}
		};
	}
	return request;
}
 
Example 2
Source File: SurgicalRoutingRequestTransformer.java    From spring-cloud-services-connector with Apache License 2.0 6 votes vote down vote up
@Override
public HttpRequest transformRequest(HttpRequest request, ServiceInstance instance) {
	Map<String, String> metadata = instance.getMetadata();
	if (metadata.containsKey(CF_APP_GUID) && metadata.containsKey(CF_INSTANCE_INDEX)) {
		final String headerValue = String.format("%s:%s", metadata.get(CF_APP_GUID), metadata.get(CF_INSTANCE_INDEX));
		// request.getHeaders might be immutable, so return a wrapper
		return new HttpRequestWrapper(request) {
			@Override
			public HttpHeaders getHeaders() {
				HttpHeaders headers = new HttpHeaders();
				headers.putAll(super.getHeaders());
				headers.add(SURGICAL_ROUTING_HEADER, headerValue);
				return headers;
			}
		};
	}
	return request;
}
 
Example 3
Source File: DiscoveryClientNameResolver.java    From grpc-spring-boot-starter with MIT License 6 votes vote down vote up
/**
 * Extracts the gRPC server port from the given service instance.
 *
 * @param instance The instance to extract the port from.
 * @return The gRPC server port.
 * @throws IllegalArgumentException If the specified port definition couldn't be parsed.
 */
private int getGRPCPort(final ServiceInstance instance) {
    final Map<String, String> metadata = instance.getMetadata();
    if (metadata == null) {
        return instance.getPort();
    }
    final String portString = metadata.get(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT);
    if (portString == null) {
        return instance.getPort();
    }
    try {
        return Integer.parseInt(portString);
    } catch (final NumberFormatException e) {
        // TODO: How to handle this case?
        throw new IllegalArgumentException("Failed to parse gRPC port information from: " + instance, e);
    }
}
 
Example 4
Source File: DiscoveryClientNameResolver.java    From grpc-spring-boot-starter with MIT License 6 votes vote down vote up
/**
 * Extracts the gRPC server port from the given service instance.
 *
 * @param instance The instance to extract the port from.
 * @return The gRPC server port.
 * @throws IllegalArgumentException If the specified port definition couldn't be parsed.
 */
private int getGRPCPort(final ServiceInstance instance) {
    final Map<String, String> metadata = instance.getMetadata();
    if (metadata == null) {
        return instance.getPort();
    }
    final String portString = metadata.get(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT);
    if (portString == null) {
        return instance.getPort();
    }
    try {
        return Integer.parseInt(portString);
    } catch (final NumberFormatException e) {
        // TODO: How to handle this case?
        throw new IllegalArgumentException("Failed to parse gRPC port information from: " + instance, e);
    }
}
 
Example 5
Source File: ConsulDiscoveryClientCustomizedTests.java    From spring-cloud-consul with Apache License 2.0 5 votes vote down vote up
private void assertInstance(ServiceInstance instance) {
	assertThat(instance.getInstanceId()).as("instance id was wrong")
			.isEqualTo("testConsulDiscovery2Id");
	assertThat(instance.getServiceId()).as("service id was wrong")
			.isEqualTo("testConsulDiscovery2");

	Map<String, String> metadata = instance.getMetadata();
	assertThat(metadata).as("metadata was null").isNotNull();

	String foo = metadata.get("foo");
	assertThat(foo).as("metadata key foo was wrong").isEqualTo("bar");

	String foo2 = metadata.get("foo2");
	assertThat(foo2).as("metadata key foo2 was wrong").isEqualTo("bar2");
}
 
Example 6
Source File: DefaultServiceLoadBalancer.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
protected ServiceDefinition convertServiceInstanceToServiceDefinition(ServiceInstance instance) {
    return new DefaultServiceDefinition(
        instance.getServiceId(),
        instance.getHost(),
        instance.getPort(),
        instance.getMetadata()
    );
}
 
Example 7
Source File: ZonePreferenceServiceInstanceListSupplier.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
private String getZone(ServiceInstance serviceInstance) {
	Map<String, String> metadata = serviceInstance.getMetadata();
	if (metadata != null) {
		return metadata.get(ZONE);
	}
	return null;
}
 
Example 8
Source File: DiscoveryRegistrationBean.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
private String getContextPath(final ServiceInstance serviceInstance) {
    final Map<String, String> metaData = serviceInstance.getMetadata();
    final String contextPath;
    if (metaData != null) {
        contextPath = metaData.getOrDefault("contextPath", "");
    } else {
        log.debug("No meta data available, nothing todo");
        contextPath = "";
    }
    return contextPath;
}
 
Example 9
Source File: LightminApplicationDiscoveryListener.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
private Boolean checkIsLightminInstance(final ServiceInstance serviceInstance) {
    final Boolean result;
    final Map<String, String> metaData = serviceInstance.getMetadata();
    if (metaData.containsKey(DiscoveryBase.LIGHTMIN_CLIENT_META_DATA_KEY)) {
        final String value = metaData.get(DiscoveryBase.LIGHTMIN_CLIENT_META_DATA_KEY);
        if (DiscoveryBase.LIGHTMIN_CLIENT_META_DATA_VALUE.equals(value)) {
            result = Boolean.TRUE;
        } else {
            result = Boolean.FALSE;
        }
    } else {
        result = Boolean.FALSE;
    }
    return result;
}
 
Example 10
Source File: DubboServiceMetadataRepository.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
public Integer getDubboProtocolPort(ServiceInstance serviceInstance,
		String protocol) {
	String protocolProperty = getDubboProtocolPropertyName(protocol);
	Map<String, String> metadata = serviceInstance.getMetadata();
	String protocolPort = metadata.get(protocolProperty);
	return hasText(protocolPort) ? Integer.valueOf(protocolPort) : null;
}
 
Example 11
Source File: ClientDiscoveryImpl.java    From BootNettyRpc with Apache License 2.0 5 votes vote down vote up
private List<NettyClient> convertNettyClients(List<ServiceInstance> serviceInstances) {
    if (CollectionUtils.isEmpty( serviceInstances )) {
        return null;
    }
    List<NettyClient> nettyClients = new CopyOnWriteArrayList<>();
    for (ServiceInstance serviceInstance : serviceInstances) {
        NettyClient nettyClient = new NettyClient();
        Map<String, String> metadata = serviceInstance.getMetadata();
        String host = metadata.get( NettyRpcConstants.NETTY_SERVER_HOST );
        if (StringUtils.isEmpty( host )) {
            host = serviceInstance.getHost();
        }
        nettyClient.setHost( host );

        String name = metadata.get( NettyRpcConstants.NETTY_SERVER_NAME );
        if (StringUtils.isEmpty( name )) {
            name = serviceInstance.getServiceId();
        }
        nettyClient.setName( name );

        String port = metadata.get( NettyRpcConstants.NETTY_SERVER_PORT );
        if (StringUtils.isEmpty( port )) {
            throw new CommonException( "netty server " + name + " 's port connot be null" );
        }
        nettyClient.setPort( Integer.valueOf( port ) );
        nettyClients.add( nettyClient );
    }
    return nettyClients;


}
 
Example 12
Source File: DefaultServiceInstanceConverter.java    From Moss with Apache License 2.0 4 votes vote down vote up
protected Map<String, String> getMetadata(ServiceInstance instance) {
    return instance.getMetadata() != null ? instance.getMetadata() : emptyMap();
}
 
Example 13
Source File: DefaultServiceInstanceConverter.java    From Moss with Apache License 2.0 4 votes vote down vote up
protected Map<String, String> getMetadata(ServiceInstance instance) {
    return instance.getMetadata() != null ? instance.getMetadata() : emptyMap();
}
 
Example 14
Source File: DefaultServiceInstanceConverter.java    From Moss with Apache License 2.0 4 votes vote down vote up
protected Map<String, String> getMetadata(ServiceInstance instance) {
    return instance.getMetadata() != null ? instance.getMetadata() : emptyMap();
}
 
Example 15
Source File: DefaultServiceInstanceConverter.java    From Moss with Apache License 2.0 4 votes vote down vote up
protected Map<String, String> getMetadata(ServiceInstance instance) {
    return instance.getMetadata() != null ? instance.getMetadata() : emptyMap();
}
 
Example 16
Source File: DefaultServiceInstanceConverter.java    From spring-boot-admin with Apache License 2.0 4 votes vote down vote up
protected Map<String, String> getMetadata(ServiceInstance instance) {
	return (instance.getMetadata() != null) ? instance.getMetadata() : emptyMap();
}
 
Example 17
Source File: DefaultServiceInstanceConverter.java    From Moss with Apache License 2.0 4 votes vote down vote up
protected Map<String, String> getMetadata(ServiceInstance instance) {
    return instance.getMetadata() != null ? instance.getMetadata() : emptyMap();
}
 
Example 18
Source File: DubboServiceMetadataRepository.java    From spring-cloud-alibaba with Apache License 2.0 2 votes vote down vote up
/**
 * Get the {@link URL urls} that {@link DubboMetadataService} exported by the
 * specified {@link ServiceInstance}.
 * @param serviceInstance {@link ServiceInstance}
 * @return the mutable {@link URL urls}
 */
public List<URL> getDubboMetadataServiceURLs(ServiceInstance serviceInstance) {
	Map<String, String> metadata = serviceInstance.getMetadata();
	String dubboURLsJSON = metadata.get(DUBBO_METADATA_SERVICE_URLS_PROPERTY_NAME);
	return jsonUtils.toURLs(dubboURLsJSON);
}