----------------------------------------------------------------------------------------
################### 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-ordercloud:consul:host: localhostport: 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
{@BeanLogger.Level feignLoggerLevel() { return Logger.Level.FULL;}
}# feign日志以什么级别监控哪个接口
logging:level:com:ye:apis:PayFeignApi: debug ----------------------------------------------------------------------------------------