Search in sources :

Example 11 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project eureka by Netflix.

the class AbstractJerseyEurekaHttpClient method sendHeartBeat.

@Override
public EurekaHttpResponse<InstanceInfo> sendHeartBeat(String appName, String id, InstanceInfo info, InstanceStatus overriddenStatus) {
    String urlPath = "apps/" + appName + '/' + id;
    ClientResponse response = null;
    try {
        WebResource webResource = jerseyClient.resource(serviceUrl).path(urlPath).queryParam("status", info.getStatus().toString()).queryParam("lastDirtyTimestamp", info.getLastDirtyTimestamp().toString());
        if (overriddenStatus != null) {
            webResource = webResource.queryParam("overriddenstatus", overriddenStatus.name());
        }
        Builder requestBuilder = webResource.getRequestBuilder();
        addExtraHeaders(requestBuilder);
        response = requestBuilder.put(ClientResponse.class);
        EurekaHttpResponseBuilder<InstanceInfo> eurekaResponseBuilder = anEurekaHttpResponse(response.getStatus(), InstanceInfo.class).headers(headersOf(response));
        if (response.hasEntity()) {
            eurekaResponseBuilder.entity(response.getEntity(InstanceInfo.class));
        }
        return eurekaResponseBuilder.build();
    } finally {
        if (logger.isDebugEnabled()) {
            logger.debug("Jersey HTTP PUT {}/{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus());
        }
        if (response != null) {
            response.close();
        }
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) EurekaHttpResponseBuilder(com.netflix.discovery.shared.transport.EurekaHttpResponse.EurekaHttpResponseBuilder) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 12 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project eureka by Netflix.

the class AbstractJerseyEurekaHttpClient method getInstanceInternal.

private EurekaHttpResponse<InstanceInfo> getInstanceInternal(String urlPath) {
    ClientResponse response = null;
    try {
        Builder requestBuilder = jerseyClient.resource(serviceUrl).path(urlPath).getRequestBuilder();
        addExtraHeaders(requestBuilder);
        response = requestBuilder.accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
        InstanceInfo infoFromPeer = null;
        if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) {
            infoFromPeer = response.getEntity(InstanceInfo.class);
        }
        return anEurekaHttpResponse(response.getStatus(), InstanceInfo.class).headers(headersOf(response)).entity(infoFromPeer).build();
    } finally {
        if (logger.isDebugEnabled()) {
            logger.debug("Jersey HTTP GET {}/{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus());
        }
        if (response != null) {
            response.close();
        }
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) EurekaHttpResponseBuilder(com.netflix.discovery.shared.transport.EurekaHttpResponse.EurekaHttpResponseBuilder) Builder(com.sun.jersey.api.client.WebResource.Builder) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 13 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project eureka by Netflix.

the class DiscoveryClientRegistryTest method testGetByVipInRemoteRegion.

@Test
public void testGetByVipInRemoteRegion() throws Exception {
    prepareRemoteRegionRegistry();
    EurekaClient client = discoveryClientResource.getClient();
    String vipAddress = takeFirst(client.getApplicationsForARegion(TEST_REMOTE_REGION)).getVIPAddress();
    List<InstanceInfo> instancesByVipAddress = client.getInstancesByVipAddress(vipAddress, false, TEST_REMOTE_REGION);
    assertThat(instancesByVipAddress.size(), is(equalTo(2)));
    InstanceInfo instance = instancesByVipAddress.iterator().next();
    assertThat(instance.getVIPAddress(), is(equalTo(vipAddress)));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Example 14 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project eureka by Netflix.

the class DiscoveryClientRegistryTest method testApplyDeltaWithBadInstanceInfoDataCenterInfoAsNull.

@Test
public void testApplyDeltaWithBadInstanceInfoDataCenterInfoAsNull() throws Exception {
    InstanceInfoGenerator instanceGen = InstanceInfoGenerator.newBuilder(2, "testApp").build();
    // Full fetch with one item
    InstanceInfo first = instanceGen.first();
    Applications initial = toApplications(first);
    when(requestHandler.getApplications(TEST_REMOTE_REGION)).thenReturn(anEurekaHttpResponse(200, initial).type(MediaType.APPLICATION_JSON_TYPE).build());
    EurekaClient client = discoveryClientResource.getClient();
    assertThat(client.getApplications().getAppsHashCode(), is(equalTo("UP_1_")));
    // Delta with one add
    InstanceInfo second = new InstanceInfo.Builder(instanceGen.take(1)).setInstanceId("foo1").setStatus(InstanceStatus.DOWN).setDataCenterInfo(null).build();
    InstanceInfo third = new InstanceInfo.Builder(instanceGen.take(1)).setInstanceId("foo2").setStatus(InstanceStatus.UP).setDataCenterInfo(new DataCenterInfo() {

        @Override
        public Name getName() {
            return null;
        }
    }).build();
    Applications delta = toApplications(second, third);
    delta.setAppsHashCode("DOWN_1_UP_2_");
    when(requestHandler.getDelta(TEST_REMOTE_REGION)).thenReturn(anEurekaHttpResponse(200, delta).type(MediaType.APPLICATION_JSON_TYPE).build());
    assertThat(discoveryClientResource.awaitCacheUpdate(5, TimeUnit.SECONDS), is(true));
    assertThat(client.getApplications().getAppsHashCode(), is(equalTo("DOWN_1_UP_2_")));
}
Also used : Applications(com.netflix.discovery.shared.Applications) EurekaEntityFunctions.toApplications(com.netflix.discovery.util.EurekaEntityFunctions.toApplications) EurekaEntityFunctions.mergeApplications(com.netflix.discovery.util.EurekaEntityFunctions.mergeApplications) EurekaEntityFunctions.copyApplications(com.netflix.discovery.util.EurekaEntityFunctions.copyApplications) InstanceInfoGenerator(com.netflix.discovery.util.InstanceInfoGenerator) DataCenterInfo(com.netflix.appinfo.DataCenterInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Example 15 with InstanceInfo

use of com.netflix.appinfo.InstanceInfo in project eureka by Netflix.

the class DiscoveryClientRegistryTest method testEurekaClientPeriodicHeartbeat.

@Test
public void testEurekaClientPeriodicHeartbeat() throws Exception {
    DiscoveryClientResource registeringClientResource = discoveryClientResource.fork().withRegistration(true).withRegistryFetch(false).build();
    InstanceInfo instance = registeringClientResource.getMyInstanceInfo();
    when(requestHandler.register(any(InstanceInfo.class))).thenReturn(EurekaHttpResponse.status(204));
    when(requestHandler.sendHeartBeat(instance.getAppName(), instance.getId(), null, null)).thenReturn(anEurekaHttpResponse(200, InstanceInfo.class).build());
    // Initialize
    registeringClientResource.getClient();
    verify(requestHandler, timeout(5 * 1000).atLeast(2)).sendHeartBeat(instance.getAppName(), instance.getId(), null, null);
}
Also used : DiscoveryClientResource(com.netflix.discovery.junit.resource.DiscoveryClientResource) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Aggregations

InstanceInfo (com.netflix.appinfo.InstanceInfo)192 Test (org.junit.Test)74 Application (com.netflix.discovery.shared.Application)35 Applications (com.netflix.discovery.shared.Applications)22 AmazonInfo (com.netflix.appinfo.AmazonInfo)21 ArrayList (java.util.ArrayList)19 InstanceStatus (com.netflix.appinfo.InstanceInfo.InstanceStatus)11 Before (org.junit.Before)11 DiscoveryClient (com.netflix.discovery.DiscoveryClient)10 DecoderWrapper (com.netflix.discovery.converters.wrappers.DecoderWrapper)10 Lease (com.netflix.eureka.lease.Lease)9 HashMap (java.util.HashMap)8 Response (javax.ws.rs.core.Response)8 DataCenterInfo (com.netflix.appinfo.DataCenterInfo)7 CodecWrappers (com.netflix.discovery.converters.wrappers.CodecWrappers)7 EncoderWrapper (com.netflix.discovery.converters.wrappers.EncoderWrapper)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ApplicationInfoManager (com.netflix.appinfo.ApplicationInfoManager)6 EurekaEntityFunctions.mergeApplications (com.netflix.discovery.util.EurekaEntityFunctions.mergeApplications)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6