1.引入依赖
要排除hystrix-core里的archaius-core,否则报错
<dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-core</artifactId> <version>1.5.12</version> <exclusions> <exclusion> <artifactId>archaius-core</artifactId> <groupId>com.netflix.archaius</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.netflix.archaius</groupId> <artifactId>archaius-core</artifactId> <version>0.7.4</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency>
2.定义HttpHystrixCommand类
import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommandGroupKey; import com.netflix.hystrix.exception.HystrixRuntimeException; import com.netflix.hystrix.exception.HystrixTimeoutException; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import lombok.extern.slf4j.Slf4j; @Slf4j public class HttpHystrixCommand extends HystrixCommand<ResponseEntity<String>> { private org.springframework.http.HttpMethod httpMethod; private HttpEntity httpEntity; private String url; private RestTemplate restTemplate = new RestTemplate(); public HttpHystrixCommand(String url, HttpMethod httpMethod, HttpEntity<?> httpEntity) { // 设置3秒超时 super(HystrixCommandGroupKey.Factory.asKey("http_hystrix_command_group_key"), 3000); this.httpMethod = httpMethod; this.httpEntity = httpEntity; this.url = url; } public HttpHystrixCommand(String url, HttpEntity<?> httpEntity) { this(url, HttpMethod.POST, httpEntity); } public HttpHystrixCommand(String url) { this(url, HttpMethod.GET, null); } public ResponseEntity<String> post() { this.httpMethod = HttpMethod.POST; return execute(); } public ResponseEntity<String> get() { this.httpMethod = HttpMethod.GET; this.httpEntity = null; return execute(); } @Override protected ResponseEntity<String> run() throws Exception { // Thread.sleep(3000); return restTemplate.exchange(url, httpMethod, (HttpEntity) httpEntity, String.class); } @Override protected ResponseEntity<String> getFallback() { Throwable executionException = getExecutionException(); if (executionException instanceof HystrixTimeoutException) { log.error("请求因超时融断!url:{} param:{}", this.url, this.httpEntity, executionException); } else if (executionException instanceof HystrixRuntimeException) { log.error("请求直接融断!url:{} param:{}", this.url, this.httpEntity, executionException); } else { log.error("请求异常!url:{} param:{}", this.url, this.httpEntity, executionException); } return new ResponseEntity<String>(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR); } }
3.测试
import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springside.modules.utils.mapper.JsonMapper; import java.util.Collections; import lombok.extern.slf4j.Slf4j; /** * */ @Slf4j public class HttpHystrixCommandTest { public static void main(String[] args) { ResponseEntity<String> response = new HttpHystrixCommand( "https://aaa.xxx.com/setting/get?key=key1&source=h5") .get(); if (response.getStatusCode().value() == org.springframework.http.HttpStatus.OK.value()) { log.info("请求成功"); }else{ log.info("请求失败"); } } private static HttpHeaders defaultRequestHeaders = new HttpHeaders(); static { defaultRequestHeaders.setContentType(MediaType.APPLICATION_JSON); defaultRequestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); } public static HttpEntity<String> getHttpEntity(Object postData) { return new HttpEntity<>(JsonMapper.INSTANCE.toJson(postData), defaultRequestHeaders); } public static HttpEntity<String> getHttpEntity(String postData) { return new HttpEntity<>(postData, defaultRequestHeaders); } }
4.正常的返回如下
INFO HttpHystrixCommandTest - 请求成功
5.异常(增加代码Thread.sleep(3000);)的返回如下:
ERROR HttpHystrixCommand - 请求因超时融断!url:https://aaa.xxx.com/setting/get?key=depository&source=h5 param:null
com.netflix.hystrix.exception.HystrixTimeoutException: null