使用ribbon之前的准备工作:

  1.你要有两个服务,一个是服务消费方(下图的xing-movie是消费方),一个是服务提供方(xing-user是服务提供者),并且服务提供方要有两个实例,也就是xing-user有两个实例,分别运行在8070和8071端口。

  2.所有服务注册到eureka server中。

    

接下来我们在movie服务使用ribbon实现负载均衡调用中调用user服务,spring cloud使用ribbon很简单

  1.引入依赖,因为user服务的pom.xml中引入了eureka的依赖,eureka的依赖包含了ribbon的依赖所以这一步省略

  2.在movie服务的启动类中加入标红的代码,加入是为了在之后调用的时候可以直接@Autowired  restTemplate,顺便提醒一下,@Autowired是 byType的,不是byName:

  

package com.xing.movie;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableEurekaClient
public class SpringDemoApplication { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringDemoApplication.class, args);
}
}

  启动类写好之后我们去controller类中写调用user服务的方法:

     @Autowired
    private RestTemplate restTemplate;

   @ApiOperation(value = "查询用户", notes = "查询用户")//方法说明
@ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Movie.class)})//响应数据说明,可以有多个
@ApiImplicitParam(name = "name", value = "用户名", paramType = "path", required = true, dataType = "String")
@GetMapping(value = "/findUerByName/{name}",produces = { "application/json;charset=UTF-8" })
public User findUserByName(@PathVariable String name) {
return this.restTemplate.getForObject("http://xing-user/user/findByName/"+name, User.class);
}

  标红的地方要注意使用的是user服务的spring.application.name,这里使用到的user服务在上一篇博客中有具体的代码,最后一行代码中的User.class是返回类型,如果报错那就去user服务中把User类拷贝过来,到目前为止ribbon的简答的负载均衡就搞定了,查看结果

可以看到已经成功的调用到了user服务,并返回了数据,这个时候我们只能说服务调用成功并不能说负载均衡。这个网页我们多刷新几遍,多做几次请求,之后去查看后台数据会发现user1中的控制台打印了数据,user2的控制台也打印了数据,说明负载均衡成功。

简单提一下spring cloud中ribbon的负载均衡算法默认是轮询。那么我们不想使用轮询怎么自定义呢?

我们首先需要创建一个class,根据官方文档这个类必须在启动类能扫描的范围之外,所以这个class定义的位置如下:

RibbonConfig.java:

package com.xing.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule; /**
* Copyright: Copyright (c) 2018 LanRu-Caifu
* @ClassName: RibbonConfig.java
* @Description: 自定义ribbon配置类
* @version: v1.0.0
* @author: zjx
* @date: 2018年11月2日 上午10:08:36
*/ @Configuration
public class RibbonConfig { @Autowired
IClientConfig config; @Bean
@ConditionalOnMissingBean
public IRule ribbonRule(IClientConfig config) {
System.out.println("-------ribbon自定义随机------");
return new RandomRule();
}
}

RibbonConfig.java

movie服务的启动类修改之后如下:

package com.xing.movie;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; import com.xing.config.RibbonConfig; @SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "xing-user" , configuration = RibbonConfig.class)
public class SpringDemoApplication { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringDemoApplication.class, args);
}
}

SpringDemoApplication

最后启动类测试,做了4次请求,user1打印了3次,user2打印了1次

还有其他的很多种配置方式,这里只简单写出这种,以后有空还会补充配置文件的方式等等

源码地址:https://github.com/OnlyXingxing/SpringCloud

