时间:2024-6-20 作者:老大夫 分类: SpringCloud
平替不再更新的Ribbon
它有多种实现类
Spring RestTemplate as a LoadBalancer Client
Spring RestClient as a LoadBalancer Client
Spring WebClient as a LoadBalancer Client
Spring WebFlux WebClient with ReactorLoadBalancerExchangeFilterFunction
常用RestTemplate
<!--loadbalancer-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
@GetMapping("/pay/get/info")
public String getInfoByConsul(){
return restTemplate.getForObject(PaymentSrv_URL+"/pay/get/info",String.class);
}
@Configuration
@LoadBalancerClient(
//下面的value值大小写一定要和consul里面的名字一样,必须一样
value = "cloud-payment-service",configuration = RestTemplateConfig.class)
public class RestTemplateConfig
{
@Bean
@LoadBalanced //使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
public RestTemplate restTemplate(){
return new RestTemplate();
}
@Bean
ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
}
}
推荐阅读:
OpenFeign重试机制
OpenFeign日志打印功能
OpenFeign更换HTTPClient提升性能
OpenFeign请求压缩功能
gateway 报错:Unable to find RoutePredicateFactory with name My
Resilience4J的CircuitBreaker断路器之熔断
Resilience4J的BulkHead舱壁隔离
OpenFeign超时控制功能
四种限流算法
报错:I/O error on POST request for "http://localhost:9411/api/v2/spans": Connect to http://localhost:9411 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: no further information
行动消除疑虑