spring cloud netflix Ribbon

一、Ribbon简介

    什么是Ribbon?简单讲它是Netflix发布的负载均衡器,当我们为Ribbon配置了服务地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求。Ribbon默认为我们提供了很多负载均衡算法,例如轮询、随机等。

    ribbon它其实就是为了完成客户端的负载均衡。

    ribbon主要就是对请求进行拦截。在拦截的过程中对请求进行解析,解析的步骤分为:服务列表的加载、负载均衡的计算。

二、通过demo来学习

  首先,我们需要一个“order项目”,我们通过部署多个订单系统来分散订单压力;其次我们需要一个“user项目”,在“user项目”上我们需要实现客户端的负载均衡。

1、order-service订单项目

新建一个spring项目,项目名称为“order-service”。

(1)、项目依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

(2)、application.properties配置文件

spring.application.name=order-service
server.port=8080

(3)、项目代码

package com.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @Value("${server.port}")
    private int port;

    @GetMapping("getOrder")
    public String getOrder(){
        return "获取订单信息,服务端口:"+port;
    }

}

订单项目构建好后,我们可以启动多个订单服务。IDEA启动多个服务

2、user-service用户系统

新建一个spring项目,项目名称为“user-service”。

(1)、项目依赖

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

(2)、application.properties配置文件

spring.application.name=user-service
server.port=8070

#配置指定服务提供者的列表
order-service.ribbon.listOfServers=\
  localhost:8080,localhost:8081

(3)、项目代码

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder){
        //RestTemplate默认不注入IOC容器,需要build注入
        return restTemplateBuilder.build();
    }

    @RequestMapping("getOrder")
    public String getOrder(){
        //order-service  订单服务系统application name
        return restTemplate.getForObject("http://order-service/getOrder",String.class);
    }
    
}

       至此Ribbon负载均衡demo项目代码就都全部分享完毕了。启动user-service项目,请求user-service的getOrder方法,就可以验证Ribbon客户端负载均衡的结果了。

     另外,我们也可以去掉@LoadBalanced注解,使用LoadBalancerClient来实现服务的负载均衡。详见一下代码:

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder){
        //RestTemplate默认不注入IOC容器,需要build注入
        return restTemplateBuilder.build();
    }
    
    @Autowired
    LoadBalancerClient loadBalancerClient;

    @RequestMapping("getOrder")
    public String getOrder(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("order-service");
        String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort()+"/getOrder");
        return restTemplate.getForObject(url,String.class);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值