官网:https://github.com/Netflix/ribbon/wiki/Getting-Started
一句话就是:负载均衡+RestTemplate调用
总结:Ribbon其实就是一个软负载均衡的客户端组件,他可以和其他所需请求的客户端结合使用,和eureka结合只是其中的一个实例。
架构说明:
关于RestTemplate
https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html
getForObject方法/getForEntity方法
Ribbon核心组件IRule
IRule:根据特定算法从服务列表中选取一个要访问的服务
Ribbon自带的负载均衡规则:
默认是轮询
如何替换其他的规则?
注意:
故这个自定义类需要逃离主启动类所在的包重新建新的包
配置类:
package com.atguigu.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();//定义为随机规则
}
}
注意回看上面的7种规则图片
主启动类添加@RibbonClient
package com.atguigu.springcloud;
import com.atguigu.myrule.MySelfRule;
import javafx.application.Application;
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);
}
}
CLOUD-PAYMENT-SERVICE是Eureka中注册的服务提供者的服务名
MySelfRule是自定义的负载均衡规则类名
访问:http://localhost/consumer/payment/get/31(8001和8002之间是随机访问的)
Ribbon默认负载均衡轮询算法原理
手写轮询算法
注释掉Ribbon负载均衡功能
生产者 8001 8002 controller加上
@GetMapping(value = "/payment/lb")
public String getPaymentLB(){
return serverPort;
}
消费者添加接口(需要能够被扫描到)
package com.atguigu.springcloud.Ib;
import org.springframework.cloud.client.ServiceInstance;
import java.util.List;
public interface LoadBalancer {
ServiceInstance instances(List<ServiceInstance> serviceInstances);
}
接口的实现类
package com.atguigu.springcloud.Ib;
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);
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("*****第几次访问,次数next: "+next);
return next;
}
@Override
public ServiceInstance instances(List<ServiceInstance> serviceInstances) {
int index = getAndIncrement() % serviceInstances.size();
return serviceInstances.get(index);
}
}
消费者controller
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import com.atguigu.springcloud.Ib.LoadBalancer;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.net.URI;
import java.util.List;
@RestController
@Slf4j
public class OrderController
{
public static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
//public static final String PAYMENT_URL="http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@Resource
private LoadBalancer loadBalanced;
@Resource
private DiscoveryClient discoveryClient;
@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 = loadBalanced.instances(instances);
URI uri = serviceInstance.getUri();
System.out.println(uri);
return restTemplate.getForObject(uri+"/payment/lb",String.class);
}
}
访问:http://localhost/consumer/payment/lb