SpringCloud教程二:Ribbon(Finchley版)

在上一篇文章,讲了服务的注册和发现。在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于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,说明同时启动了三个实例。

SpringCloud教程二:Ribbon(Finchley版)

 

 

 

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

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

SpringCloud教程二:Ribbon(Finchley版)

 

 

三、建一个服务消费者

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

1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 "> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.liumy</groupId> 8 <artifactId>ribbon-f-2</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>pom</packaging> 11 12 <parent> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-parent</artifactId> 15 <version>2.0.3.RELEASE</version> 16 <relativePath/> 17 </parent> 18 19 <modules> 20 <module>service-ribbon</module> 21 </modules> 22 23 <properties> 24 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 25 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 26 <java.version>1.8</java.version> 27 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> 28 </properties> 29 30 <dependencies> 31 <dependency> 32 <groupId>org.springframework.boot</groupId> 33 <artifactId>spring-boot-starter-test</artifactId> 34 <scope>test</scope> 35 </dependency> 36 </dependencies> 37 38 <dependencyManagement> 39 <dependencies> 40 <dependency> 41 <groupId>org.springframework.cloud</groupId> 42 <artifactId>spring-cloud-dependencies</artifactId> 43 <version>${spring-cloud.version}</version> 44 <type>pom</type> 45 <scope>import</scope> 46 </dependency> 47 </dependencies> 48 </dependencyManagement> 49 50 <build> 51 <plugins> 52 <plugin> 53 <groupId>org.springframework.boot</groupId> 54 <artifactId>spring-boot-maven-plugin</artifactId> 55 </plugin> 56 </plugins> 57 </build> 58 59 </project>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppfzp.html