Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls。 -------來自官網java
feign:
hystrix:
enabled: true
複製代碼
server:
port: 8811 # 你的端口
spring:
application:
name: feign
eureka:
client:
service-url:
defaultZone: http://admin:admin@server1:8761/eureka/,http://admin:admin@server2:8762/eureka/
feign:
hystrix:
enabled: true
service-eureka-client:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
#com.netflix.loadbalancer.RandomRule #配置規則 隨機
#com.netflix.loadbalancer.RoundRobinRule #配置規則 輪詢
#com.netflix.loadbalancer.RetryRule #配置規則 重試
#com.netflix.loadbalancer.WeightedResponseTimeRule #配置規則 響應時間權重
#com.netflix.loadbalancer.BestAvailableRule #配置規則 最空閒鏈接策略
複製代碼
package com.hewl.hystrix;
import com.hewl.feign.SchedualCilentName;
public interface SchedualClientNameWithFallbackFactory extends SchedualCilentName {
}
複製代碼
package com.hewl.hystrix;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.hewl.feign.SchedualCilentName;
import feign.hystrix.FallbackFactory;
@Component
public class HystrixClientFallbackFactory implements FallbackFactory<SchedualCilentName> {
private static final Logger log = LoggerFactory.getLogger(HystrixClientFallbackFactory.class);
@Override
public SchedualCilentName create(Throwable cause) {
// TODO Auto-generated method stub
return new SchedualClientNameWithFallbackFactory() {
@Override
public String testFromClientOne(String name) {
log.error("fallback ,the result is : " + cause);
// TODO Auto-generated method stub
Map<String, Object> map = new HashMap<String, Object>();
map.put("ID", -1L);
map.put("NAME","");
return map.toString();
}
};
}
}
複製代碼
package com.hewl.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.hewl.hystrix.HystrixClientFallbackFactory;
@FeignClient(value="service-eureka-client",fallbackFactory = HystrixClientFallbackFactory.class)
public interface SchedualCilentName {
@GetMapping(value = "/test")
public String testFromClientOne(@RequestParam("name") String name);
}
複製代碼