spring|spring cloud之feign
1.引入依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.retry
spring-retry
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
2.配置
server:
port: 6063spring:
application:
name: consumer-feign
cloud:
loadbalancer:
retry:
enabled: true # 开启Spring Cloud的重试功能
main:
allow-bean-definition-overriding: true
logging:
level:
com.d4c: debugeureka:
instance:
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://peer1:6001/eureka/,http://peer2:6002/eureka/feign:
hystrix:
enabled: true # 开启Feign的熔断功能
compression:
request:
enabled: true # 开启请求压缩
mime-types: text/html,application/xml,application/json # 设置压缩的数据类型
min-request-size: 2048 # 设置触发压缩的大小下限
response:
enabled: true # 开启响应压缩ribbon:
ConnectTimeout: 250 # Ribbon的连接超时时间
ReadTimeout: 1000 # Ribbon的数据读取超时时间
OkToRetryOnAllOperations: true # 是否对所有操作都进行重试
MaxAutoRetriesNextServer: 1 # 切换实例的重试次数
MaxAutoRetries: 1 # 对当前实例的重试次数hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 6000 # 设置hystrix的超时时间为6000ms
3.相关类 启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix//或者@EnableCircuitBreaker
@EnableFeignClients
public class ConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerFeignApplication.class, args);
}
}
FeignClient接口
@FeignClient(value = "https://www.it610.com/article/account-demo",fallback = AccountClientFallback.class)
public interface AccountClient {@GetMapping("account/get/server/{id}")
String queryAccountById(@PathVariable Long id);
@GetMapping("account/get/{id}")
String queryAccountByIdTwo(@PathVariable Long id);
}
FeignClient接口实现类
@Component
public class AccountClientFallback implements AccountClient {
@Override
public String queryAccountById(Long id) {
return "I am fallback!";
}@Override
public String queryAccountByIdTwo(Long id) {
return "I am fallback two!";
}
}
【spring|spring cloud之feign】service类
@Service
public class AccountConsumerService {@Autowired
private AccountClient accountClient;
public String queryAccountById(Long id) {
String s = accountClient.queryAccountById(id);
return s;
}public String queryAccountByIdTwo(Long id) {
String s = accountClient.queryAccountByIdTwo(id);
return s;
}
}
controller类
@RestController
@RequestMapping("account")
public class AccountConsumerController {@Resource
private AccountConsumerService accountConsumerService;
@RequestMapping("/{id}")
public String queryAccountById(@PathVariable Long id){
return accountConsumerService.queryAccountById(id);
}@RequestMapping("/two/{id}")
public String queryAccountByIdTwo(@PathVariable Long id){
return accountConsumerService.queryAccountByIdTwo(id);
}}
feign自定的日志类
@Configuration
public class LogLevelConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
这里指定的Level级别是FULL,Feign支持4种级别:
- NONE:不记录任何日志信息,这是默认值。
- BASIC:仅记录请求的方法,URL以及响应状态码和执行时间
- HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息
- FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据。
推荐阅读
- PMSJ寻平面设计师之现代(Hyundai)
- 太平之莲
- 闲杂“细雨”
- 七年之痒之后
- 深入理解Go之generate
- 由浅入深理解AOP
- Activiti(一)SpringBoot2集成Activiti6
- 期刊|期刊 | 国内核心期刊之(北大核心)
- 生活随笔|好天气下的意外之喜
- 感恩之旅第75天