springcloud openFeign 组件的基本使用

一、 引入pom 依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

二、 激活注解使用
启动类上面加入@EnableFeignClients注解
三、 编写service 接口

@FeignClient(value = "CLOUD-PAYMENT-SERVICE")   //寻找哪个服务模块
public interface PaymentService {
	// 哪个接口
    @GetMapping("/payment/get/{id}") //@PathVariable 必须携带参数 否则报错
    CommonResult get(@PathVariable("id") Long id);

}

注意在这里如果使用到@PathVariable 注解 必须携带参数,否则启动时报错。
四、controller 层像我们普通接口调用

@Autowired
    PaymentService paymentService;

    @GetMapping("/payment/get/{id}")
    public CommonResult getOrder(@PathVariable Long id){
        System.out.println(paymentService);
        return paymentService.get(id);
    }

五、 接口调用的响应超时时间

ribbon:
  # 建立连接所花费最长时间
  ReadTimeout: 5000
  #  建立连接后从服务器读取到可用资源的最长时间   默认1s
  ConnectTimeout: 5000

六、 feign 日志
NONE:默认, 不显示任何日志
BASIC: 仅记录请求方法、url、响应状态码与时间
HEADERS: 包含上述之外, 还包含请求和响应的头信息
FULL: 除了上述之外, 还包含请求和响应的正文和元数据

配置:

import feign.Logger;

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

yml 文件:

logging:
  level:
    # feign 日志以什么级别监控哪个接口
    com.example.controller.service.PaymentService: debug
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值