Commit a0a6a43b by Jason Song

get timeout config from server config service

parent bf722d5c
...@@ -41,6 +41,11 @@ public class ServerConfigService { ...@@ -41,6 +41,11 @@ public class ServerConfigService {
return serverConfig == null ? null : serverConfig.getValue(); return serverConfig == null ? null : serverConfig.getValue();
} }
public String getValue(String key, String defaultValue) {
String value = getValue(key);
return value == null ? defaultValue : value;
}
String getDataCenter() { String getDataCenter() {
return Foundation.server().getDataCenter(); return Foundation.server().getDataCenter();
} }
......
package com.ctrip.framework.apollo.common.auth;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
@Target({ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@AuthenticationPrincipal
public @interface ActiveUser {
}
...@@ -5,7 +5,6 @@ import javax.annotation.PostConstruct; ...@@ -5,7 +5,6 @@ import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.ctrip.framework.apollo.common.auth.RestTemplateFactory;
import com.ctrip.framework.apollo.core.enums.Env; import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.service.ServiceLocator; import com.ctrip.framework.apollo.portal.service.ServiceLocator;
......
package com.ctrip.framework.apollo.common.auth; package com.ctrip.framework.apollo.portal.api;
import com.google.common.io.BaseEncoding; import com.google.common.io.BaseEncoding;
import com.ctrip.framework.apollo.portal.service.ServerConfigService;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
...@@ -26,6 +28,9 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ ...@@ -26,6 +28,9 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ
@Autowired @Autowired
private HttpMessageConverters httpMessageConverters; private HttpMessageConverters httpMessageConverters;
@Autowired
private ServerConfigService serverConfigService;
private RestTemplate restTemplate; private RestTemplate restTemplate;
public RestTemplate getObject() { public RestTemplate getObject() {
...@@ -56,9 +61,22 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ ...@@ -56,9 +61,22 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ
restTemplate = new RestTemplate(httpMessageConverters.getConverters()); restTemplate = new RestTemplate(httpMessageConverters.getConverters());
HttpComponentsClientHttpRequestFactory requestFactory = HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient); new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setConnectTimeout(3000); requestFactory.setConnectTimeout(getConnectTimeout());
requestFactory.setReadTimeout(10000); requestFactory.setReadTimeout(getReadTimeout());
restTemplate.setRequestFactory(requestFactory); restTemplate.setRequestFactory(requestFactory);
} }
private int getConnectTimeout() {
String connectTimeout = serverConfigService.getValue("api.connectTimeout", "3000");
return Integer.parseInt(connectTimeout);
}
private int getReadTimeout() {
String readTimeout = serverConfigService.getValue("api.readTimeout", "10000");
return Integer.parseInt(readTimeout);
}
} }
...@@ -42,11 +42,23 @@ public class CtripUserService implements UserService { ...@@ -42,11 +42,23 @@ public class CtripUserService implements UserService {
private ClientHttpRequestFactory clientHttpRequestFactory() { private ClientHttpRequestFactory clientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(3000); factory.setConnectTimeout(getConnectTimeout());
factory.setConnectTimeout(5000); factory.setReadTimeout(getReadTimeout());
return factory; return factory;
} }
private int getConnectTimeout() {
String connectTimeout = serverConfigService.getValue("api.connectTimeout", "3000");
return Integer.parseInt(connectTimeout);
}
private int getReadTimeout() {
String readTimeout = serverConfigService.getValue("api.readTimeout", "3000");
return Integer.parseInt(readTimeout);
}
@Override @Override
public List<UserInfo> searchUsers(String keyword, int offset, int limit) { public List<UserInfo> searchUsers(String keyword, int offset, int limit) {
UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit); UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit);
......
...@@ -28,4 +28,9 @@ public class ServerConfigService { ...@@ -28,4 +28,9 @@ public class ServerConfigService {
return serverConfig == null ? null : serverConfig.getValue(); return serverConfig == null ? null : serverConfig.getValue();
} }
public String getValue(String key, String defaultValue) {
String value = getValue(key);
return value == null ? defaultValue : value;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment