微服务工程构建(七) Ribbon负载均衡服务

在这里插入图片描述
主要提供客户端的软件负载均衡算法和服务的调用
Ribbon官网
在这里插入图片描述
在这里插入图片描述
如果eureka-client有jar,就不用加了

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

config

       @Configuration
public class ApplicationContextConfig  {
    @Bean
    //@LoadBalanced  //赋予RestTemplate负载均衡的能力
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
 	@Resource
    private RestTemplate restTemplate;
    
 @GetMapping(value = "/order/payment/getEntity/{serial}")
    public Reslt<Payment> getEntity(@PathVariable("serial") String serial) {

        ResponseEntity<Reslt> forEntity = restTemplate.getForEntity(url + "/payment/getBySerial/" + serial, Reslt.class);
        if (forEntity.getStatusCode().is2xxSuccessful()) {
            return forEntity.getBody();
        } else {
            return new Reslt(404, "失败");
        }
    }

 @PostMapping(value = "/order/payment/insertEntity")
    public Reslt<Payment> insertEntity(Payment payment) {
        Reslt body = restTemplate.postForEntity(url + "/payment/insert", payment, Reslt.class).getBody();
        return body;
    }

2.Ribbon核心组件IRule
在这里插入图片描述
在替换访问服务的时候注意
在这里插入图片描述
在这里插入图片描述
添加Rule

@Configuration
public class MySelfRule  {

 @Bean
 public IRule myRule(){
     return new RandomRule();//设置随机
 }
}

添加启动类@RibbonClient

@SpringBootApplication
@EnableEurekaClient
//这里加入自己的规则,name名字是服务端的名字
@RibbonClient(name = "springcloud-payment-service",configuration = MySelfRule.class)
public class OrderMain80 {
   public static void main(String[] args) {
       SpringApplication.run(OrderMain80.class, args);
   }
}

3.负载均衡
在这里插入图片描述

服务端controller添加

@GetMapping(value = "/payment/lb")
public String getPaymentLB(){
    return serverPort;
}

客户端注释@LoadBalanced
ApplicationContextBean去掉@LoadBalanced
客户端 LoadBalanced 接口

public interface LoadBalanced {
    public ServiceInstance instance(List<ServiceInstance> serviceInstances);
}

客户端 实现

@Component
public class loadBalancedImpl implements LoadBalanced {
    private AtomicInteger atomicInteger = new AtomicInteger(0);
    @Resource
    private DiscoveryClient discoveryClient;

    //负载均衡算法:rest第几次请求书/服务器集群的总数=实际调用服务器位置的下标,
    @Override
    public ServiceInstance instance(List<ServiceInstance> serviceInstances) {  //得到机器的列表
        int index = getAndIncrement() % serviceInstances.size(); //得到服务器的下标位置
        return serviceInstances.get(index);
    }


    //坐标
    private 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;
    }

}

客户端 controller

@@GetMapping(value = "/consumer/payment/lb")
    public String getPaymentLB(){
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PAYMENT-SERVICE");
        if (instances == null || instances.size() <= 0){
            return null;
        }
        ServiceInstance serviceInstance = loadBalanced.instance(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri+"/payment/lb",String.class);
    }

END

代码下载.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值