Java Code Examples for org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient#EurekaServiceInstance

The following examples show how to use org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient#EurekaServiceInstance . 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: 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 3
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")));
}