外观
SpringCloud 学习笔记 05 --openFeign
约 333 字大约 1 分钟
2018-03-25
⚠ 请注意,本文编写于 2658 天前,最后最后修改于 2658 天前,其中某些信息可能已经过时。
openFeign是一个声明式的Web 服务客户端,让编写Web服务客户端变得非常容易,只需创建一个借口并在借口上添加注解即可。
用于服务调用方
1.添加pom依赖
<dependencies>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
2.application.yml
server:
port: 80
spring:
application:
name: cloud-consumer-feign-order
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
3.主启动
@SpringBootApplication
@EnableFeignClients //说明是feign 客户端
public class FeignOrderMain80 {
public static void main(String[] args) {
SpringApplication.run(FeignOrderMain80.class, args);
}
}
4.service接口
@Component
//作为feign组件的接口 以及将要调用的服务名称
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping(value = "/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id);
@PostMapping(value = "/payment/creat")
public CommonResult creat(@RequestBody Payment payment);
}
5.controller调用
@RestController
@Slf4j
public class FeignOrderController {
@Resource
public PaymentFeignService paymentFeignService;
@GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id) {
return paymentFeignService.getPaymentById(id);
}
}
6.配置详细日志
FeignConfig.java
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
application.xml
logging:
level:
# feign日志以什么级别监控哪个接口
org.mc.mycloud.service.PaymentFeignService: debug