使用ribbon实现负载均衡的更多相关文章

  1. Spring Cloud Ribbon——客户端负载均衡

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

  2. Spring Cloud:使用Ribbon实现负载均衡详解(下)

    在上一篇文章(Spring Cloud:使用Ribbon实现负载均衡详解(上))中,我对 Ribbon 做了一个介绍,Ribbon 可以实现直接通过服务名称对服务进行访问.这一篇文章我详细分析一下如何 ...

  3. springcloud(十二):Ribbon客户端负载均衡介绍

    springcloud(十二):Ribbon客户端负载均衡介绍 Ribbon简介 使用分布式微服务脚骨的应用系统,在部署的时候通常会为部分或者全部微服务搭建集群环境,通过提供多个实例来提高系统的稳定型 ...

  4. spring cloud 系列第3篇 —— ribbon 客户端负载均衡 (F版本)

    源码仓库地址:https://github.com/heibaiying/spring-samples-for-all 一.ribbon 简介 ribbon是Netfix公司开源的负载均衡组件,采用服 ...

  5. Spring Cloud Ribbon客户端负载均衡(四)

    序言 Ribbon 是一个客户端负载均衡器(Nginx 为服务端负载均衡),它赋予了应用一些支配 HTTP 与 TCP 行为的能力,可以得知,这里的客户端负载均衡也是进程内负载均衡的一种.它在 Spr ...

  6. springcloud 之Ribbon客户端负载均衡配置使用

    pom.xml添加配置说明:这里服务注册与发现用的是Eureka,所以消费者端需要引入eureka,使用EurekaClient来调用服务 <dependency> <groupId ...

  7. Ribbon【负载均衡策略】

    ribbon有7种负载均衡策略可供选择: 策略类 命名 描述 RandomRule 随机策略 随机选择server RoundRobinRule 轮询策略 按照顺序选择server(ribbon默认策 ...

  8. 【一起学源码-微服务】Feign 源码三:Feign结合Ribbon实现负载均衡的原理分析

    前言 前情回顾 上一讲我们已经知道了Feign的工作原理其实是在项目启动的时候,通过JDK动态代理为每个FeignClinent生成一个动态代理. 动态代理的数据结构是:ReflectiveFeign ...

  9. SpringCloud微服务实战——搭建企业级开发框架(十二):OpenFeign+Ribbon实现负载均衡

      Ribbon是Netflix下的负载均衡项目,它主要实现中间层应用程序的负载均衡.为Ribbon配置服务提供者地址列表后,Ribbon就会基于某种负载均衡算法,自动帮助服务调用者去请求.Ribbo ...

随机推荐

  1. SpringCloud-技术专区-Zuul-使用指南

    Zuul作为微服务系统的网关组件,用于构建边界服务,致力于动态路由.过滤.监控.弹性伸缩和安全. Zuul功能 认证 压力测试 金丝雀测试 动态路由 负载削减 安全 静态响应处理 主动/主动交换管理 ...

  2. iView Card 图文组件

    <Card style="width:3.3rem" :dis-hover="false" > <div style="text-a ...

  3. android中SharedPreferences

    SharedPreferences:用于存储少量并且数据格式简单,基本上都是普通的字符串,标量类型的值,比如:应用程序的各种配置信息等. SharedPreferences:保存的数据格式主要是key ...

  4. Jmeter实现百分比业务比例

    Jmeter实现百分比业务比例   相较于LoadRunner,jmeter在复杂场景方式貌似略有欠缺.前一段时间,想实现一个功能,如有两个采样器a与b,a采样器与b采样器被执行的概率分别为1/4与3 ...

  5. leetcode-160周赛-5240-串联字符串的最大长度

    题目描述: 自己的提交:O(2**n∗n∗m),m 为字符串长度 class Solution: def maxLength(self, arr: List[str]) -> int: from ...

  6. java 获取String出现最多次数的字段

    package hello; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator ...

  7. 重视项目排期,对dateline 有所敬畏

    项目排期 = 个人任务完成 + 风险预估 + 意外情况 + 项目上下游依赖 个人任务完成 个人任务具体话 不要考虑私人情感,专注工作 风险预估 对可能出现的情况进行考虑 意外情况 对出现的意外情况提前 ...

  8. Java——is-a、is-like-a、has-a

    3.8 is-a.is-like-a.has-a 3.8.1 is-a(类和类之间的继承关系,泛化关系) public class Animal{ public void method1() ; } ...

  9. 高级运维(三):部署Lnmp环境、构建Lnmp平台、地址重写

    一.部署LNMP环境 目标: 安装部署Nginx.MariaDB.PHP环境 1> 安装部署Nginx.MariaDB.PHP.PHP-FPM: 2> 启动Nginx.MariaDB.FP ...

  10. IReport实践指南

    IReport实践指南 前言 最近,在做一个电子签章的功能,然后就接触到IReport报表,经过好几天的摸索实践,功能已经完成了,今天来总结一下. 什么是IReport,IReport是JasperR ...