1.引入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--這個(gè)依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.配置
server:
port: 6063
spring:
application:
name: consumer-feign
cloud:
loadbalancer:
retry:
enabled: true # 開(kāi)啟Spring Cloud的重試功能
main:
allow-bean-definition-overriding: true
logging:
level:
com.d4c: debug
eureka:
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 # 開(kāi)啟Feign的熔斷功能
compression:
request:
enabled: true # 開(kāi)啟請(qǐng)求壓縮
mime-types: text/html,application/xml,application/json # 設(shè)置壓縮的數(shù)據(jù)類型
min-request-size: 2048 # 設(shè)置觸發(fā)壓縮的大小下限
response:
enabled: true # 開(kāi)啟響應(yīng)壓縮
ribbon:
ConnectTimeout: 250 # Ribbon的連接超時(shí)時(shí)間
ReadTimeout: 1000 # Ribbon的數(shù)據(jù)讀取超時(shí)時(shí)間
OkToRetryOnAllOperations: true # 是否對(duì)所有操作都進(jìn)行重試
MaxAutoRetriesNextServer: 1 # 切換實(shí)例的重試次數(shù)
MaxAutoRetries: 1 # 對(duì)當(dāng)前實(shí)例的重試次數(shù)
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 6000 # 設(shè)置hystrix的超時(shí)時(shí)間為6000ms
3.相關(guān)類
啟動(dòng)類
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix//或者@EnableCircuitBreaker
@EnableFeignClients
public class ConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerFeignApplication.class, args);
}
}
FeignClient接口
@FeignClient(value = "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接口實(shí)現(xiàn)類
@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!";
}
}
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級(jí)別是FULL,F(xiàn)eign支持4種級(jí)別:
NONE:不記錄任何日志信息,這是默認(rèn)值。
BASIC:僅記錄請(qǐng)求的方法,URL以及響應(yīng)狀態(tài)碼和執(zhí)行時(shí)間
HEADERS:在BASIC的基礎(chǔ)上,額外記錄了請(qǐng)求和響應(yīng)的頭信息
FULL:記錄所有請(qǐng)求和響應(yīng)的明細(xì),包括頭信息、請(qǐng)求體、元數(shù)據(jù)。