springcloud-Ribbon负载均衡服务调用(四)
2021-05-03 10:27
标签:while scope compare 访问 工作 payment 订单 其他 object springcloud-Ribbon负载均衡服务调用(四) 标签:while scope compare 访问 工作 payment 订单 其他 object 原文地址:https://www.cnblogs.com/binwenhome/p/13189113.htmlRibbon入门介绍
Ribbon负载均衡演示
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-starter-netflix-ribbonartifactId>
version>2.2.1.RELEASEversion>
scope>compilescope>
dependency>
Ribbon核心组件IRule
@Configuration
public class MySelfRule {
@Bean
public IRule myRule() {
//将负载均衡策略改为随机
return new RandomRule();
}
}
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE", configuration = MySelfRule.class)
@EnableEurekaClient
@SpringBootApplication
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}
Ribbon负载均衡算法
@GetMapping("/payment/lb")
public String getPaymentLB() {
return serverPort;
}
public interface LoadBalancer {
ServiceInstance instances(List
@Component
public class MyLB implements LoadBalancer {
private AtomicInteger atomicInteger = new AtomicInteger(0);
public final int getAndIncrement() {
int current;
int next;
do {
current = this.atomicInteger.get();
next = current >= 2147483000 ? 0 : current+1;
} while(!this.atomicInteger.compareAndSet(current, next));
return next;
}
@Override
public ServiceInstance instances(List
@Resource
private RestTemplate restTemplate;
@Resource
private LoadBalancer loadBalancer;
@Resource
private DiscoveryClient discoveryClient;
@GetMapping("/consumer/payment/lb")
public String getPaymentLB() {
List
文章标题:springcloud-Ribbon负载均衡服务调用(四)
文章链接:http://soscw.com/essay/81751.html