Netflix SpringCloud负载均衡 Ribbon Feign

Ribbon是什么

SpringCloudRibbon 是 继续Netflix Ribbon 实现的一套客户端负载均衡的工具

LB:负载均衡
方式:轮询,随机,权重

将用户的请求平摊的分配到多个服务上,从而达到系统的HA(高可用)

LB分为两种:

进程式LB:

将LB逻辑集成到消费方,由消费方从服务注册中心知道哪些地址可用,然后自己再从这些地址中选出一个合适的服务
Ribbon就属于进程式LB,他只是一个类库,继承与消费方进程,消费方通过它来获取服务器提供方的地址。

集中式LB:

像Nginx一样,在服务方和消费方中间作为一个独立的LB设置

Ribbon的使用

1、依赖加入Ribbon和Eureka

<!--Ribbon-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--Eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

2、在配置类里给RestTemplate加入注解

@Configuration
public class RestTempConfig {

  
    @Bean
    @LoadBalanced //配置负载均衡实现RestTemplate(Ribbon)
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }


}

3、在Controller中配置、Url前缀要使用调用者的Name,Ribbon才能帮助负载均衡

//消费者不应该有Service层
//RestTemplate 能够提供给我们支持Rest风格的请求,但是需要先注入到IOC中,写一个Config
@RestController
public class DeptConsumerController {

    @Autowired
    private RestTemplate restTemplate;//   提供多种便捷访问远程http服务的方法,简单的restful服务模板

    //  Ribbon 需要通过服务来访问,实现loadBalance
//    private static final String REST_URL_PREFIX = "http://localhost:8081";
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER";

    //使用restTemplate核心三个参数(url,实体(Map),responseType)

    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add",dept,Boolean.class);
    }

    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable Long id){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/"+id,Dept.class);
    }

    @RequestMapping("/consumer/dept/getAll")
    public List<Dept> getAll(){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/getAll",List.class);
    }
}

Ribbon的负载算法

算法:随机,轮询,权重

Ribbon默认算法: 轮询

AvailabilityFillteringRule:跳闸,过滤掉失效的服务,在有效的服务中轮询
在这里插入图片描述
自己选择Bibbon提供的负载均衡的办法,需要在@Configuration中注入到IOC中

@Bean
    public IRule myRule(){
        return new RandomRule();
    }

自定义RibbonClient客户端(自定义负载策略)

主启动类上增加代码

@RibbonClient(name = "SPRINGCLOUD-PROVIDER", configuration = MyRule.class)
public class ConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConsumerApplication.class, args);
	}

}

自定义Ribbon客户端类,可以返回自定义的算法

@Configuration
public class MyRule {

    @Bean
    public IRule myRule(){
//        return new MyRendom();
    }
}

注意:这个不应该在application上下文中,不要被主启动类扫描到,不能和主启动类放在同一包下
在这里插入图片描述


Feign

Feign和Ribbon不同之处在于:
Feign只需要调用接口和注解,类似于以前在Mapper类上增加@Mapper一样,现在是需要在微服务接口上添加一个Feign注解即可

Feign集成了Ribbon

1、增加依赖

<!--Feign-->
 <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>

2、创建Service接口

@FeignClient(value = "SPRINGCLOUD-PROVIDER")
@Component
public interface DeptClientService {

    @GetMapping("/dept/add")
    public boolean addDept(Dept dept);

    @RequestMapping("/dept/get/{id}")
    public Dept get(@PathVariable Long id);

    @RequestMapping("/dept/getAll")
    public List<Dept> getAll();
}

3、创建Controller

@RestController
public class FeignDeptController {
    @Autowired
    DeptClientService deptClientService;

    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        return deptClientService.addDept(dept);
    }

    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable Long id){
        return deptClientService.get(id);
    }

    @RequestMapping("/consumer/dept/getAll")
    public List<Dept> getAll(){
        return deptClientService.getAll();
    }
}

4、启动类增加注解

@EnableFeignClients(basePackages = {"com.bowen.springcloud"})
public class ConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConsumerApplication.class, args);
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值