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

The following examples show how to use org.springframework.cloud.client.ServiceInstance#getUri() . 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: ServiceUtils.java    From blog-microservices with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param serviceId
 * @param fallbackUri
 * @return
 */
protected URI getServiceUrl(String serviceId, String fallbackUri) {
    URI uri = null;
    try {
        ServiceInstance instance = loadBalancer.choose(serviceId);

        if (instance == null) {
            throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
        }

        uri = instance.getUri();
        LOG.debug("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

    } catch (RuntimeException e) {
        // Eureka not available, use fallback if specified otherwise rethrow the error
        if (fallbackUri == null) {
            throw e;

        } else {
            uri = URI.create(fallbackUri);
            LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }
    }

    return uri;
}
 
Example 2
Source File: DeliveryServerServiceImpl.java    From sds with Apache License 2.0 6 votes vote down vote up
@Override
public Server getOkServer() {

    List<ServiceInstance> serviceInstances =  discoveryClient.getInstances(SOCKET_SERVER_KEY);

    //选取socket服务
    if (serviceInstances != null && serviceInstances.size() > 0 ) {
        for(ServiceInstance instance : serviceInstances){
            URI uri = instance.getUri();
            if (uri !=null ) {
                Server server =  restTemplate.getForObject(uri+"/socket/getServer",Server.class);
                if(server.getNowCount()<=server.getMaxCount()){
                    return server;
                }
            }
        }
    }

    return null;
}
 
Example 3
Source File: DeliveryServerServiceImpl.java    From sds with Apache License 2.0 6 votes vote down vote up
@Override
public List<Server> serverList() {
    List<ServiceInstance> serviceInstances =  discoveryClient.getInstances(SOCKET_SERVER_KEY);

    List<Server> servers = new ArrayList<>();

    //选取socket服务
    if (serviceInstances != null && serviceInstances.size() > 0 ) {
        for(ServiceInstance instance : serviceInstances){
            URI uri = instance.getUri();
            if (uri !=null ) {
                Server server =  restTemplate.getForObject(uri+"/socket/getServer",Server.class);
                servers.add(server);
            }
        }
    }

    return servers;
}
 
Example 4
Source File: ServiceHelper.java    From Mastering-Microservices-with-Java-Third-Edition with MIT License 5 votes vote down vote up
/**
 *
 * @param serviceId
 * @param fallbackUri
 * @return
 */
protected URI getServiceUrl(String serviceId, String fallbackUri) {
    URI uri = null;
    try {
        ServiceInstance instance = loadBalancer.choose(serviceId);

        if (instance == null) {
            throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
        }

        uri = instance.getUri();
        LOG.info("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

    } catch (RuntimeException e) {
        e.printStackTrace();
        // Eureka not available, use fallback if specified otherwise rethrow the error
        Integer.parseInt("");
        if (fallbackUri == null) {
            throw e;
        } else {
            uri = URI.create(fallbackUri);
            LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }
    }

    return uri;
}
 
Example 5
Source File: DiscoveryRegistrationBean.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
private LightminClientApplication getLightminClientApplication(final ServiceInstance serviceInstance) {

        final URI uri = serviceInstance.getUri();
        final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder
                .fromUri(uri)
                .path(this.getContextPath(serviceInstance))
                .path("/api/lightminclientapplications");
        final String uriString = uriComponentsBuilder.toUriString();
        final ResponseEntity<LightminClientApplication> response =
                this.restTemplate.getForEntity(uriString, LightminClientApplication.class);
        ResponseUtil.checkHttpOk(response);
        return response.getBody();
    }
 
Example 6
Source File: TraversonFactory.java    From myfeed with Apache License 2.0 5 votes vote down vote up
public Traverson create(String serviceId) {
	ServiceInstance instance = loadBalancerClient.choose(serviceId);
	if (instance == null) {
		throw new IllegalStateException("No instances for service: "+serviceId);
	}
	return new Traverson(instance.getUri(), MediaTypes.HAL_JSON);
}
 
Example 7
Source File: ServiceHelper.java    From Mastering-Microservices-with-Java with MIT License 5 votes vote down vote up
/**
 *
 * @param serviceId
 * @param fallbackUri
 * @return
 */
protected URI getServiceUrl(String serviceId, String fallbackUri) {
    URI uri = null;
    try {
        ServiceInstance instance = loadBalancer.choose(serviceId);

        if (instance == null) {
            throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
        }

        uri = instance.getUri();
        LOG.info("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

    } catch (RuntimeException e) {
        e.printStackTrace();
        // Eureka not available, use fallback if specified otherwise rethrow the error
        Integer.parseInt("");
        if (fallbackUri == null) {
            throw e;
        } else {
            uri = URI.create(fallbackUri);
            LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }
    }

    return uri;
}
 
Example 8
Source File: ServiceHelper.java    From Mastering-Microservices-with-Java with MIT License 5 votes vote down vote up
/**
 *
 * @param serviceId
 * @param fallbackUri
 * @return
 */
protected URI getServiceUrl(String serviceId, String fallbackUri) {
    URI uri = null;
    try {
        ServiceInstance instance = loadBalancer.choose(serviceId);

        if (instance == null) {
            throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
        }

        uri = instance.getUri();
        LOG.info("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

    } catch (RuntimeException e) {
        e.printStackTrace();
        // Eureka not available, use fallback if specified otherwise rethrow the error
        Integer.parseInt("");
        if (fallbackUri == null) {
            throw e;
        } else {
            uri = URI.create(fallbackUri);
            LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }
    }

    return uri;
}
 
Example 9
Source File: ServiceHelper.java    From Mastering-Microservices-with-Java with MIT License 5 votes vote down vote up
/**
 *
 * @param serviceId
 * @param fallbackUri
 * @return
 */
protected URI getServiceUrl(String serviceId, String fallbackUri) {
    URI uri = null;
    try {
        ServiceInstance instance = loadBalancer.choose(serviceId);

        if (instance == null) {
            throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
        }

        uri = instance.getUri();
        LOG.info("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

    } catch (RuntimeException e) {
        e.printStackTrace();
        // Eureka not available, use fallback if specified otherwise rethrow the error
        Integer.parseInt("");
        if (fallbackUri == null) {
            throw e;
        } else {
            uri = URI.create(fallbackUri);
            LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }
    }

    return uri;
}
 
Example 10
Source File: ServiceHelper.java    From Microservices-Building-Scalable-Software with MIT License 5 votes vote down vote up
/**
 *
 * @param serviceId
 * @param fallbackUri
 * @return
 */
protected URI getServiceUrl(String serviceId, String fallbackUri) {
    URI uri = null;
    try {
        ServiceInstance instance = loadBalancer.choose(serviceId);

        if (instance == null) {
            throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
        }

        uri = instance.getUri();
        LOG.info("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

    } catch (RuntimeException e) {
        e.printStackTrace();
        // Eureka not available, use fallback if specified otherwise rethrow the error
        Integer.parseInt("");
        if (fallbackUri == null) {
            throw e;
        } else {
            uri = URI.create(fallbackUri);
            LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }
    }

    return uri;
}
 
Example 11
Source File: ServiceHelper.java    From Microservices-Building-Scalable-Software with MIT License 5 votes vote down vote up
/**
 *
 * @param serviceId
 * @param fallbackUri
 * @return
 */
protected URI getServiceUrl(String serviceId, String fallbackUri) {
    URI uri = null;
    try {
        ServiceInstance instance = loadBalancer.choose(serviceId);

        if (instance == null) {
            throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
        }

        uri = instance.getUri();
        LOG.info("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

    } catch (RuntimeException e) {
        e.printStackTrace();
        // Eureka not available, use fallback if specified otherwise rethrow the error
        Integer.parseInt("");
        if (fallbackUri == null) {
            throw e;
        } else {
            uri = URI.create(fallbackUri);
            LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }
    }

    return uri;
}
 
Example 12
Source File: ServiceHelper.java    From Microservices-Building-Scalable-Software with MIT License 5 votes vote down vote up
/**
 *
 * @param serviceId
 * @param fallbackUri
 * @return
 */
protected URI getServiceUrl(String serviceId, String fallbackUri) {
    URI uri = null;
    try {
        ServiceInstance instance = loadBalancer.choose(serviceId);

        if (instance == null) {
            throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
        }

        uri = instance.getUri();
        LOG.info("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

    } catch (RuntimeException e) {
        e.printStackTrace();
        // Eureka not available, use fallback if specified otherwise rethrow the error
        Integer.parseInt("");
        if (fallbackUri == null) {
            throw e;
        } else {
            uri = URI.create(fallbackUri);
            LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }
    }

    return uri;
}
 
Example 13
Source File: ProductApiClient.java    From microservices-springboot with MIT License 5 votes vote down vote up
private URI getProductServiceUri() {

        ServiceInstance serviceInstance = loadBalancer.choose(PRODUCT_SERVICE);
        if (serviceInstance == null) {
            throw new ServiceUnavailableException("Service unavailable");
        }

        return serviceInstance.getUri();
    }
 
Example 14
Source File: ServiceHelper.java    From Mastering-Microservices-with-Java-9-Second-Edition with MIT License 5 votes vote down vote up
/**
 *
 * @param serviceId
 * @param fallbackUri
 * @return
 */
protected URI getServiceUrl(String serviceId, String fallbackUri) {
    URI uri = null;
    try {
        ServiceInstance instance = loadBalancer.choose(serviceId);

        if (instance == null) {
            throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
        }

        uri = instance.getUri();
        LOG.info("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

    } catch (RuntimeException e) {
        e.printStackTrace();
        // Eureka not available, use fallback if specified otherwise rethrow the error
        Integer.parseInt("");
        if (fallbackUri == null) {
            throw e;
        } else {
            uri = URI.create(fallbackUri);
            LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }
    }

    return uri;
}
 
Example 15
Source File: ServiceHelper.java    From Mastering-Microservices-with-Java-9-Second-Edition with MIT License 5 votes vote down vote up
/**
 *
 * @param serviceId
 * @param fallbackUri
 * @return
 */
protected URI getServiceUrl(String serviceId, String fallbackUri) {
    URI uri = null;
    try {
        ServiceInstance instance = loadBalancer.choose(serviceId);

        if (instance == null) {
            throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
        }

        uri = instance.getUri();
        LOG.info("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

    } catch (RuntimeException e) {
        e.printStackTrace();
        // Eureka not available, use fallback if specified otherwise rethrow the error
        Integer.parseInt("");
        if (fallbackUri == null) {
            throw e;
        } else {
            uri = URI.create(fallbackUri);
            LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }
    }

    return uri;
}
 
Example 16
Source File: ServiceHelper.java    From Mastering-Microservices-with-Java-Third-Edition with MIT License 5 votes vote down vote up
/**
 *
 * @param serviceId
 * @param fallbackUri
 * @return
 */
protected URI getServiceUrl(String serviceId, String fallbackUri) {
    URI uri = null;
    try {
        ServiceInstance instance = loadBalancer.choose(serviceId);

        if (instance == null) {
            throw new RuntimeException("Can't find a service with serviceId = " + serviceId);
        }

        uri = instance.getUri();
        LOG.info("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

    } catch (RuntimeException e) {
        e.printStackTrace();
        // Eureka not available, use fallback if specified otherwise rethrow the error
        Integer.parseInt("");
        if (fallbackUri == null) {
            throw e;
        } else {
            uri = URI.create(fallbackUri);
            LOG.warn("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }
    }

    return uri;
}
 
Example 17
Source File: ServiceDescriptionUpdater.java    From microservice-patterns with Apache License 2.0 4 votes vote down vote up
private String getSwaggerURL( ServiceInstance instance){
	String swaggerURL = instance.getMetadata().get(KEY_SWAGGER_URL);
	return swaggerURL != null ? instance.getUri()+swaggerURL : instance.getUri()+DEFAULT_SWAGGER_URL;
}
 
Example 18
Source File: DefaultServiceInstanceConverter.java    From spring-boot-admin with Apache License 2.0 4 votes vote down vote up
protected URI getServiceUrl(ServiceInstance instance) {
	return instance.getUri();
}
 
Example 19
Source File: DiscoveredResource.java    From spring-cloud-commons with Apache License 2.0 3 votes vote down vote up
private Link discoverLink() {

		try {

			ServiceInstance service = this.provider.getServiceInstance();

			if (service == null) {
				return null;
			}

			URI uri = service.getUri();
			String serviceId = service.getServiceId();

			this.log.debug("Discovered {} system at {}. Discovering resource…", serviceId,
					uri);

			Traverson traverson = new Traverson(uri, MediaTypes.HAL_JSON);
			Link link = this.traversal.buildTraversal(traverson).asTemplatedLink();

			this.log.debug("Found link pointing to {}.", link.getHref());

			return link;

		}
		catch (RuntimeException o_O) {

			this.link = null;
			this.log.debug("Target system unavailable. Got: ", o_O.getMessage());

			return null;
		}
	}