9 Şubat 2021 Salı

SpringCloud Netflix Eureka DiscoveryClient Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.cloud.client.discovery.DiscoveryClient;
getInstances metodu - service name
Örnek
Şöyle yaparız
public static final String URL_DISCOVER_BOOKS = "%s/books?bridgeEndpoint=true";

@Autowired
private DiscoveryClient discoveryClient;

String.format(URL_DISCOVER_BOOKS,
discoveryClient.getInstances("discovery-client").get(0).getUri());
Örnek
Şöyle yaparız
// (Need!!) eureka.client.fetchRegistry=true
List<ServiceInstance> instances = this.discoveryClient.getInstances(serviceId);
 
if (instances == null || instances.isEmpty()) {
  return "No instances for service: " + serviceId;
}
String html = "<h2>Instances for Service Id: " + serviceId + "</h2>";
 
for (ServiceInstance serviceInstance : instances) {
  html += "<h3>Instance: " + serviceInstance.getUri() + "</h3>";
  html += "Host: " + serviceInstance.getHost() + "<br>";
  html += "Port: " + serviceInstance.getPort() + "<br>";
}
 
return html;
getServices metodu
Tüm servis isimlerini döner. 
Şöyle yaparız
List<String> serviceIds = this.discoveryClient.getServices();

Hiç yorum yok:

Yorum Gönder