Spring Cloud 学习笔记-04-Ribbon负载均衡

什么是Ribbon?
Spring Cloud Ribbon 是一个负载均衡解决方案,Ribbon是Netflix发布的负载均衡器,
Spring Cloud Ribbon是基于Netflix Ribbon实现的,是一个用于对HTTP请求进行控制
的负载均衡客户端。

在注册中心对Ribbon进行注册之后,Ribbon就可以基于某种负载均衡算法(轮询、随机等)自动帮助服务消费者调用接口。
Eureka Server提供所有可以调用的服务提供者列表,Ribbon基于特定的负载均衡算法
从这些服务提供者中选择要调用的具体实例。

代码实现

1.创建Module, 名字叫ribbon,pom.xml加入依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>
2.创建配置文件application.yml
server:
  port: 8040
spring:
  application:
    name: ribbon
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
3.创建启动类 
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class RibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run(RibbonApplication.class,args);
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

注解说明:
@LoadBalanced :声明一个基于Ribbon的负载均衡。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值