①. OpenFeign的概述
-
①. Feign是一个服务接口的绑定器,(接口+@FeignClient(value=“服务名称”)) 接口与接口之间的一个调用 -
②. Feign集成了Ribbon。利用Ribbon维护了Payment的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign值需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用 -
③. Feign和OpenFeign的区别 -
④. SpringMvc中能用的在Feign中都能使用
②. Feign的基本使用(消费端)
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</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>
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
- ④. 主启动类:@EnableFeignClients
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class,args);
}
}
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping("/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
@GetMapping("/payment/feign/timeout")
public String paymentFeignTimeOut();
}
@RestController
@RequestMapping("/feignClient")
public class OrderFeignController {
@Autowired
private PaymentFeignService paymentFeignService;
@GetMapping("/payment/getPaymentById/{id}")
public CommonResult<Payment>getPaymentById(@PathVariable("id") Long id){
return paymentFeignService.getPaymentById(id);
}
}
- ⑥. 测试
先启动2个eureka集群7001/7002 再启动2个微服务8001/8002 启动OpenFeign启动 http://localhost/consumer/payment/get/31 Feign自带负载均衡配置项 - ⑦. 小总结:
③. OpenFeign超时控制
-
①. 超时设置,故意设置超时演示出错情况。由于Feign的默认超时时间是1s,这里我们在8001、8002中写一个方法,在方法中停顿3s -
②. OpenFeign默认等待一秒钟,超过后报错
YML文件里需要开启OpenFeign客户端超时控制
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping("/payment/getPaymentById/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id")Long id);
@GetMapping("/payment/timeout")
public String timeout();
}
- ③. 进行了上述②的配置后,再次访问:不会出现错误页面,会等待3s后出现结果
④. OpenFeign日志打印功能
- NONE:默认的,不显示任何日志
- BASIC:仅记录请求方法、URL\响应状态码及执行时间
- HEADERS:除了BASIC中定义的信息之外,还又请求和响应的头信息
- FULL:除了HEADERS中定义的信息之外,还又请求和响应的正文及元数据
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
logging:
level:
com.atguigu.springcloud.service.PaymentFeignService: debug
- ⑤. 后台日志查看
|