当前位置:网站首页>Ribbon load balancing service call
Ribbon load balancing service call
2022-06-26 05:40:00 【RB_ VER】
summary
spring cloud ribbon Is based on Netflix Ribbon Implement a set of client load balancing tools .
In short ,ribbon yes Netflix Published open source projects , The main function is to provide the software load balancing algorithm and service call of the client .ribbon The client component provides a complete set of configuration items such as connection timeouts 、 Retry etc. . In short , This is listed in the configuration file load balancer( abbreviation LB) All the machines in the back ,Ribbon Will automatically help you based on certain rules ( Such as simple polling , Random connection, etc ) To connect the machines . It's easy for us to use Ribbon Implement custom load balancing algorithm .
Load balancing is to evenly distribute users' requests to multiple services , So as to achieve systematic HA( High availability ). Common load balancing software includes Nginx、LVS etc. .
Nginx It's server load balancing , All client requests are delivered to Nginx, Then from Nginx Implement forwarding request . That is to say, the load balance is realized by the server .( Centralized LB)
Ribbon It's local load balancing , When calling the microservice interface , The registration information service list will be retrieved from the registry and then cached to JVM Local , So that it can be implemented locally RPC Remote procedure call technology .( In process LB)
Centralized LB: I.e. use independent... Between consumers and providers of services LB facilities ( It can be hardware , Such as F5, It can also be software , Such as Nginx), The facility is responsible for forwarding access requests through some policy to the service provider .
In process LB: take LB Logic is integrated into the consumer , The consumer knows from the service registry what addresses are available , Then I choose a suitable server from these addresses .
Ribbon Load balancing and Rest call
Ribbon It's actually a load balanced client component , It can be used in conjunction with other clients that need to request .

