Java Code Examples for com.netflix.appinfo.InstanceInfo#getInstanceId()

The following examples show how to use com.netflix.appinfo.InstanceInfo#getInstanceId() . 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: ArmeriaEurekaClientTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static com.linecorp.armeria.internal.common.eureka.InstanceInfo convertInstanceInfo(
        InstanceInfo info) {
    final PortWrapper port = new PortWrapper(info.isPortEnabled(PortType.UNSECURE), info.getPort());
    final PortWrapper securePort = new PortWrapper(info.isPortEnabled(PortType.SECURE),
                                                   info.getSecurePort());

    return new com.linecorp.armeria.internal.common.eureka.InstanceInfo(
            info.getInstanceId(),
            info.getAppName(), info.getAppGroupName(), info.getHostName(),
            info.getIPAddr(),
            info.getVIPAddress(), info.getSecureVipAddress(), port,
            securePort,
            com.linecorp.armeria.internal.common.eureka.InstanceInfo.InstanceStatus
                    .toEnum(info.getStatus().name()), info.getHomePageUrl(),
            info.getStatusPageUrl(),
            info.getHealthCheckUrl(),
            info.getSecureHealthCheckUrl(),
            convertDataCenterInfo(info.getDataCenterInfo()),
            convertLeaseInfo(info.getLeaseInfo()),
            info.getMetadata());
}
 
Example 2
Source File: GatewayNotifierTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testDistributeInvalidatedCredentials() {
    InstanceInfo targetInstanceInfo = createInstanceInfo("host", 1000, 1433);
    String targetInstanceId = targetInstanceInfo.getInstanceId();

    InstanceInfo gatewayInstance = createInstanceInfo("gateway", 111, 123);
    String gatewayUrl = "https://gateway:123/gateway/auth/distribute/" + targetInstanceId;

    Application application = mock(Application.class);
    when(application.getInstances()).thenReturn(Collections.singletonList(gatewayInstance));
    when(registry.getApplication("GATEWAY")).thenReturn(application);

    final String messageNotifyError = "org.zowe.apiml.discovery.errorNotifyingGateway";
    when(messageService.createMessage(messageNotifyError)).thenReturn(createMessage(messageNotifyError));
    final String messageKey = "org.zowe.apiml.discovery.registration.gateway.notify";
    Message msg = createMessage(messageKey, gatewayUrl, targetInstanceId);
    when(messageService.createMessage(messageKey, gatewayUrl, targetInstanceId)).thenReturn(msg);

    // succeed notified
    gatewayNotifierSync.distributeInvalidatedCredentials(targetInstanceId);
    verify(restTemplate, times(1)).getForEntity(eq(gatewayUrl), any(), (Exception) any());

    // error on notification
    when(restTemplate.getForEntity(anyString(), any())).thenThrow(new RuntimeException());
    gatewayNotifierSync.distributeInvalidatedCredentials(targetInstanceId);
    verify(messageService, times(1)).createMessage(messageKey, gatewayUrl, targetInstanceId);
}
 
Example 3
Source File: EurekaRegistry.java    From oneplatform with Apache License 2.0 5 votes vote down vote up
private void initEurekaClient(Properties properties) throws Exception{
	
	properties.setProperty("eureka.metadataMap.nodeId", NodeNameHolder.getNodeId());
	ConfigurationManager.loadProperties(properties);
	//ConfigurationManager.loadPropertiesFromResources("eureka.properties");
	
	//DynamicPropertyFactory configInstance = com.netflix.config.DynamicPropertyFactory.getInstance();
	
	MyDataCenterInstanceConfig instanceConfig = new MyDataCenterInstanceConfig(){
		@Override
		public String getHostName(boolean refresh) {
			String hostName = super.getHostName(refresh);
			if(ResourceUtils.getBoolean("eureka.preferIpAddress")){
				hostName = IpUtils.getLocalIpAddr();
			}
			return hostName;
		}

		@Override
		public String getIpAddress() {
			return IpUtils.getLocalIpAddr();
		}
		
		
	};
	InstanceInfo instanceInfo = new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get();
       applicationInfoManager = new ApplicationInfoManager(instanceConfig, instanceInfo);
       
       DefaultEurekaClientConfig clientConfig = new DefaultEurekaClientConfig();

       eurekaClient = new DiscoveryClient(applicationInfoManager, clientConfig);
       
       instanceId = instanceInfo.getInstanceId();
}
 
Example 4
Source File: ServiceInstance.java    From oneplatform with Apache License 2.0 5 votes vote down vote up
public ServiceInstance(InstanceInfo instanceInfo) {
	this.appName = instanceInfo.getAppName();
	this.instanceId = instanceInfo.getInstanceId();
	this.hostName = instanceInfo.getHostName();
	this.ipAddr = instanceInfo.getIPAddr();
	this.vipAddress = instanceInfo.getVIPAddress();
	this.status = instanceInfo.getStatus().name();
	this.port = instanceInfo.getPort();
	this.healthCheckUrl = instanceInfo.getHealthCheckUrl();
	this.lastRenewalTime = new Date(instanceInfo.getLastDirtyTimestamp());
	this.nodeId = instanceInfo.getMetadata().get("nodeId");
}
 
