Spring-Cloud-Ribbon-Load Balancing

Spring-Cloud-Ribbon realizes load balancing (Ribbon is used when the client requests the server, so it needs to be configured on the client)
Ribbon core object: realize load balancing through restTemplate

1. Add ribbon client support
<!-- Realize load Balance the introduction of the client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
2. Add
@SpringBootApplication
@ to the startup class EnableEurekaClient
@EnableCircuitBreaker
public class RequestApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(RequestApp.class, args);
}

@Bean
@LoadBalanced
public RestTemplate newRestTemplate(){
return new RestTemplate();
}
}
3. Call the server layer to inject restTemplate and
call resultTemplate related methods to initiate requests such as url = "http://server-ribbonservice/ribbonInfo" (server-ribbonservice is the service name) responseType is the return value type
restTemplate.getForObject(url, responseType);
restTemplate.postForObject(url, request, responseType);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325129502&siteId=291194637