当前位置: 代码迷 >> 综合 >> SpringCloud学习(九)---OpenFeign
  详细解决方案

SpringCloud学习(九)---OpenFeign

热度:49   发布时间:2023-12-29 04:30:21.0

1.OpenFeign的使用示例

建立module

导入pom.xml

<?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.kebo.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-consumer-feign-order80</artifactId><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.liuscoding</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></project>

改yml

server:port: 80eureka:client:#表示自己注册进eureka默认为trueregister-with-eureka: falseservice-url:defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

配置启动类以及业务类

@SpringBootApplication
@EnableFeignClients//开启feign客户端
public class OrderMain80 {public static void main(String[] args) {SpringApplication.run(OrderMain80.class, args);}
}@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")//value为微服务的生产者名称
public interface PaymentFeignService {//这个路径要和服务端保持一致,也就是8001和8002,而不是这个80项目的控制层@GetMapping("/payment/get/{id}")public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}@RestController
@Slf4j
public class OrderController {@Autowiredprivate PaymentFeignService paymentFeignService;@GetMapping("/consumer/payment/get/{id}")public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {return paymentFeignService.getPaymentById(id);}}

启动项目即可成功看到效果

2.OpenFeign的超时示例

在8001的控制层加入以下代码,为了模拟执行接口时间过长

@GetMapping("/payment/feign/timeout")public String paymentFeignTimeout(){try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}return serverPort;}

在feign的module加入以下业务代码

 @GetMapping("/payment/feign/timeout")public String paymentFeignTimeout();@GetMapping("/consumer/payment/feign/timeout")public String paymentFeignTimeout() {return paymentFeignService.paymentFeignTimeout();}

启动项目会报超时错误

解决只需要在yml文件加入以下配置

ribbon:#指的是建立连接后从服务器读取到可用资源所用的时间ReadTimeout: 5000#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间ConnectTimeout: 5000

3.OpenFeign的日志增强

新建一个config的包,加入一个类

@Configuration
public class FeignConfig {@BeanLogger.Level feignLoggerLevel() {return Logger.Level.FULL;}
}

修改yml文件


logging:level:# feign日志以什么级别监控哪个接口com.kebo.springcloud.service.PaymentFeignService: debug

启动项目即可