一 Ribbon简介   
 Ribbon是Netflix发布的负载均衡器,它是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现。通过Spring Cloud的封装,可以让我们轻松地将面向服务的REST模版请求自动转换成客户端负载均衡的服务调用。
Spring Cloud Ribbon虽然只是一个工具类框架,它不像服务注册中心、配置中心、API网关那样需要独立部署,但是它几乎存在于每一个Spring Cloud构建的微服务和基础设施中。因为微服务间的调用,
API网关的请求转发等内容,实际上都是通过Ribbon来实现的,包括后续我们将要介绍的Feign,它也是基于Ribbon实现的工具。所以,对Spring Cloud Ribbon的理解和使用,对于我们使用Spring Cloud来构建微服务非常重要。
在Spring Cloub 项目中  使用  feign 的更多,默认集成了ribbon,写起来更加思路清晰和方便,下一篇 讲feign。
二 新建order_service项目
1 为RestTemplate添加注解@LoadBalanced
package unicom.com.cn.order_service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableFeignClients
public class OrderServiceApplication { public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} }
2.application.yml 的配置(注册中心一定要启动 Eureka)
server:
port: 8781
eureka:
client:
serviceUrl:
#在此指定服务注册中心地址
defaultZone: http://localhost:8761/eureka/
#在此指定服务的名字
spring:
application:
name: order-service
3.对应业务中的调用
@Service
public class ProductOrderServiceImpl implements ProductOrderService {
// 方式一
@Autowired
private RestTemplate restTemplate; @Override
public ProductOrder save(int userId, int productId) {
Map<String,Object> productMap = restTemplate.getForObject("http://product-service/api/v1/product/find?id="+productId, Map.class);
ProductOrder productOrder = new ProductOrder();
productOrder.setCreateTime(new Date());
productOrder.setUserId(userId);
productOrder.setTradeNo(UUID.randomUUID().toString());
productOrder.setProductName(productMap.get("name").toString());
productOrder.setPrice(Integer.parseInt(productMap.get("price").toString()));
return productOrder;
}
}
三 测试
1 启动eureka-server微服务
2 启动product_service微服务(和上面一样在配置文件中修改下端口就行8771)
3 启动product_service微服务(和上面一样在配置文件中修改下端口就行8772)
4启动order_service微服务(8781)
 
  测试结果

注册成功。

调用 order_service 中的方法是否成功

测试结果(多测试几次可以得出ribbon默认采用轮询的方式进行负载均衡)

四 修改ribbon默认采用轮询的方式(随机)

自定义负载均衡策略:http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html#_customizing_the_ribbon_client_by_setting_properties

在调用方的yml 文件中加上:

#自定义负载均衡策略(随机)
  product-service:
    ribbon:
      NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

5,策略选择:
1、如果每个机器配置一样,则建议不修改策略 (推荐)
2、如果部分机器配置强,则可以改为 WeightedResponseTimeRule

SpringCloud(二)之Ribbon的实现负载均衡的基本应用的更多相关文章

  1. SpringCloud 进阶之Ribbon和Feign(负载均衡)

    1. Ribbon 负载均衡 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端,负载均衡的工具; 1.1 Ribbon 配置初步 1.1.1 修改 micros ...

  2. springcloud第四步:ribbon搭建服务负载均衡

    使用ribbon实现负载均衡 启动两个会员服务工程,端口号分别为8762.8763,订单服务 使用负载均衡策略轮训到会员服务接口. 什么是ribbon ribbon是一个负载均衡客户端 类似nginx ...

  3. 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)

    在使用RestTemplate来消费spring boot的Restful服务示例中,我们提到,调用spring boot服务的时候,需要将服务的URL写死或者是写在配置文件中,但这两种方式,无论哪一 ...

  4. SpringCloud全家桶学习之Feign负载均衡----Feign(四)

    一.Feign概述 (1)Feign是什么? 官网地址:https://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-f ...

  5. Ribbon实现客户端负载均衡

    什么是Ribbon? 客户端负载均衡组件. 前期准备: 搭建一个Eureka集群和一个注册服务 https://www.cnblogs.com/noneplus/p/11374883.html 创建服 ...

  6. Ribbon提供的负载均衡算法IRule(四)

    一.Ribbon算法的介绍 Ribbon的源码地址:https://github.com/Netflix/ribbon IRule:根据特定算法中从服务器列表中选取一个要访问的服务,Ribbon默认的 ...

  7. 撸一撸Spring Cloud Ribbon的原理-负载均衡策略

    在前两篇<撸一撸Spring Cloud Ribbon的原理>,<撸一撸Spring Cloud Ribbon的原理-负载均衡器>中,整理了Ribbon如何通过负载均衡拦截器植 ...

  8. Ribbon自带负载均衡策略比较

    Ribbon自带负载均衡策略比较 策略名 策略声明 策略描述 实现说明 BestAvailableRule public class BestAvailableRule extends ClientC ...

  9. Ocelot(二)- 请求聚合与负载均衡

    Ocelot(二)- 请求聚合与负载均衡 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/10865511.html 源码地址: ...

  10. springcloud(二) 负载均衡器 ribbon

    代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo ribbon是一个负载均衡客户端 类似nginx反向代理,可 ...

随机推荐

  1. 针对nginx应用场景的配置 知识整理

    本文为转载,原文链接 前言 原本想写整理一篇针对nginx应用场景的相应配置,但发现已经有人整理了,而且写得非常不错,特意转过来 概论 Nginx 是一款面向性能设计的 HTTP 服务器,能反向代理 ...

  2. Packet for query is too large (4,544,730 > 4,194,304). You can change this value on the server by setting the 'max_allowed_packet' variable.

    修改 my.ini 加上 max_allowed_packet =6710886467108864=64M默认大小4194304  也就是4M修改完成之后要重启mysql服务,如果通过命令行修改就不用 ...

  3. Delphi 线程的同步

  4. 实时跟踪之TRACA

    背景: 目前,在实时跟踪领域存在着越来越多的先进方法,同时也极大地促进了该领域的发展.主要有两种不同的基于深度学习的跟踪方法:1.由在线跟踪器组成,这些跟踪器依赖网络连续的微调来学习目标的变化外观,精 ...

  5. u-boot从nand 启动时的问题解决记录

    u-boot从nand 启动时的问题解决记录 问题描述: 使用u-boot-1.1.6版本u-boot移植到JZ2440开发板上,当前已经能够从Nor启动,但是不能从Nand正常启动(u-boot大小 ...

  6. CF15E Triangles

    思路 有四种方法,L,R,L->R,只走上面的小三角形 然后组合方案数\(2f^2+8f+10\) 然后求f,递推一下就好啦(其实是太麻烦了) 时间和空间复杂度都是\(O(n)\) 代码 #in ...

  7. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 140: invalid continuation byte

    web阅片系统,遇到dicom文件在文件夹不能正常读取的问题.解决方法如下: def rep7(request): file_path = os.path.dirname(__file__) + re ...

  8. url传参特殊字符问题(+、%、#等)

    这样的话,你传的大多数带特殊符号的参数,都能在后台拿到,但是, url中可能用到的特殊字符及在url中的经过编码后的值:(此表格借鉴) 字符   特殊字符的含义 URL编码 #   用来标志特定的文档 ...

  9. CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

    按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...

  10. EXE中释放DLL中分配的内存

    在DLL中分配的内存,如果到其调用者中释放,可能会出现CRASH的情况,其原因在于: 在DLL中的Code Generation如果是采用了MT(静态加载LIBCRTD.LIB)在该库中维护了一个al ...