pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
Product Service calling Pricing Service
@Bean
@LoadBalanced // Ribbon intercepts this WebClient / RestTemplate
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
public String getPrice(String productId) {
// Ribbon chooses one of the pricing-service instances from Eureka
return restTemplate.getForObject("<http://pricing-service/price/>" + productId, String.class);
}
Visual Flow:
[Product Service] ---(Ribbon chooses instance)--> [Pricing Service: 8081 or 8082]
^ ^
|<-------------Service Registry (Eureka)-----|
application.yml
pricing-service:
ribbon:
listOfServers: localhost:8081,localhost:8082