img

Ribbon

image-20230815175255942

概述

image-20230815175345997

image-20230815175750452

负载均衡

使用 resetTemplate 测试

1
2
3
4
5
6
7
8
@GetMapping("/consumer/payment/getForEntity/{id}")
public CommonResult<Payment> getPayment2(@PathVariable("id") Long id) {
ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
if (entity.getStatusCode().is2xxSuccessful()) {
return entity.getBody();
}
return new CommonResult<>(444, "失败");
}

IRule 接口

image-20230815182012626

image-20230815183354485

==更改替换算法==

  • ==注意== 不能放在主类下

image-20230815182455546

  1. 建 package

image-20230815182750035

  1. Myrule 类
1
2
3
4
5
6
7
@Configuration
public class MyselfRule {
@Bean
public IRule myRule() {
return new RandomRule(); // 定义随机算法
}
}
  1. ==启动类添加@RibbonClient==
1
2
3
4
5
6
7
8
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEurekaClient // 注册EurekaClient
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE", configuration = MyselfRule.class) // 替换算法
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}

负载均衡算法

原理

image-20230817171221536

OpenFeign

概述

介绍

  • 用于客户端,通过==接口==连接服务调用

image-20230817175009286

对比

image-20230817175259330

==服务调用==

image-20230817220210011

建 module

  • cloud-consumer-feign-order80

pom

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<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支付Entity -->
<dependency>
<groupId>com.cyt.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--web-->
<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>

写 yml

1
2
3
4
5
6
7
8
server:
port: 80

eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

主启动

==@EnableFeignClients==

1
2
3
4
5
6
7
@SpringBootApplication
@EnableFeignClients // 使用feign
public class OrderFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class, args);
}
}

业务类

image-20230817215644791

测试

image-20230817220636367

超时控制

image-20230817233826496

  • 配置代码
1
2
3
4
5
6
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000

日志打印

  • 日志级别

image-20230817234337461

  • 建 config 包
1
2
3
4
5
6
7
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL; // 全部
}
}
  • 配置
1
2
3
4
logging:
level:
# feign日志以什么级别监控哪个接口
com.atguigu.springcloud.service.PaymentFeignService: debug