HttpInvoker消费者调用时,配置httpInvokerRequestExecutor这个bean。在服务注册框架里,这个bean应该由服务注册框架的消费者xml提供。如果不配置这个bean,默认是SimpleHttpInvokerRequestExecutor,这个不适用于线上环境,它直接使用java.net中的HttpURLConnection。
<bean id="commonHttpInvokerRequestExecutor"
class="com.gdl.consumer.httpinvoker.HystrixHttpInvokerRequestExecutorOfGdl">
<property name="httpClient" ref="httpclient" />
<property name="mark" value="${service.mark}"></property>
<property name="markMap" value="${service.markMap}"></property>
<property name="callServerType" value="${callServerType}" />
</bean>
如名所见,这里的httpInvokerRequestExecutor支持Hystrix。相比于SimpleHttpInvokerRequestExecutor,我们至少要让他具有如下功能:
- 使用HttpClient作为底层通信机制
- 自动从注册中心拿到服务的url
- 具有Hystrix功能
- 具有cat打点功能
Hystrix功能的核心代码如下
HystrixCommand<RemoteInvocationResult> theCommand = new HystrixCommand<RemoteInvocationResult>(theSetter) {
@Override
protected RemoteInvocationResult run() {
try {
try {
RemoteInvocationResult ret = doExecuteProxy(config, baos);
return ret;
} catch (Exception e) {
}
return new RemoteInvocationResult(new RuntimeException("Hystrix ERROR"));
}finally {
}
}
@Override
protected RemoteInvocationResult getFallback(){
return new RemoteInvocationResult(new RuntimeException("Hystrix-Fallback"));
}
};
return theCommand.execute();