在上一篇文章,讲了服务的注册和发现。在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下基于ribbon+rest。

一、ribbon简介

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.

—–摘自官网

ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。

ribbon 已经默认实现了这些配置bean:

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl

  • IRule ribbonRule: ZoneAvoidanceRule

  • IPing ribbonPing: NoOpPing

  • ServerList ribbonServerList: ConfigurationBasedServerList

  • ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

二、准备工作

这一篇文章基于上一篇文章的工程,启动eureka-server 工程;启动eureka-client工程,它的端口为8882;将service-hi的配置文件的端口改为8883,并启动,这时你会发现:eureka-client在eureka-server注册了2个实例,我将端口又换成8886,这样就往eureka-server中注册了三个实例,这就相当于一个小的集群。如下图可以看到图标显示为3,说明同时启动了三个实例。

如何在idea下启动多个实例,请参照这篇文章: https://blog.csdn.net/forezp/article/details/76408139

访问localhost:8881如图所示: 如何一个工程启动多个实例,请看这篇文章:https://blog.csdn.net/forezp/article/details/76408139

三、建一个服务消费者

重新建一个Maven工程,其中pom如下:

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.liumy</groupId>
<artifactId>ribbon-f-2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent> <modules>
<module>service-ribbon</module>
</modules> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

再重新建一个spring-boot工程,取名为:service-ribbon; 在它的pom.xml继承了父pom文件,并引入了以下依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <parent>
<groupId>com.liumy</groupId>
<artifactId>ribbon-f-2</artifactId>
<version>1.0-SNAPSHOT</version>
</parent> <groupId>com.liumy</groupId>
<artifactId>service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>service-ribbon</name>
<description>Demo project for Spring Boot</description> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency> </dependencies> </project>

在工程的配置文件指定服务的注册中心地址为http://localhost:8881/eureka/,程序名称为 service-ribbon,程序端口为8884。配置文件application.yml如下:

 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8881/eureka/
server:
port: 8884
spring:
application:
name: service-ribbon

在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

 package com.liumy.serviceribbon;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
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; @EnableEurekaClient
@SpringBootApplication
@EnableDiscoveryClient//通过@EnableDiscoveryClient向服务中心注册
public class ServiceRibbonApplication { public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
} @Bean//向程序的ioc注入一个bean
@LoadBalanced//通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
RestTemplate restTemplate(){
return new RestTemplate();
}
}

写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费eureka-client服务的“/hello”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

 package com.liumy.serviceribbon;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; /**
* @ClassName HelloService
* @Descroption TODO ribbon测试service
* @Author Yubaba
* @Date 2019/10/20 15:11
**/
@Service
public class HelloService { @Autowired
RestTemplate restTemplate; public String HelloService(String name){
return restTemplate.getForObject("http://EUREKA-CLIENT/hello?name="+name,String.class);
}
}

写一个controller,在controller中用调用HelloService 的方法,代码如下:

 package com.liumy.serviceribbon;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* @ClassName HelloController
* @Descroption TODO ribbon测试controller
* @Author Yubaba
* @Date 2019/10/20 15:15
**/
@RestController
public class HelloController { @Autowired
HelloService helloService; @GetMapping("helloController")
public String helloController(@RequestParam("name")String name){
return helloService.HelloService(name);
} }

在浏览器上多次访问http://localhost:8884/helloController?name=测试,浏览器交替显示:

