第七章_Ribbon负载均衡与服务调用
1.Ribbon概述
1.1Ribbon是什么

1.2官网资料
自己搜
1.3能干嘛
一句话:负载均衡+RestTemplate调用
(1)LB---负载均衡
①Nginx---负载均衡
Nginx是服务器负载均衡,客户端所有请求都会交给nginx,然后由nginx实现转发请求,即实现负载均衡
②Ribbon---负载均衡
Ribbon是本地负载均衡。在调用微服务接口时候,会在注册中心上获取注册信息服务列表之后缓存到JVM本地,从而在本地实现RPC远程服务调用技术
③集中式LB
即在服务的消费方和提供方之间使用独立的LB设施(可以是硬件,如F5,也可以是软件,如nginx),由该设施负责把访问请求通过某种策略转发至服务的提供方。
④进程内LB
将LB逻辑集成到消费方,消费方从服务注册中心获知有哪些地址可用,然后自己再从这些地址中选择出一个合适的服务器。
Ribbon属于进程内LB,只是一个类库,集成于消费方进程,消费方通过它来获取到服务提供方的地址
2.Ribbon负载均衡演示
2.1架构说明
Ribbon是一个软负载均衡的客户端组件,可以和其他所需请求的客户端结合使用,和eureka结合只是其中的一个实例
(1)Ribbon架构图

(2)Ribbon的工作顺序
第一,选择EurekaServer,优先选择在同一个区域内负载较少的server
第二,根据用户指定的策略,再从server取到的服务注册列表中选择一个地址
其中用户指定的策略有:轮询、随机和根据响应时间加权
2.2POM文件

2.3二说RestTemplate的使用
(1)getForObject()和getForEntity()方法的区别

(2)postForObject()和postForEntity()方法的区别
postForObject()是post请求,可以携带参数,返回的对象是json格式。
调用的远程服务,返回的是什么,就是什么
postForEntity()是post请求,可以携带参数,返回的对象是Entity对象。
调用的远程服务,返回的是什么,再加上返回码。
3.Ribbon核心组件IRule
IRule:根据特定算法,从服务列表中选取一个要访问的服务
IDEA生成:

3.1Ribbon默认自带的负载规则

(1)修改默认规则
-
说明:自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,又主启动类上的注解@SpringBootApplication包含了注解@ComponentScan,所以得另起一个包放置自定义规则

(2)新建MySelfRule规则类

(3)主启动类添加@RibbonClient
指定重新配置的规则

4.Ribbon负载均衡算法
4.1原理

4.2源码
4.3手写轮询算法
(1)ApplicationContextBean去掉注解@LoadBalanced

(2)主启动取消自己配置的规则
注意:是在80端口服务的主启动

(3)编写LoadBalancer接口

(4)手写轮询器

(5)80服务的业务层

(6)8001/8002服务的业务层

(7)测试
访问:localhost/consumer/payment/lb
轮询效果出现,查看后台

第八章_OpenFeign接口调用
1.OpenFeign简介
1.1OpenFeign是什么

官网地址:Spring Cloud
1.2Feign能干什么
总结:Feign是一个服务接口绑定器,接口加一个注解

1.3Feign和OpenFeign的区别

2.OpenFeign使用步骤
2.1说明
微服务调用接口+@FeignClient,即接口+注解

2.2新建cloud-consumer-feign-order80
(1)POM
xml
<dependencies>
<!-- OpenFeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用payment对象-->
<dependency>
<groupId>springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
(2)YML

(3)主启动

(4)业务类

(5)测试
- 先启动7001/7002服务
- 再启动8001/8002订单服务
- 最后启动feign80服务
(6)小总结

3.OpenFeign超时控制
3.1服务提供方8001/8002添加超时方法

3.2服务消费方80添加超时方法

3.3测试

3.4修改默认连接和超时时间

4.OpenFeign日志打印功能
Feign 提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解 Feign 中 Http 请求的细节。
说白了就是对Feign接口的调用情况进行监控和输出
4.1日志级别

4.2添加config配置类

4.3yml设置日志打印级别

4.4测试
