springcloud openfeign

1.POM

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

2.yml

openfeign 无配置

nacos 配置

server:
  port: 8887

spring:
  application:
    name: cloud-consumer-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #服务注册中心地址

3.启动类

激活标签 @EnableFeignClients

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderMain {
public static void main(String[] args) {
SpringApplication.run(OrderMain.class,args);
}
}

4.业务类

Api+@FeignClient 重点
@Component
@FeignClient(value = "cloud-payment-service" )
public interface PaymentFeignService {

    @GetMapping(value = "/ordernacos/getnacos")
    public CommonResult getnacos();
}

 

5. Openfeign 负载均衡,超时控制,日志功能

5.1 自带负责均衡,因为openfeign使用Ribbon

 

  5.2 超时设置 默认是1秒

ribbon:
  ReadTimeout:  5000
  ConnectTimeout: 5000
 

 5.3 日志功能

  配置日志

@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}
 

  yml配置日志级别

logging:
  level:
    cn.emergen.springcloud.FeignService: debug

  

 

posted @ 2022-04-21 17:34  王叫兽  阅读(70)  评论(0编辑  收藏  举报