1. 什么是负责均衡?

  负载均衡,就是分发请求流量到不同的服务器。

  负载均衡一般分为两种

  1. 服务器端负载均衡(nginx)

  2. 客户端负载均衡(Ribbon)

  

2. 服务提供者(spring-cloud-provider)

  实体类User:

  1. package com.wangx.cloud.model;
  2.  
  3. import java.util.Date;
  4. public class User {
  5.  
  6. private Integer id;
  7.  
  8. private String name;
  9.  
  10. private Date date;
  11.  
  12. public Integer getId() {
  13. return id;
  14. }
  15.  
  16. public void setId(Integer id) {
  17. this.id = id;
  18. }
  19.  
  20. public String getName() {
  21. return name;
  22. }
  23.  
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27.  
  28. public Date getDate() {
  29. return date;
  30. }
  31.  
  32. public void setDate(Date date) {
  33. this.date = date;
  34. }
  35.  
  36. @Override
  37. public String toString() {
  38. return "User{" +
  39. "id=" + id +
  40. ", name='" + name + '\'' +
  41. ", date=" + date +
  42. '}';
  43. }
  44. }

  Controller接口:

  1. package com.wangx.cloud.controller;
  2.  
  3. import com.wangx.cloud.model.User;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. import java.util.Date;
  10.  
  11. @RestController
  12. @RequestMapping("/api/user")
  13. public class UserController {
  14.  
  15. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  16. public User view(@PathVariable int id) {
  17. User user = new User();
  18. user.setId(id);
  19. user.setName("小张");
  20. user.setDate(new Date());
  21. System.out.println(user);
  22. return user;
  23. }
  24. }

3. 服务消费者(spring-cloud-consumer)

  

  1. package com.wangx.cloud.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.client.RestTemplate;
  9.  
  10. @RestController
  11. @RequestMapping(value = "/user", method = RequestMethod.POST)
  12. public class UserController {
  13.  
  14. private static final String URL = "http://localhost:7777/api/user/{id}";
  15.  
  16. @Autowired
  17. private RestTemplate restTemplate;
  18.  
  19. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  20. public String get(@PathVariable(value = "id") int id) {
  21. return restTemplate.getForObject(URL, String.class, id);
  22. }
  23. }

分别查看两个服务是否能正常调用。

4. 如何通过Ribbon进行调用

  1. 在创建RestTemplate bean的方法上添加注解@LoadBalanced

  

  1.   @Bean
  2. @LoadBalanced //默认的负载策略是轮询算法
  3. public RestTemplate restTemplate() {
  4. return new RestTemplate();
  5. }

  2. 修改调用的URL为URL=http://spring-cloud-provider/api/user/{id}

  注意:控制台的应用名为大写,我们统一为小写,更不能大小写都存在

5. 如何实现负载均衡

  启动多个提供者,进行测试

  为了区分,可以修改实体类User的name属性,

  注意:修改应用的时候,端口也要修改

6. 结论

  1. Ribbon通过@LoadBalanced进行负载均衡

  2.  默认的负载策略是轮询算法

  

SpringCloud学习笔记(6)----Spring Cloud Netflix之负载均衡-Ribbon的使用的更多相关文章

  1. SpringCloud学习笔记(8)----Spring Cloud Netflix之负载均衡-Ribbon的负载均衡的策略

    一. 内置 负载均衡策略的介绍的 IRule的实现类 2. 通过代码实现负载均衡 在第六节Riddom的使用的工程中,随机策略配置类 package com.wangx.cloud.springclo ...

  2. SpringCloud学习笔记(7)----Spring Cloud Netflix之负载均衡-Ribbon的深入理解

    1. 注解@LoadBalanced 作用:识别应用名称,并进行负载均衡. 2. 入口类:LoadBalancerAutoConfiguration 说明:类头上的注解可以知道Ribbon 实现的负载 ...

  3. 三、Spring Cloud之软负载均衡 Ribbon

    前言 上一节我们已经学习了Eureka 注册中心,其实我们也使用到了Ribbon ,只是当时我们没有细讲,所以我们现在一起来学习一下Ribbon. 什么是Ribbon 之前接触到的负载均衡都是硬负载均 ...

  4. spring cloud 之 客户端负载均衡 Ribbon

    一.负载均衡 负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性.其意 ...

  5. springCloud学习-消息总线(Spring Cloud Bus)

    1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...

  6. Spring-Cloud-Ribbon学习笔记(二):自定义负载均衡规则

    Ribbon自定义负载均衡策略有两种方式,一是JavaConfig,一是通过配置文件(yml或properties文件). 需求 假设我有包含A和B服务在内的多个微服务,它们均注册在一个Eureka上 ...

  7. 【Spring Cloud】客户端负载均衡组件——Ribbon(三)

    一.负载均衡 负载均衡技术是提高系统可用性.缓解网络压力和处理能力扩容的重要手段之一. 负载均衡可以分为服务器负载均衡和客户端负载均衡,服务器负载均衡由服务器实现,客户端只需正常访问:客户端负载均衡技 ...

  8. API网关spring cloud gateway和负载均衡框架ribbon实战

    通常我们如果有一个服务,会部署到多台服务器上,这些微服务如果都暴露给客户,是非常难以管理的,我们系统需要有一个唯一的出口,API网关是一个服务,是系统的唯一出口.API网关封装了系统内部的微服务,为客 ...

  9. Spring Cloud中的负载均衡策略

    在上篇博客(Spring Cloud中负载均衡器概览)中,我们大致的了解了一下Spring Cloud中有哪些负载均衡器,但是对于负载均衡策略我们并没有去详细了解,我们只是知道在BaseLoadBal ...

随机推荐

  1. com.sun.jdi.internalException:Unexpected JDWP Error:103////Method threw 'java.lang.IllegalArgumentEx

    retrofit2+RxJava2 的一些坑 今天开发新项目,本着积极向上的学习态度,经过多番考虑我决定使用retrofit2 + RxJava2来做为我的网络请求......神说:你的想法非常好 先 ...

  2. sqlhelper 数据库帮助操作类

    数据库帮助类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  3. luoguP4512 【模板】多项式除法 NTT+多项式求逆+多项式除法

    Code: #include<bits/stdc++.h> #define maxn 300000 #define ll long long #define MOD 998244353 # ...

  4. loging模块

    logging模块 什么是logging模块 logging模块是python提供的用于记录日志的模块 为什么需要logging 我们完全可以自己打开文件然后,日志写进去,但是这些操作重复且没有任何技 ...

  5. 数组的filter用法

    filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素. 语法:var new_array = arr.filter(callback(element[, index[, a ...

  6. 发个ZKW线段树板子测试一下代码高亮

    是我,Long time no see          --Jim 先安利 Wolves  歌手:Madilyn Bailey http://music.163.com/song/524149464 ...

  7. 移动设备safari不支持jquery的live绑定的解决方案

    给元素加上样式如下即可 <style> .btn{ cursor: pointer; } </style>

  8. 新手学python-Day4-作业

    购物车程序 要求: 1.启动程序后,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检查余额是否足够,够了就扣款,不够就提醒 4.可随时退出,退出时,打印已购买 ...

  9. JavaScript中的常用的数组操作方法

    JavaScript中的常用的数组操作方法 一.concat() concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,仅会返回被连接数组的一个副本. var arr1 = [1,2 ...

  10. Nodejs RESTFul架构实践之api篇(转)

    why token based auth? 此段摘自 http://zhuanlan.zhihu.com/FrontendMagazine/19920223 英文原文 http://code.tuts ...