当前位置:网站首页>Replace ribbon load balancer with loadbalancer
Replace ribbon load balancer with loadbalancer
2022-07-22 02:14:00 【Hu Shang】
SpringCloud LoadBalancer yes SpringCloud The official client load balancer , To replace Ribbon Of . because netfilx Stop changing , therefore SpringCloud The official has developed a client-side load balancer to replace Ribbon. But there is no way to replace Ribbon Of . Because there are few load balancing algorithms at present .
SpringCloud In the early days Ribbon As its load balancer , stay SpringCloud2021 Version was removed Ribbon, change to the use of sth. Loadbalancer Is the default load balancer
RestTemplate Integrate LoadBalancer
To eliminate ribbon rely on , introduce LoadBalancer Dependence
<dependencies> <!--nacos Service registration and discovery dependency --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <!-- To eliminate ribbon Dependence --> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </exclusion> </exclusions> </dependency> <!-- introduce SpringCloud LoadBalancer Dependence , Note that you need to be in the father first maven Introduced in the project SpringCloud Version manager for --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> </dependencies>
In the introduction of SpringCloud LoadBalancer Before the dependency of , Need to be in the father maven Introduced in the project SpringCloud Version manager for , It is better not to specify the version directly , Otherwise, it may cause version conflicts .
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
You need to put SpringCloud Medium ribbon Disable it , Because in the early days SpringCloud There is no standard load balancer , It is the ribbon As its standard , So the previous version SpringCloud Load balancer used by default ribbon, We need to disable it now . As if 2021 Year version of SpringCloud Not used ribbon 了
spring:
cloud:
loadbalancer:
ribbon:
enabled: false
spring.cloud.loadbalancer.ribbon.enable
Set to false
Finally, it needs to be in RestTemplate Add @LoadBalanced
annotation , This annotation ribbon Also need to add
@Configuration
public class RestConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder){
RestTemplate restTemplate = builder.build();
return restTemplate;
}
}
You can use it now RestTemplate To test and invoke the service ,LoadBalancer Its default load balancer is RoundRobinLoadBalancer
polling .
If you want to replace LoadBalancer The default load balancing mechanism is actually modified Ribbon In a similar way , It's just Ribbon Is to add a IRule
The object of , and LoadBalancer Is to add ReactorLoadBalancer
Object of interface type , as follows Official website The case in
public class CustomLoadBalancerConfiguration {
@Bean
ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new RandomLoadBalancer(loadBalancerClientFactory
.getLazyProvider(name, ServiceInstanceListSupplier.class),
name);
}
}
Then use in the configuration class or the main startup class @LoadBalancerClient
notes
@Configuration
@LoadBalancerClients({
@LoadBalancerClient(value = "stores", configuration = StoresLoadBalancerClientConfiguration.class), @LoadBalancerClient(value = "aaa", configuration = CustomersLoadBalancerClientConfiguration.class)
})
public class MyConfiguration {
@Bean
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}
}
边栏推荐
- rose
- 解决报错 OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized
- 解决GD32F207串口可以接收但发送00的问题
- 基于php开发的学生成绩管理系统
- When xshell connects, it displays "the server sent an unexpected packet. Received:3, expected:20
- js中批量修改对象中的属性值
- OSPF综合实验
- 电脑是怎样上网的 (三) 报文头封装和接入网与网络运营商
- M using Verilog to develop GMSK modulator based on FPGA under ISE platform
- Left ear mouse: five capabilities that developers in the cloud native era should have
猜你喜欢
解决报错 OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized
学习STM32,从HAL库的框架设计开始
3564. Date category
Pix2Seq:谷歌大脑提出 CV 任务统一接口!
【开发教程4】疯壳·人形街舞机器人-PC 上位机在线调试
6.< tag-动态规划和打家劫舍合集(树形DP)>lt.198.打家劫舍 + lt.213. 打家劫舍 II + lt.337. 打家劫舍 III dbc
Window functions in sparksql
The sum of the last three numbers
The fathers of programming languages are too bored to retire and choose to return to the workplace
3537. Tree lookup
随机推荐
Sentinel使用代码实现流控熔断降级规则
(闲暇)LeetCode9. Palindrome Number
M using Verilog to develop GMSK modulator based on FPGA under ISE platform
rose
基于php开发的学生成绩管理系统
On Newton iteration
[deep learning notes] attention mechanism
【开发教程6】AI语音人脸识别(会议记录仪/人脸打卡机)-串口
Filtration concept (feasibility analysis)
The architecture mode is excerpted from "happy when you smell defects" (this book can be downloaded for free)
When xshell connects, it displays "the server sent an unexpected packet. Received:3, expected:20
Objet js: détection de l'existence d'attributs
学习STM32,从HAL库的框架设计开始
[论文精读]Attention Is All You Need
excel if 判读单元格如果为空就不参与计算
Sentinel容错规则持久化
Hcip day 9 notes
沟通 节选自《闻缺陷则喜》(此书可免费下载)
H5网站接入微信支付(H5支付+JSAPI支付)
3511. Water pouring problem