org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient Java Examples

The following examples show how to use org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient. 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: MossInstanceDiscoveryListener.java    From Moss with Apache License 2.0 5 votes vote down vote up
public List<ServiceInstance> getInstances(String serviceId) {
    List<InstanceInfo> infos = cloudEurekaClient.get().getInstancesByVipAddress(serviceId,
            false);
    List<ServiceInstance> instances = new ArrayList<>();
    for (InstanceInfo info : infos) {
        EurekaDiscoveryClient.EurekaServiceInstance eurekaServiceInstance = new EurekaDiscoveryClient.EurekaServiceInstance(info);
        instances.add(eurekaServiceInstance);
    }
    return instances;
}
 
Example #2
Source File: ApimlRetryableClientTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
void givenServiceId_whenChoose_thenProducesServiceInstance() {
    ApimlRetryableClient client = new ApimlRetryableClient(
        httpClient, config, introspector, retryFactory
    );
    doReturn(new Server("aaa", 22)).when(lb).chooseServer(any());
    RequestContextUtils.setInstanceInfo(
        InstanceInfo.Builder.newBuilder().setAppName("Sonya").build()
    );
    client.setLoadBalancer(lb);

    ServiceInstance instance = client.choose("service1");

    assertThat(instance, instanceOf(EurekaDiscoveryClient.EurekaServiceInstance.class));
}
 
Example #3
Source File: ConfigServerEurekaApplicationTests.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void discoveryClientIsEureka() {
	assertTrue("discoveryClient is wrong type", discoveryClient instanceof CompositeDiscoveryClient);
	assertTrue("composite discovery client should have a Eureka client with higher precendence",
			((CompositeDiscoveryClient) discoveryClient).getDiscoveryClients()
					.get(0) instanceof EurekaDiscoveryClient);
}
 
Example #4
Source File: ZuulProxyEurekaApplicationTests.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void discoveryClientIsEureka() {
	assertTrue("discoveryClient is wrong type", discoveryClient instanceof CompositeDiscoveryClient);
	assertTrue("composite discovery client should compose Eureka Discovery Client with highest precedence",
			((CompositeDiscoveryClient) discoveryClient).getDiscoveryClients()
					.get(0) instanceof EurekaDiscoveryClient);
}
 
Example #5
Source File: EurekaFirstApplicationTests.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void discoveryClientIsEureka() {
	assertTrue("discoveryClient is wrong type: " + discoveryClient,
			discoveryClient instanceof CompositeDiscoveryClient);
	assertTrue("composite discovery client should have a Eureka client with higher precendence",
			((CompositeDiscoveryClient) discoveryClient).getDiscoveryClients()
					.get(0) instanceof EurekaDiscoveryClient);
}
 
Example #6
Source File: StandaloneClientApplicationTests.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void testDiscoveryClientIsEureka() {
	assertTrue("discoveryClient is wrong instance type", discoveryClient instanceof CompositeDiscoveryClient);
	assertTrue("composite discovery client should have a Eureka client with higher precendence",
			((CompositeDiscoveryClient) discoveryClient).getDiscoveryClients()
					.get(0) instanceof EurekaDiscoveryClient);
}
 
Example #7
Source File: EurekaServerApplicationTests.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void discoveryClientIsEureka() {
	assertTrue("discoveryClient is wrong type " + discoveryClient.getClass(),
			discoveryClient instanceof CompositeDiscoveryClient);
	CompositeDiscoveryClient compositeDiscoveryClient = (CompositeDiscoveryClient) discoveryClient;
	assertTrue("composite discovery client should be composed of Eureka and Simple Discovery client's",
			compositeDiscoveryClient.getDiscoveryClients().size() == 2);

	assertTrue("the composed discovery client should have a EurekaDiscoveryClient with a higher precedence ",
			compositeDiscoveryClient.getDiscoveryClients().get(0) instanceof EurekaDiscoveryClient);
}
 
Example #8
Source File: TurbineStreamApplicationTests.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void discoveryClientIsEureka() {
	assertTrue("discoveryClient is wrong type " + discoveryClient.getClass(),
			discoveryClient instanceof CompositeDiscoveryClient);
	CompositeDiscoveryClient compositeDiscoveryClient = (CompositeDiscoveryClient) discoveryClient;
	assertTrue("composite discovery client should be composed of Eureka and Simple Discovery client's",
			compositeDiscoveryClient.getDiscoveryClients().size() == 2);

	assertTrue("the composed discovery client should have a EurekaDiscoveryClient with a higher precedence ",
			compositeDiscoveryClient.getDiscoveryClients().get(0) instanceof EurekaDiscoveryClient);

	assertTrue("the composed discovery client should have a SimpleDiscoveryClient with a lower precedence ",
			compositeDiscoveryClient.getDiscoveryClients().get(1) instanceof SimpleDiscoveryClient);
}
 
Example #9
Source File: TroubleService.java    From khs-trouble-maker with Apache License 2.0 5 votes vote down vote up
public String serviceInstanceURL(String serviceName, String instanceId) {
	// FORM LIST OF INSTANCES FOR SERVICENAME
	List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);

	String returnHostAndPort = "";

	// IF AN "INSTANCEID" VALUE WAS PASSED IN, THEN FIND THAT PARTICULAR ONE
	// ELSE JUST PICK A RANDOM INSTANCE FOR THE SERVICENAME
	if(!instanceId.equals("")) { 
		// LOOP THROUGH SERVICEINSTANCES OF SERVICE, ATTEMPING TO MATCH ON INSTANCEID
		for (Iterator<ServiceInstance> iterator = instances.iterator(); iterator.hasNext();) {
			EurekaDiscoveryClient.EurekaServiceInstance serviceInstance = (EurekaDiscoveryClient.EurekaServiceInstance) iterator.next();

			String tmpInstanceId = serviceInstance.getInstanceInfo().getInstanceId();
			//System.out.println(tmpInstanceId);
			
			if(tmpInstanceId.equals(instanceId)) {
				returnHostAndPort = serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/";
				break;
			}
		}
	} else {
		Random rn = new Random();
		int range = instances.size();
		int randomNum = rn.nextInt(range);
		ServiceInstance rndm = instances.get(randomNum);
		returnHostAndPort = rndm.getHost() + ":" + rndm.getPort() + "/";
	}

	return returnHostAndPort;
}
 
Example #10
Source File: ApimlRetryableClient.java    From api-layer with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Override method from {@link com.netflix.client.AbstractLoadBalancerAwareClient} because it constructs
 * a {@link RibbonLoadBalancerClient.RibbonServer} that is not fit for our use. Namely, it's getUri() method always returns http scheme, causing the
 * retryable client to always call http. We have stored the instance info from the load balancer in the context
 * so we recreate EurekaServiceInstance here, which correctly resolves whether instance is http or https when asked.
 * @param serviceId
 * @return EurekaServiceInstance
 */
@Override
public ServiceInstance choose(String serviceId) {
    super.choose(serviceId);
    return new EurekaDiscoveryClient.EurekaServiceInstance(
        RequestContextUtils.getInstanceInfo().orElseThrow(() -> new RequestContextNotPreparedException("request context not prepared")));
}