这说明当我们通过调用restTemplate.getForObject(“http://EUREKA-CLIENT/hello?name=”+name,String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。

四、此时的架构

(图例来源于网络,为了方便理解)

  • 一个服务注册中心,eureka server,端口为8881
  • service-hi工程跑了三个实例,端口分别为8882,8883,8886,分别向服务注册中心注册
  • sercvice-ribbon端口为8884,向服务注册中心注册
  • 当sercvice-ribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用eureka-client:8882、8883和8886 三个端口的hello接口

五、参考资料

本文参考了以下:

http://blog.csdn.net/forezp/article/details/69788938

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

注:本篇为原创文章,其中的过程与实现是参考某BAT顶级架构师的教学完成。

SpringCloud教程二:Ribbon(Finchley版)的更多相关文章

  1. 最适合新手入门的SpringCloud教程 6—Ribbon负载均衡「F版本」

    SpringCloud版本:Finchley.SR2 SpringBoot版本:2.0.3.RELEASE 源码地址:https://gitee.com/bingqilinpeishenme/Java ...

  2. GitHub 新手教程 二,Windows 版 GitHub 安装

    1,下载地址: https://git-scm.com/download/ 2,信息: 3,选择安装位置: 例如:d:\soft\git 4,选择组件: 5,创建开始菜单: 6,选择Git使用的默认编 ...

  3. Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin【Finchley 版】

    Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin[Finchley 版]  发表于 2018-04-24 |  随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请 ...

  4. Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】

    Spring Cloud(二):服务注册与发现 Eureka[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  上一篇主要介绍了相关理论,这一篇开始我们 ...

  5. 学习笔记(三)--->《Java 8编程官方参考教程(第9版).pdf》:第十章到十二章学习笔记

    回到顶部 注:本文声明事项. 本博文整理者:刘军 本博文出自于: <Java8 编程官方参考教程>一书 声明:1:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法 ...

  6. 学习笔记(二)--->《Java 8编程官方参考教程(第9版).pdf》:第七章到九章学习笔记

    注:本文声明事项. 本博文整理者:刘军 本博文出自于: <Java8 编程官方参考教程>一书 声明:1:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法者自负一切 ...

  7. 史上最简单的SpringCloud教程 | 第十篇: 高可用的服务注册中心(Finchley版本)

    转载请标明出处: 原文首发于 https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f10-eureka/ 本文出自方志朋的博客 文章 史上最简单 ...

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

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

  9. 史上最简单的 SpringCloud 教程 | 终章

    https://blog.csdn.net/forezp/article/details/70148833转载请标明出处:http://blog.csdn.net/forezp/article/det ...

随机推荐

  1. IOCAutofac与ORMEntityFramwork的联系--单例模式

    在你阅读之前默认你已经理解了IOC.DI.ORM以及autofac和EF的使用 在我最近写项目的时候我在单步调试时偶然发现的一个问题 先说明我的项目使用.NET MVC 三层架构,运用IOC Auto ...

  2. Python网络爬虫实战(三)照片定位与B站弹幕

    之前两篇已经说完了如何爬取网页以及如何解析其中的数据,那么今天我们就可以开始第一次实战了. 这篇实战包含两个内容. * 利用爬虫调用Api来解析照片的拍摄位置 * 利用爬虫爬取Bilibili视频中的 ...

  3. Python中使用高德API实现经纬度转地名

    场景 高德API提供给开发者们一些常用功能的接口,其中有一种叫地理/逆地理编码能实现 地名查询经纬度和经纬度查地名. 实现 高德API平台: https://lbs.amap.com/ 注册并登陆 找 ...

  4. Visual Studio Code 安装美化合集

    这是一个关于VSCode编辑器的各种配置. 你可以在这里找到VSCode 的各种操作,如果这里找不到,请移步官方文档C++ programming with Visual Studio Code以及各 ...

  5. OkHttp3使用教程,实现get、post请求发送,自动重试,打印响应日志。

    一.创建线程安全的okhttp单例 import service.NetworkIntercepter;import service.RetryIntercepter;import okhttp3.* ...

  6. HTML定位和布局----float浮动

    1.定位体系一共有三种 (1)常规流: (2)浮动定位 (3)绝对定位 2.float属性常用的语法: (1)float:left:左浮动 (2)float:right:右浮动 (3)float:no ...

  7. LCX使用心得

    最近在搞内网渗透,碰到 端口转发&边界处理 的时候,我们就可以借助一些小工具了,这类工具有很多,这里主要说明lcx的用法. lcx是个很老的端口转发工具,而它的使用也很简单.不过想要把lcx玩 ...

  8. java数据结构——红黑树(R-B Tree)

    红黑树相比平衡二叉树(AVL)是一种弱平衡树,且具有以下特性: 1.每个节点非红即黑; 2.根节点是黑的; 3.每个叶节点(叶节点即树尾端NULL指针或NULL节点)都是黑的; 4.如图所示,如果一个 ...

  9. python9

    v> 软件测试 广州博才科技开发有限公司 迅捷PDF编辑器 3.5 集合 学习目标: 1. 能够说出如何创建集合 2. 能够说出字典和集合的区别 3. 能够说出如何向集合中添加元素 4. 能够说 ...

  10. Django REST Framework序列化器

    Django序列化和json模块的序列化 从数据库中取出数据后,虽然不能直接将queryset和model对象以及datetime类型序列化,但都可以将其转化成可以序列化的类型,再序列化. 功能需求都 ...