1.Ribbon概述

实际环境中,我们往往会开启很多个itcast-service-provider的集群。此时我们获取的服务列表中就会有多个,到底该访问哪一个呢?

Eureka中已经帮我们集成了负载均衡组件:Ribbon,简单修改代码即可使用。

2.负载均衡入门案例

(1)启动两个LuckyServiceProviderApplication实例,一个8081,一个8082。

注意:启动第2个Application时需要修改yml文件中的端口配置。

Eureka监控面板:http://localhost:10086/

(2)开启负载均衡

因为Eureka中已经集成了Ribbon,所以我们无需引入新的依赖,直接修改代码。
修改lucky-service-consumer的引导类,在RestTemplate的配置方法上添加`@LoadBalanced`注解:

  1. package lucky.service;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.web.client.RestTemplate;
  9.  
  10. @SpringBootApplication
  11. @EnableDiscoveryClient
  12. public class LuckyServiceConsumerApplication {
  13.  
  14. @Bean
  15. @LoadBalanced
  16. public RestTemplate restTemplate() {
  17. return new RestTemplate();
  18. }
  19.  
  20. public static void main(String[] args) {
  21. SpringApplication.run(LuckyServiceConsumerApplication.class, args);
  22. }
  23.  
  24. }

修改调用方式,不再手动获取ip和端口,而是直接通过服务名称调用:

  1. package lucky.service.controller;
  2.  
  3. import lucky.service.domain.Users;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.cloud.client.ServiceInstance;
  6. import org.springframework.cloud.client.discovery.DiscoveryClient;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import org.springframework.web.client.RestTemplate;
  12.  
  13. import java.util.List;
  14.  
  15. @Controller
  16. @RequestMapping(path = "/consumer/user")
  17. public class UserController {
  18. @Autowired
  19. private RestTemplate restTemplate;
  20.  
  21. // @Autowired
  22. // private DiscoveryClient discoveryClient; // eureka客户端,可以获取到eureka中服务的信息
  23.  
  24. @RequestMapping(path = "/queryUsersById")
  25. @ResponseBody
  26. public Users queryUserById(@RequestParam("id") Integer id){
  27.  
  28. /*// 根据服务名称,获取服务实例。有可能是集群,所以是service实例集合
  29. List<ServiceInstance> instances = discoveryClient.getInstances("service-provider");
  30. // 因为只有一个Service-provider。所以获取第一个实例
  31. ServiceInstance instance = instances.get(0);*/
  32.  
  33. // 获取ip和端口信息,拼接成服务地址
  34. String baseUrl = "http://SERVICE-PROVIDER/users/queryUsersById?id=" + id;
  35. Users user = this.restTemplate.getForObject(baseUrl, Users.class);
  36. return user;
  37. }
  38.  
  39. }

访问页面,查看结果:

008 SpringCloud 学习笔记4-----Ribbon负载均衡的更多相关文章

  1. spring cloud学习笔记二 ribbon负载均衡

    Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为.为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求.Ribb ...

  2. spring-cloud: eureka之:ribbon负载均衡自定义配置(二)

    spring-cloud: eureka之:ribbon负载均衡自定义配置(二) 有默认配置的话基本上就是轮询接口,现在我们改用自定义配置,同时支持:轮询,随机接口读取 准备工作: 1.eureka服 ...

  3. spring-cloud: eureka之:ribbon负载均衡配置(一)

    spring-cloud: eureka之:ribbon负载均衡配置(一) 比如我有: 一个eureka服务:8761 两个user用户服务: 7900/7901端口 一个movie服务:8010 1 ...

  4. SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)

    1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现 ...

  5. SpringCloud学习笔记(2)——Ribbon

    参考SpringCloud官网第16.17章 16. Client Side Load Balancer: Ribbon Ribbon是一个客户端的负载均衡器,它提供对大量的HTTP和TCP客户端的访 ...

  6. SpringCloud与微服务Ⅵ --- Ribbon负载均衡

    一.Ribbon是什么 Sping Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具. 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户 ...

  7. SpringCloud微服务之Ribbon负载均衡(一)

    什么是微服务?什么是SpringCloud? 微服务是一种架构的模式,它提倡将一个应用程序划分成很多个微小的服务,服务与服务之间相互协调.相互配合.每个服务运行都是一个独立的进程,服务与服务之间采用轻 ...

  8. Spring Cloud微服务开发笔记5——Ribbon负载均衡策略规则定制

    上一篇文章单独介绍了Ribbon框架的使用,及其如何实现客户端对服务访问的负载均衡,但只是单独从Ribbon框架实现,没有涉及spring cloud.本文着力介绍Ribbon的负载均衡机制,下一篇文 ...

  9. Web负载均衡学习笔记之实现负载均衡的几种实现方式

    0x00 概要 负载均衡(Load Balance)是集群技术(Cluster)的一种应用.负载均衡可以将工作任务分摊到多个处理单元,从而提高并发处理能力.目前最常见的负载均衡应用是Web负载均衡.根 ...

随机推荐

  1. Python I/O编程 --读写文件、StringIO/ BytesIO

    I/O编程 Input/Output  输入/输出 Stream(流)是一个很重要的概念,可以把流想象成一个水管,数据就是水管里的水 Input Stream就是数据从外面(磁盘.网络)流进内存,Ou ...

  2. JavaScript兼容性问题 js兼容

    1.获取事件对象: var e=e||event; 2.阻止冒泡: e:stopPropagation?e:stopPropagation():e.cancelBubble=true; 3.阻止浏览器 ...

  3. PHP常用的魔术方法及规则

    1. __construct 具有构造函数的类会在每次创建新对象时先调用此方法;初始化工作执行.2. __desstruct 对象的所有引用都被删除或者当对象被显式销毁时执行.3.__call()在对 ...

  4. 网络编程——select介绍

    一.select函数简介 select一般用在socket网络编程中,在网络编程的过程中,经常会遇到许多阻塞的函数,网络编程时使用的recv, recvfrom.connect函数都是阻塞的函数,当函 ...

  5. win10中通过Anaconda安装tensorflow时报错Traceback (most recent call last): File “E:\Anaconda3\lib\site-packages\pip_vendor\urllib3\response.py”, line 360, in _error_catcher yield

    问题:通过默认镜像安装,下载过程中可能会报错,下载安装失败 Traceback (most recent call last): File “E:\Anaconda3\lib\site-package ...

  6. 修改Docker容器启动配置参数

    有时候,我们创建容器时忘了添加参数 --restart=always ,当 Docker 重启时,容器未能自动启动, 现在要添加该参数怎么办呢,方法有二: 1.Docker 命令修改 docker c ...

  7. kubernetes --- Glusterfs

    gluster配额管理gluster volume quota cloud enablegluster volume quota cloud limit-usage /mail/pbs 20MBdd ...

  8. 权重轮询调度算法(WeightedRound-RobinScheduling)

    权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现 ----参考Nginx中负载均衡算法实现 这里主要参考这篇文章的实现: Nginx 负载均衡-加权轮询策略 ...

  9. eventlet 模块搭建 WEB 服务器

    eventlet这个强悍的东东,看到我同事的一些整理.故贴出来,大家一起分享~ motivation 114.113.199.11服务器上nova服务中基于python eventlet实现的定时任务 ...

  10. Spark2.x(六十一):在Spark2.4 Structured Streaming中Dataset是如何执行加载数据源的?

    本章主要讨论,在Spark2.4 Structured Streaming读取kafka数据源时,kafka的topic数据是如何被执行的过程进行分析. 以下边例子展开分析: SparkSession ...