SpringCloud-远程调用OpenFeign

  一、配置使用

       1、添加依赖


        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        2、在启动类添加@EnableFeignClients注解

@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {

    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain80.class,args);
    }
}

        3、添加OpenFeign接口CLOUD-PAYMENT-SERVICE为服务名称

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService
{
    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id")  Long id);
}

        4、测试

@RestController
@Slf4j
public class OrderFeignController
{
    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        return paymentFeignService.getPaymentById(id);
    }
}
  二、超时控制

       默认一秒钟,超时报错,可以通过如下配置修改

#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
    #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
    ReadTimeout: 5000
    #指的是建立连接后从服务器读取到可用资源所用的时间
    ConnectTimeout: 5000
  三、日志打印

        1、添加配置类

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

        2、添加配置

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

你可能感兴趣的:(spring,cloud,分布式,spring,boot)