Giriş
Şu satırı dahil ederiz
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
Eureka Server'ı kullanarak Service Discovery yapar ve rest çağrısı yapar. Açıklaması şöyle
@LoadBalanced is a marker annotation. It is used to indicate that RestTemplate or WebClient should use a RibbonLoadBalancerClient or LoadBalancerClient for interacting with the services. In turn, this allows you to use “logical identifiers” for the URLs we pass to the RestTemplate or WebClient. These logical identifiers are typically the name of a service registered in service discovery.Note:- @RibbonClient(spring-cloud-starter-netflix-ribbon) and @LoadBalancerClient(spring-cloud-commons) are optional. This both works as a load-balancer on the client-side and gives us control over the HTTP and TCP clients. If we are using Service Discovery then we are good to go with default settings. we don't need to explicitly use these annotations.
Örnek
Açıklaması şöyle
Used as a marker annotation indicating that the annotated RestTemplate should use a RibbonLoadBalancerClient for interacting with your service(s).In turn, this allows you to use "logical identifiers" for the URLs you pass to the RestTemplate. These logical identifiers are typically the name of a service.
Şöyle yaparız. Burada some-service-name Eureka'da tanımlı logical identifier
restTemplate.getForObject("http://some-service-name/user/{id}", String.class, 1);
Şöyle yaparız
name Alanı@LoadBalanced@BeanRestTemplate getRestTemplate() {return new RestTemplate();}
Açıklaması şöyle
... if we are not using any service discovery or need to customize the settings of Ribbon or LoadBalancer for a particular client then we need to use any of the two.name - the service we are calling Ribbon or LoadBlancer but need additional customizations for how Ribbon or LoadBlancer interacts with that service.configuration - set it to an @Configuration class with all of our customizations defined as @Beans .
Şöyle yaparız
@Configuration @LoadBalancerClient (name ="custom-config", configuration = SomeCustomConfiguration.class) // @RibbonClient (name = "custom-config", configuration = SomeCustomConfiguration.class) public class WebClientConfigurationLB { }
Hiç yorum yok:
Yorum Gönder