SpringCloud学习笔记(6)----Spring Cloud Netflix之负载均衡-Ribbon的使用
1. 什么是负责均衡?
负载均衡,就是分发请求流量到不同的服务器。
负载均衡一般分为两种
1. 服务器端负载均衡(nginx)
2. 客户端负载均衡(Ribbon)
2. 服务提供者(spring-cloud-provider)
实体类User:
- package com.wangx.cloud.model;
- import java.util.Date;
- public class User {
- private Integer id;
- private String name;
- private Date date;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", date=" + date +
- '}';
- }
- }
Controller接口:
- package com.wangx.cloud.controller;
- import com.wangx.cloud.model.User;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import java.util.Date;
- @RestController
- @RequestMapping("/api/user")
- public class UserController {
- @RequestMapping(value = "/{id}", method = RequestMethod.GET)
- public User view(@PathVariable int id) {
- User user = new User();
- user.setId(id);
- user.setName("小张");
- user.setDate(new Date());
- System.out.println(user);
- return user;
- }
- }
3. 服务消费者(spring-cloud-consumer)
- package com.wangx.cloud.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
- @RestController
- @RequestMapping(value = "/user", method = RequestMethod.POST)
- public class UserController {
- private static final String URL = "http://localhost:7777/api/user/{id}";
- @Autowired
- private RestTemplate restTemplate;
- @RequestMapping(value = "/{id}", method = RequestMethod.GET)
- public String get(@PathVariable(value = "id") int id) {
- return restTemplate.getForObject(URL, String.class, id);
- }
- }
分别查看两个服务是否能正常调用。
4. 如何通过Ribbon进行调用
1. 在创建RestTemplate bean的方法上添加注解@LoadBalanced
- @Bean
- @LoadBalanced //默认的负载策略是轮询算法
- public RestTemplate restTemplate() {
- return new RestTemplate();
- }
2. 修改调用的URL为URL=http://spring-cloud-provider/api/user/{id}
注意:控制台的应用名为大写,我们统一为小写,更不能大小写都存在
5. 如何实现负载均衡
启动多个提供者,进行测试
为了区分,可以修改实体类User的name属性,
注意:修改应用的时候,端口也要修改
6. 结论
1. Ribbon通过@LoadBalanced进行负载均衡
2. 默认的负载策略是轮询算法
SpringCloud学习笔记(6)----Spring Cloud Netflix之负载均衡-Ribbon的使用的更多相关文章
- SpringCloud学习笔记(8)----Spring Cloud Netflix之负载均衡-Ribbon的负载均衡的策略
一. 内置 负载均衡策略的介绍的 IRule的实现类 2. 通过代码实现负载均衡 在第六节Riddom的使用的工程中,随机策略配置类 package com.wangx.cloud.springclo ...
- SpringCloud学习笔记(7)----Spring Cloud Netflix之负载均衡-Ribbon的深入理解
1. 注解@LoadBalanced 作用:识别应用名称,并进行负载均衡. 2. 入口类:LoadBalancerAutoConfiguration 说明:类头上的注解可以知道Ribbon 实现的负载 ...
- 三、Spring Cloud之软负载均衡 Ribbon
前言 上一节我们已经学习了Eureka 注册中心,其实我们也使用到了Ribbon ,只是当时我们没有细讲,所以我们现在一起来学习一下Ribbon. 什么是Ribbon 之前接触到的负载均衡都是硬负载均 ...
- spring cloud 之 客户端负载均衡 Ribbon
一.负载均衡 负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性.其意 ...
- springCloud学习-消息总线(Spring Cloud Bus)
1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...
- Spring-Cloud-Ribbon学习笔记(二):自定义负载均衡规则
Ribbon自定义负载均衡策略有两种方式,一是JavaConfig,一是通过配置文件(yml或properties文件). 需求 假设我有包含A和B服务在内的多个微服务,它们均注册在一个Eureka上 ...
- 【Spring Cloud】客户端负载均衡组件——Ribbon(三)
一.负载均衡 负载均衡技术是提高系统可用性.缓解网络压力和处理能力扩容的重要手段之一. 负载均衡可以分为服务器负载均衡和客户端负载均衡,服务器负载均衡由服务器实现,客户端只需正常访问:客户端负载均衡技 ...
- API网关spring cloud gateway和负载均衡框架ribbon实战
通常我们如果有一个服务,会部署到多台服务器上,这些微服务如果都暴露给客户,是非常难以管理的,我们系统需要有一个唯一的出口,API网关是一个服务,是系统的唯一出口.API网关封装了系统内部的微服务,为客 ...
- Spring Cloud中的负载均衡策略
在上篇博客(Spring Cloud中负载均衡器概览)中,我们大致的了解了一下Spring Cloud中有哪些负载均衡器,但是对于负载均衡策略我们并没有去详细了解,我们只是知道在BaseLoadBal ...
随机推荐
- com.sun.jdi.internalException:Unexpected JDWP Error:103////Method threw 'java.lang.IllegalArgumentEx
retrofit2+RxJava2 的一些坑 今天开发新项目,本着积极向上的学习态度,经过多番考虑我决定使用retrofit2 + RxJava2来做为我的网络请求......神说:你的想法非常好 先 ...
- sqlhelper 数据库帮助操作类
数据库帮助类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
- luoguP4512 【模板】多项式除法 NTT+多项式求逆+多项式除法
Code: #include<bits/stdc++.h> #define maxn 300000 #define ll long long #define MOD 998244353 # ...
- loging模块
logging模块 什么是logging模块 logging模块是python提供的用于记录日志的模块 为什么需要logging 我们完全可以自己打开文件然后,日志写进去,但是这些操作重复且没有任何技 ...
- 数组的filter用法
filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素. 语法:var new_array = arr.filter(callback(element[, index[, a ...
- 发个ZKW线段树板子测试一下代码高亮
是我,Long time no see --Jim 先安利 Wolves 歌手:Madilyn Bailey http://music.163.com/song/524149464 ...
- 移动设备safari不支持jquery的live绑定的解决方案
给元素加上样式如下即可 <style> .btn{ cursor: pointer; } </style>
- 新手学python-Day4-作业
购物车程序 要求: 1.启动程序后,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检查余额是否足够,够了就扣款,不够就提醒 4.可随时退出,退出时,打印已购买 ...
- JavaScript中的常用的数组操作方法
JavaScript中的常用的数组操作方法 一.concat() concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,仅会返回被连接数组的一个副本. var arr1 = [1,2 ...
- Nodejs RESTFul架构实践之api篇(转)
why token based auth? 此段摘自 http://zhuanlan.zhihu.com/FrontendMagazine/19920223 英文原文 http://code.tuts ...