openFeign

 

----------------------------------------------------------------------------------------
###################  spring cloud openFeign #########################################

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


server:
  port: 80

spring:
  application:
    name: cloud-consumer-openfeign-order
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true #优先使用服务ip进行注册
        service-name: ${spring.application.name}



@SpringBootApplication
@EnableDiscoveryClient //该注解用于向使用consul为注册中心时注册服务
@EnableFeignClients//启用feign客户端,定义服务+绑定接口,以声明式的方法优雅而简单的实现服务调用




@FeignClient(value = "cloud-payment-service")
public interface PayFeignApi
{
    @PostMapping(value = "/pay/add")
    public ResultData addPay(@RequestBody PayDTO payDTO);

    @GetMapping(value = "/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id);

    @GetMapping(value = "/pay/get/info")
    public String mylb();
}



http://localhost/feign/pay/get/1
http://localhost/feign/pay/all
http://localhost/feign/pay/add
{
    "payNo": "payString1",
    "orderNo": "abcasdf",
    "userId": "1",
    "amount" : "9.99"
}
http://localhost/feign/pay/del/1
http://localhost/feign/pay/update
{
    "id":"3",
    "payNo": "payUpdate",
    "orderNo": "updateNo11111",
    "userId": "3",
    "amount" : "9.99"
}
http://localhost/feign/pay/get/mylb

openFeign默认集成了loadbalancer

超时控制
openFeign默认超时时间60秒。
spring:
    cloud:
        openfeign:
            client:
                config:
                    default:
                        connectTimeout: 5000
                        readTimeout: 5000
                        loggerLevel: basic


重试机制
openFeign默认重试机制是关闭的。
@Configuration
public class FeignConfig
{
    @Bean
    public Retryer myRetryer()
    {
        //最大请求次数为3(1+2),初始间隔时间为100ms,重试间最大间隔时间为1s
        return new Retryer.Default(100,1,3);
    }
}

默认httpclient修改
        <!-- httpclient5-->
        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.3</version>
        </dependency>
        <!-- feign-hc5-->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-hc5</artifactId>
            <version>13.1</version>
        </dependency>

spring:
  cloud:
    openfeign:
      httpclient:
        hc5:
          enabled: true


请求响应压缩
spring:
  cloud:
    openfeign:
      compression:
        request:
          enabled: true
          min-request-size: 2048 #最小触发压缩的大小
          mime-types: text/xml,application/xml,application/json #触发压缩数据类型
        response:
          enabled: true
            # 开启circuitbreaker和分组激活 spring.cloud.openfeign.circuitbreaker.enabled


日志打印功能
@Configuration
public class FeignConfig
{
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

# feign日志以什么级别监控哪个接口
logging:
  level:
    com:
      ye:
        apis:
          PayFeignApi: debug


----------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2025-12-21 21:01  yebinghuai-qq-com  阅读(0)  评论(0)    收藏  举报

导航