Ribbon Working time: two steps :
The first step is to choose Eureka Server, It gives priority to those who are less responsible in the same area server.
The second step is according to the policy specified by the user , In from server Select an address from the service registration list obtained .
spring-cloud-starter-netflix-eureka-client Bring it with you spring-cloud-starter-ribbon quote .
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Ribbon Default built-in load rules
IRule Interface : Select a service to access from the list of services according to the specific algorithm .
Realization IRule The built-in common rules of the interface :
- com.netflix.loadbalancer.RoundRobinRule: polling .( Default )
- com.netflix.loadbalancer.RandomRule: Random .
- com.netflix.loadbalancer.RetryRule: First according to RoundRobinRule The strategy to get services , If it fails to get the service, it will try again within the specified time , Get available services .
- WeightedResponseTimeRule: Yes RoundRobinRule An extension of , The faster the response speed, the greater the weight of instance selection , The easier it is to be chosen .
- BestAvailableRule: Services in the breaker trip state due to multiple access failures will be filtered out first , Then choose a service with the least amount of concurrency .
- AvailabilityFilteringRule: Filter the fault instance first , Then select the instance with smaller concurrency .
- ZoneAvoidanceRule: The default rules , Composite judgment server The performance of the region server The availability of select server .
Load rule replacement
Custom configuration class cannot be placed in @ComponentScan Under the current package and sub package scanned , Otherwise, our custom configuration class will be owned by all Ribbon Shared by the client , Can't achieve the purpose of special customization .
cloud-consumer-order80 Module directory structure :
MySelfRule
package com.qrxqrx.myrule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MySelfRule {
@Bean
public IRule myRule() {
return new RandomRule();
}
}
OrderMain80
package com.qrxqrx.springcloud;
import com.qrxqrx.myrule.MySelfRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
Ribbon Principle of default load balancing algorithm
rest The number of requests for the interface % Total number of server clusters = The actual call server location subscript
After every service restart rest Interface count from 1 Start .
RoundRobinRule Source code analysis
RandomRule Of choose Method :
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
log.warn("no load balancer");
return null;
} else {
Server server = null;
int count = 0;
while(true) {
if (server == null && count++ < 10) {
List<Server> reachableServers = lb.getReachableServers();
List<Server> allServers = lb.getAllServers();
int upCount = reachableServers.size();
int serverCount = allServers.size();
if (upCount != 0 && serverCount != 0) {
int nextServerIndex = this.incrementAndGetModulo(serverCount);
server = (Server)allServers.get(nextServerIndex);
if (server == null) {
Thread.yield();
} else {
if (server.isAlive() && server.isReadyToServe()) {
return server;
}
server = null;
}
continue;
}
log.warn("No up servers available from load balancer: " + lb);
return null;
}
if (count >= 10) {
log.warn("No available alive servers after 10 tries from load balancer: " + lb);
}
return server;
}
}
}
Ribbon Handwritten polling algorithm
stay cloud-provider-payment8001 and cloud-provider-payment8002 Revision in China controller
@GetMapping(value = "/payment/lb")
public String getPaymentLB() {
return serverPort;
}
modify cloud-consumer-order80 modular , Comment out the configuration class @LoadBalanced
@Configuration
public class ApplicationContextConfig {
@Bean
//@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
Realization LoadBalancer
package com.qrxqrx.springcloud.lb;
import org.springframework.cloud.client.ServiceInstance;
import java.util.List;
public interface LoadBalancer {
ServiceInstance instances(List<ServiceInstance> serviceInstances);
}
Realization
package com.qrxqrx.springcloud.lb;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@Component
public class MyLB implements LoadBalancer {
private AtomicInteger atomicInteger = new AtomicInteger(0);
@Override
public ServiceInstance instances(List<ServiceInstance> serviceInstances) {
int index = getAndIncrement() % serviceInstances.size();
return serviceInstances.get(index);
}
public final int getAndIncrement() {
int current;
int next;
do {
current = this.atomicInteger.get();
next = current >= 2147483647 ? 0 : current + 1;
} while(this.atomicInteger.compareAndSet(current,next));
System.out.println(" The first "+next+" visit ");
return next;
}
}
modify OrderController
@GetMapping(value = "/consumer/payment/lb")
public String getPaymentLB() {
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
if (instances == null || instances.size() <= 0) {
return null;
}
ServiceInstance serviceInstance = loadBalancer.instances(instances);
URI uri = serviceInstance.getUri();
return restTemplate.getForObject(uri+"/payment/lb",String.class);
}
边栏推荐
- cross entropy loss = log softmax + nll loss
- 10 set
- The most refined language interprets the event dispatcher (also known as the event scheduler)
- About abstact and virtual
- The State Council issued a document to improve the application of identity authentication and electronic seals, and strengthen the construction of Digital Government
- Daily production training report (17)
- 2021年OWASP-TOP10
- A new explanation of tcp/ip five layer protocol model
- Consul服务注册与发现
- A new journey
猜你喜欢

Uni app ceiling fixed style

原型模式,咩咩乱叫

慢慢学JVM之缓存行和伪共享
![[arm] add desktop application for buildreoot of rk3568 development board](/img/9a/28015cdea7362261c39ffc7f6e13a9.png)
[arm] add desktop application for buildreoot of rk3568 development board

Gram 矩阵

Serious hazard warning! Log4j execution vulnerability is exposed!

The difference between get and post in small interview questions

Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?

【ARM】在NUC977上搭建基于boa的嵌入式web服务器

工厂方法模式、抽象工厂模式
随机推荐
Learn cache lines and pseudo sharing of JVM slowly
1212312321
cartographer_ backend_ constraint
机器学习 05:非线性支持向量机
Henkel database custom operator '~~‘
适配器模式
FindControl的源代码
Introduction to GUI programming to game practice (II)
Daily production training report (16)
Ribbon负载均衡服务调用
pytorch(网络模型)
bingc(继承)
Redis usage and memory optimization
[arm] build boa based embedded web server on nuc977
Two step processing of string regular matching to get JSON list
DOM文档
How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
最后一次飞翔
REUSE_ ALV_ GRID_ Display event implementation (data_changed)
机器学习 07:PCA 及其 sklearn 源码解读