目標
- 模擬實現(xiàn)熔斷機制
account增加Hystrix依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
account 增加fallBack處理
@Component
public class WalletRemoteFallBack implements WalletRemoteService {
@Override
public String getWallet() {
return "fall back";
}
}
配置開啟hystrix
# 端口號
server.port=8000
# 服務(wù)名
spring.application.name=account
# eureka服務(wù)注冊中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/
spring.cloud.loadbalancer.retry.enabled=true
feign.hystrix.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
ribbon.ConnectTimeout=3000
ribbon.ReadTimeout=3000
ribbon.OkToRetryOnAllOperations=true
ribbon.MaxAutoRetries=1
ribbon.MaxAutoRetriesNextServer=1
遠程調(diào)用類修改
@FeignClient(value = "wallet",fallback = WalletRemoteFallBack.class)
public interface WalletRemoteService {
@GetMapping("getWallet")
String getWallet();
}
關(guān)閉wallet應用幼驶,并啟動account
訪問 http://localhost:8000/userInfo
可以看出觸發(fā)熔斷機制,返回 fallBack 信息