Example 5
Source File: CloudJacksonJson.java    From didi-eureka-server with MIT License 5 votes vote down vote up
static InstanceInfo updateIfNeeded(final InstanceInfo info) {
	if (info.getInstanceId() == null && info.getMetadata() != null) {
		String instanceId = info.getMetadata().get("instanceId");
		if (StringUtils.hasText(instanceId)) {
			// backwards compatibility for Angel
			if (StringUtils.hasText(info.getHostName()) && !instanceId.startsWith(info.getHostName())) {
				instanceId = info.getHostName()+":"+instanceId;
			}
			return new InstanceInfo.Builder(info).setInstanceId(instanceId).build();
		}
	}
	return info;
}
 
Example 6
Source File: CloudJacksonJson.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
static InstanceInfo updateIfNeeded(final InstanceInfo info) {
	if (info.getInstanceId() == null && info.getMetadata() != null) {
		String instanceId = info.getMetadata().get("instanceId");
		if (StringUtils.hasText(instanceId)) {
			// backwards compatibility for Angel
			if (StringUtils.hasText(info.getHostName())
					&& !instanceId.startsWith(info.getHostName())) {
				instanceId = info.getHostName() + ":" + instanceId;
			}
			return new InstanceInfo.Builder(info).setInstanceId(instanceId).build();
		}
	}
	return info;
}
 
Example 7
Source File: RequestAttempt.java    From zuul with Apache License 2.0 5 votes vote down vote up
public RequestAttempt(int attemptNumber, InstanceInfo server, String targetVip, String chosenWarmupLB, int status, String error, String exceptionType,
                      int readTimeout, int connectTimeout, int maxRetries)
{
    if (attemptNumber < 1) {
        throw new IllegalArgumentException("Attempt number must be greater than 0! - " + attemptNumber);
    }
    this.attempt = attemptNumber;
    this.vip = targetVip;

    if (server != null) {
        this.app = server.getAppName().toLowerCase();
        this.asg = server.getASGName();
        this.instanceId = server.getInstanceId();
        this.host = server.getHostName();
        this.port = server.getPort();

        // If targetVip is null, then try to use the actual server's vip.
        if (targetVip == null) {
            this.vip = server.getVIPAddress();
        }

        if (server.getDataCenterInfo() instanceof AmazonInfo) {
            this.availabilityZone = ((AmazonInfo) server.getDataCenterInfo()).getMetadata().get("availability-zone");

            // HACK - get region by just removing the last char from zone.
            String az = getAvailabilityZone();
            if (az != null && az.length() > 0) {
                this.region = az.substring(0, az.length() - 1);
            }
        }
    }
    
    this.status = status;
    this.error = error;
    this.exceptionType = exceptionType;
    this.readTimeout = readTimeout;
    this.connectTimeout = connectTimeout;
    this.maxRetries = maxRetries;
}
 
Example 8
Source File: DiscoveryPollingConfiguration.java    From echo with Apache License 2.0 4 votes vote down vote up
@Bean
public String currentInstanceId(InstanceInfo instanceInfo) {
  return instanceInfo.getInstanceId();
}
 
Example 9
Source File: RequestAttempt.java    From zuul with Apache License 2.0 4 votes vote down vote up
public RequestAttempt(final Server server, final IClientConfig clientConfig, int attemptNumber, int readTimeout) {
    this.status = -1;
    this.attempt = attemptNumber;
    this.readTimeout = readTimeout;

    if (server != null) {
        this.host = server.getHost();
        this.port = server.getPort();
        this.availabilityZone = server.getZone();

        if (server instanceof DiscoveryEnabledServer) {
            InstanceInfo instanceInfo = ((DiscoveryEnabledServer) server).getInstanceInfo();
            this.app = instanceInfo.getAppName().toLowerCase();
            this.asg = instanceInfo.getASGName();
            this.instanceId = instanceInfo.getInstanceId();
            this.host = instanceInfo.getHostName();
            this.port = instanceInfo.getPort();

            if (server.getPort() == instanceInfo.getSecurePort()) {
                this.vip = instanceInfo.getSecureVipAddress();
            }
            else {
                this.vip = instanceInfo.getVIPAddress();
            }
            if (instanceInfo.getDataCenterInfo() instanceof AmazonInfo) {
                this.availabilityZone = ((AmazonInfo) instanceInfo.getDataCenterInfo()).getMetadata().get("availability-zone");
            }
        }
        else {
            final Server.MetaInfo metaInfo = server.getMetaInfo();
            if (metaInfo != null) {
                this.asg = metaInfo.getServerGroup();
                this.vip = metaInfo.getServiceIdForDiscovery();
                this.instanceId = metaInfo.getInstanceId();
            }
        }
        // HACK - get region by just removing the last char from zone.
        if (availabilityZone != null && availabilityZone.length() > 0) {
            region = availabilityZone.substring(0, availabilityZone.length() - 1);
        }
    }

    if (clientConfig != null) {
        this.connectTimeout = clientConfig.get(IClientConfigKey.Keys.ConnectTimeout);
    }
}