一、什么是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. 痞子衡嵌入式:恩智浦机器视觉模块OpenMV-RT那些事(1)- 初体验

    大家好,我是痞子衡,是正经搞技术的痞子.本系列痞子衡给大家介绍的是机器视觉模块OpenMV-RT初体验. 近些年机器视觉应用一直是个很火的方向,想象一下机器如果能长上"眼睛",是不 ...

  2. java JDK安装包的获取与安装

    Java JDK 安装包获取和安装: JDK 1.8.211 官网下载地址 https://www.oracle.com/technetwork/java/javase/downloads/jdk8- ...

  3. SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”

    1.问题描述: 我的项目是tcServer,在运行的之后出现如下错误: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBin ...

  4. 【Android - 自定义View】之View的measure过程解析

    measure(测量)过程是View的工作流程中最开始.最核心的过程,在这个过程中负责确定View的测量宽/高. 对于View和ViewGroup,measure过程有不同的执行方法:如果目标是一个原 ...

  5. 设置颜色的函数:rgb,hsv,color palettes

    1.grb函数 (1)功能: 通过对给定的三个基本色红,绿,蓝的颜色饱和度(intensity)的设定,而创造颜色. 阿尔法透明度(alpha transparent):其值也能被指定,从0到max ...

  6. python_爬虫

    1.网络爬虫    1.定义:网络蜘蛛,网络机器人,抓取网络数据的程序    2.总结:用Python程序去模仿人去访问网站,模仿的越逼真越好    3.目的:通过有效的大量的数据分析市场走势,公司的 ...

  7. python中的局部变量和全局变量

  8. 有奖投票丨HC2019开发者关注的TOP10问题你最想听哪个?

    目前,人工智能已经成为广大开发者重点关注的技术领域.然而,随着人工智能技术的快速发展,AI应用场景复杂度在与日俱增,算法调教也亟需不断成熟,这些都为开发者们带来了更多全新的挑战.如何快速把握前沿技术的 ...

  9. 基于串口通信做my_printf时遇到的坑儿

    首先,完成了串口向终端putty的打印函数ConsolePrint(),但该函数只能打印字符串,无法像stdio库中的printf函数一样打印整数和浮点数等. 因此,我先是使用了标准库stdio中的s ...

  10. 【原创】003 | 搭上基于SpringBoot事务思想实战专车

    前言 如果这是你第二次看到师长,说明你在觊觎我的美色! 点赞+关注再看,养成习惯 没别的意思,就是需要你的窥屏^_^ 专车介绍 该趟专车是开往基于Spring Boot事务思想实战的专车,在上一篇 搭 ...