Ribbon负载均衡

1 篇文章 0 订阅

Ribbon:LB+RestTemplate
使用Eurake做服务注册中心时,客户端依赖就已经帮助我们将Ribbon所需要的依赖引入了进来
在这里插入图片描述

在使用springcloud ribbon客户端负载均衡的时候,可以给RestTemplate bean 加一个@LoadBalanced注解,就能让这个RestTemplate在请求时拥有客户端负载均衡的能力:
在这里插入图片描述

RestTemplate中常用方法:
在这里插入图片描述
getForObject与getForEntity的区别:
![在这里插入图片描述](https://img-blog.csdnimg.cn/bb17ea06b00a427e808104b602f8ec58.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5Lmf6K645LuK5aSp,size_20,color_FFFFFF,t_70,g_se,x_16
Ribben默认负载均衡策略为:轮询
负载均衡器Ribbon中的核心组件IRule负责选择什么样的负载均衡算法
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述注意:@ComponentScan 的作用就是根据定义的扫描路径,把符合扫描规则的类装配到spring容器中,注解定义如下。比如在启动类上就有此注解。

步骤一:在客户端消费方下创建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();  //定义为随机
    }
}

步骤二: 在启动类上添加@RibbonClient注解,告知系统消费方在访问提供方的时候要使用那种负载衡策略

package com.atguigu.springcloud;

//import com.atguigu.myrule.MySelfRule;
import com.atguigu.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;
//import org.springframework.cloud.netflix.ribbon.RibbonClient;

@EnableEurekaClient
@SpringBootApplication
@RibbonClient(name="CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值