上一篇:SpringCloud 2020-Ribbon负载均衡服务调用(笔记)
OpenFeign服务接口调用
1、概述
1、OpenFeign是什么
2、能干嘛
3、Feign和OpenFeign两者区别
2、OpenFeign使用步骤
OpenFeign自带Ribbon
1、新建cloud-consumer-feign-order80
2、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">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.liukai.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-feign-order80</artifactId>
<!--openfeign-->
<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.liukai.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>
</project>
3、YML
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
4、主启动类OrderFeignMain80
package com.liukai.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author liukai
* @version 1.0.0
* @ClassName OrderFeignMain80.java
* @Description TODO
* @createTime 2021年03月22日 14:00:00
*/
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class);
}
}
5、业务类
- 业务逻辑接口+@FeignClient配置调用provider服务
- 新建PaymentFeignService接口并新增注解@FeignClient
package com.liukai.springcloud.service;
import com.liukai.springcloud.entities.CommonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author liukai
* @version 1.0.0
* @ClassName PaymentFeignService.java
* @Description TODO
* @createTime 2021年03月22日 14:10:00
*/
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping(value = "/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id);
}
6、控制层Controller
package com.liukai.springcloud.controller;
import com.liukai.springcloud.entities.CommonResult;
import com.liukai.springcloud.service.PaymentFeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author liukai
* @version 1.0.0
* @ClassName OrderFeignController.java
* @Description TODO
* @createTime 2021年03月22日 14:14:00
*/
@RestController
public class OrderFeignController {
@Resource
private PaymentFeignService paymentFeignService;
@GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){
return paymentFeignService.getPaymentById(id);
}
}
7、测试:依次启动7001,7002,8001,8002,cloud-consumer-feign-order80
访问 http://localhost/consumer/payment/get/31
多访问几次,可以发现访问的端口号会在8001,8002之间轮询
Feign自带负载均衡配置项
8、总结
3、OpenFeign超时控制
1、超时设置,故意设置超时演示出错情况
- 服务提供方8001故意写暂停程序,修改cloud-provider-payment8001的controller,添加如下代码
// OpenFeign超时访问
@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout(){
try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) {e.printStackTrace();}
return serverPort;
}
- 服务消费方cloud-consumer-feign-order80的PaymentFeignService中添加一下代码,超时方法
@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout();
- 服务消费方cloud-consumer-feign-order80的OrderFeignController中添加超时方法
@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeout(){
return paymentFeignService.paymentFeignTimeout();
}
2、测试:依次启动7001,7002,8001,cloud-consumer-feign-order80,访问 http://localhost/consumer/payment/feign/timeout
访问呢超时,报错
3、原因:OpenFeign默认等待一秒钟,超过后报错,8001设置的等待时间为3秒
4、解决:OpenFeign默认支持RibbonYML文件里需要开启OpenFeign客户端超时控制
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000
5、再次测试,
访问在等待三秒后,成功返回数据。
4、OpenFeign日志打印功能
是什么:
日志级别:
配置日志bean:cloud-consumer-feign-order80下新建config包,下面新建FeignConfig 类
package com.liukai.springcloud.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author liukai
* @version 1.0.0
* @ClassName FeignConfig.java
* @Description TODO
* @createTime 2021年03月22日 19:38:00
*/
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
YML文件里需要开启日志的Feign客户端
logging:
level:
com.liukai.springcloud.service.PaymentFeignService: debug
测试,依次启动7001,7002,8001,80。访问 http://localhost/consumer/payment/get/31
控制台打印详细的日志信息。
5、下一篇:SpringCloud 2020-Hystrix断路器(笔记)
SpringCloud 2020-Hystrix断路器(笔记)