一、什么是Ribbon:

Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法。默认是使用轮询。

方法1:可以在springboot启动类之外定义一个配置类(@RibbonClient)配置对应的算法。

方法2:在配置文件yml中配置

相关链接:https://www.cnblogs.com/fengyuduke/p/10712569.html

怎么使用Ribbon实现负载均衡?

相关链接: https://www.cnblogs.com/xing-12/p/9889153.html

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

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

3.通过注入bean:  用RestTemplate调用restTemplate.getForObject("http://xing-user/user/findByName/"+name, User.class)方法。

二、Eureka服务提供者集群

先启动上篇文章中的注册中心eureka-server:8001, 以及hello-service:8011,接着修改hello-service项目配置文件中的端口,改成8012,再次运行用户服务项目。

执行成功,查看Eureka注册中心,可以看到有用户服务应用有两个端口对应。

(如果是IDEA用户,需要修改Application.java的执行方式,默认是单实例运行,所以你在运行8011的项目,修改端口无法再次运行。)

三、RestTemplate+Ribbon消费者:  新建一个maven服务

 pom.xml:

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

application.properties:

spring.application.name=hello-consumer-ribbon
server.port=8021
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/spring.application.name=hello-consumer-ribbon
server.port=8021
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/

启动类:

package cn.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
@RestController
public class HelloConsumerRibbonApplication { public static void main(String[] args) {
SpringApplication.run(HelloConsumerRibbonApplication.class, args);
} @Bean
@LoadBalanced
public RestTemplate restTemplate () {
return new RestTemplate();
} @Autowired
private RestTemplate restTemplate; //获取服务实例,作用为之后console显示效果;"http://USER-SERVICE/hello?name= 标识注册的服务。USER-SERVICE在eureka注册的服务名
@RequestMapping("hello")
public ResponseEntity<String> hello (String name) {
return restTemplate.getForEntity("http://USER-SERVICE/hello?name=" + name, String.class);
} }

四、测试
测试服务消费
http://localhost:8021/hello?name=ribbon

测试负载均衡:
我们在hello-service hello方法中加上日志打印,然后再分别启动 hello-service:8012,8002,然后多次访问 http://localhost:8021/hello?name=ribbon,可以看到两个hello-service项目的控制台都有日志输出,表示实现了负载均衡。

使用RestTemplate+Ribbon必须指定调用服务名称,如上面的HELLO-SERVICE,为了方便使用,SpringCloud还集成了Feign消费方式。
————————————————

SpringCloud(三):服务消费以及负载均衡(RestTemplate+Ribbon)的更多相关文章

  1. Spring Cloud系列(三):服务消费与负载均衡

    上一篇介绍了服务提供者,有了注册中心和服务提供者,我们就可以进行服务消费了.Spring Cloud可以通过RestTemplate+Ribbon和Feign这两种方式消费服务. 我们仍然在上一篇的项 ...

  2. Spring Cloud ---- 服务消费与负载均衡(Rest + Ribbon )

    上一篇主要写了基于Eurake的服务的注册,主要就是创建注册中心,创建服务者,将服务者注册到注册中心,完成服务的暴露.这一篇主要写服务的消费与服务消费的负载均衡. 服务的调用方式有两种,Rest + ...

  3. spring cloud(服务消费者(利用ribbon实现服务消费及负载均衡)——初学二)

    Ribbon是一个基于HTTP和TCP客户端的负载均衡器,利用ribbon实现服务消费,并实现客户端的负载均衡. 一.准备工作(利用上一节的内容) 启动服务注册中心 启动computer-servic ...

  4. 微服务SpringCloud之服务调用与负载均衡

    上一篇我们学习了服务的注册与发现,本篇博客是在上一篇的基础上学习服务的调用.上一博客主要创建了Eureka的服务端和一个Client,该Client包含了一个Controller用来提供对外服务供外部 ...

  5. Spring Cloud ---- 服务消费与负载均衡(feign)

    feign是一个声明式的伪客户端,只需要创建一个接口并且注解,它具有可插拔的特性.feign集合了Ribbon,再与Eurake结合实现服务的注册发现与负载均衡.结合Hystrix,具有熔断功能. 1 ...

  6. spring cloud(服务消费者(利用feign实现服务消费及负载均衡)——初学三)

    Feign是一个声明式的Web Service客户端,我们只需要使用Feign来创建一个接口并用注解来配置它既可完成. 它具备可插拔的注解支持,包括Feign注解和JAX-RS注解.Feign也支持可 ...

  7. 学习一下 SpringCloud (三)-- 服务调用、负载均衡 Ribbon、OpenFeign

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  8. spring cloud --- Ribbon 客户端负载均衡 + RestTemplate ---心得【无熔断器】

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 了解了 eureka 服务注册与发现 的3大角色 ,会使用RestTem ...

  9. spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 当超大并发量并发访问一个服务接口时,服务器会崩溃 ,不仅导致这个接口无法 ...

随机推荐

  1. String字符串为什么不可变的深入理解

    String是被final修饰的,是不可变对象,那么这句什么意思呢.在学习scala时候var,val时候,就想到这个问题,所以记录下 看案例: package com.cxy; import sun ...

  2. nginx常用模块(三)

    Nginx常用模块(三) ngx_http_proxy_module模块配置(http或https协议代理) proxy_pass URL; 应用上下文:location, if in locatio ...

  3. 架构师成长之路5.2-Saltstack远程执行

    点击架构师成长之路 架构师成长之路5.2-Saltstack远程执行 配置管理工具: Pupper:1. 采用ruby编程语言:2. 安装环境相对较复杂:3.不支持远程执行,需要FUNC工具配置才可以 ...

  4. yum运行报错:libcurl.so.4: cannot open shared object file: No such file or directory

    /usr/lib64/目录下存在libcurl.so.4文件 CURL的动态库找不到,这里我们加入到ld.so.conf [root@localhost bin]#  vim /etc/ld.so.c ...

  5. django ListView

    context_object_name = 'posts'. The template default name is ListView 'object_list' from .models impo ...

  6. P1055 ISBN号码

    题目描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括99位数字.11位识别码和33位分隔符,其规定格式如x-xxx-xxxxx-x,其中符号-就是分隔符(键盘上的减号),最后一位 ...

  7. idea中自定义快捷键

    idea中自定义快捷键 在开发的过程中,总要写一些重复的代码,那么使用自定义快捷键,可以将这些经常用到的代码,自动生成. 1.在idea工具中点击File-->Settings 2.在弹出来的界 ...

  8. luogu P5514 [MtOI2019]永夜的报应

    题目背景 在这世上有一乡一林一竹亭,也有一主一仆一仇敌. 有人曾经想拍下他们的身影,却被可爱的兔子迷惑了心神. 那些迷途中的人啊,终究会消失在不灭的永夜中-- 题目描述 蓬莱山 辉夜(Kaguya)手 ...

  9. THML第一天学习!

    又迎来了新一轮的周末,学习的耗时光呀!这周呢学了一点点数据库,暂时还不想写下自己的感受(这学期在 学习数据库,等学期末的时候在总结一下数据库的相关学习). 目前呢,我是打算跟着sunck学习观pyth ...

  10. XCode项目配置

    此设置优先级在playersetting之上,如果为空或者格式不正确或者文件不存在将不会设置,请注意 一.设置面板 二.对应Xcode中设置 1.TeamID  登录苹果开发者网站,查看个人信息,就有 ...