SWITCH DATASOURCE
package jojo.speedo.datasource;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSource {
String value() default "dataSource2";
}
package jojo.speedo.datasource;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DataSourceAspect {
static final Logger log = LoggerFactory.getLogger(DataSourceAspect.class);
public void intercept(JoinPoint point) throws Exception {
Class<?> target = point.getTarget().getClass();
MethodSignature signature = (MethodSignature) point.getSignature();
// 默认使用目标类型的注解,如果没有则使用其实现接口的注解
// for (Class<?> clazz : target.getInterfaces()) {
resolveDataSource(target, signature.getMethod());
// }
resolveDataSource(target, signature.getMethod());
}
private void resolveDataSource(Class<?> clazz, Method method) {
try {
DynamicDataSourceHolder.clearDataSource();
Class<?>[] types = method.getParameterTypes();
// 默认使用类型注解
if (clazz.isAnnotationPresent(DataSource.class)) {
DataSource source = clazz.getAnnotation(DataSource.class);
DynamicDataSourceHolder.setDataSource(source.value());
} else {
DynamicDataSourceHolder.setDataSource("dataSource");
}
//log.info("curent datasource : " + DynamicDataSourceHolder.getDataSource());
// 方法注解可以覆盖类型注解
// Method m = clazz.getMethod(method.getName(), types);
// if (m != null && m.isAnnotationPresent(DataSource.class)) {
// DataSource source = m.getAnnotation(DataSource.class);
// DynamicDataSourceHolder.setDataSource(source.value());
// } else {
// DynamicDataSourceHolder.setDataSource("dataSource");
// }
} catch (Exception e) {
log.error(clazz + ":" ,e);
}
}
}
package jojo.speedo.datasource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceHolder.getDataSource();
}
}
package jojo.speedo.datasource;
public class DynamicDataSourceHolder {
private static final ThreadLocal<String> THREAD_DATA_SOURCE = new ThreadLocal<String>();
public static String getDataSource() {
return THREAD_DATA_SOURCE.get();
}
public static void setDataSource(String dataSource) {
THREAD_DATA_SOURCE.set(dataSource);
}
public static void clearDataSource() {
THREAD_DATA_SOURCE.remove();
}
}
HTTP CLIENT
package jojo.speedo.common.httpclient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
public class HttpclientBuilder {
private HttpClientConnectionManager connectionManager;
private SSLConnectionSocketFactory sslSocketFactory;
private RequestConfig defaultRequestConfig;
public CloseableHttpClient build() {
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(connectionManager);
builder.setSSLSocketFactory(sslSocketFactory);
builder.setDefaultRequestConfig(defaultRequestConfig);
return builder.build();
}
public HttpClientConnectionManager getConnectionManager() {
return connectionManager;
}
public void setConnectionManager(HttpClientConnectionManager connectionManager) {
this.connectionManager = connectionManager;
}
public SSLConnectionSocketFactory getSslSocketFactory() {
return sslSocketFactory;
}
public void setSslSocketFactory(SSLConnectionSocketFactory sslSocketFactory) {
this.sslSocketFactory = sslSocketFactory;
}
public RequestConfig getDefaultRequestConfig() {
return defaultRequestConfig;
}
public void setDefaultRequestConfig(RequestConfig defaultRequestConfig) {
this.defaultRequestConfig = defaultRequestConfig;
}
}
package jojo.speedo.common.httpclient;
import java.net.InetAddress;
import java.util.Collection;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
public class HttpRequestConfigBuilder {
private boolean expectContinueEnabled;
private HttpHost proxy;
private InetAddress localAddress;
private boolean staleConnectionCheckEnabled;
private String cookieSpec;
private boolean redirectsEnabled;
private boolean relativeRedirectsAllowed;
private boolean circularRedirectsAllowed;
private int maxRedirects;
private boolean authenticationEnabled;
private Collection<String> targetPreferredAuthSchemes;
private Collection<String> proxyPreferredAuthSchemes;
private int connectionRequestTimeout;
private int connectTimeout;
private int socketTimeout;
public boolean isExpectContinueEnabled() {
return expectContinueEnabled;
}
public void setExpectContinueEnabled(boolean expectContinueEnabled) {
this.expectContinueEnabled = expectContinueEnabled;
}
public HttpHost getProxy() {
return proxy;
}
public void setProxy(HttpHost proxy) {
this.proxy = proxy;
}
public InetAddress getLocalAddress() {
return localAddress;
}
public void setLocalAddress(InetAddress localAddress) {
this.localAddress = localAddress;
}
public boolean isStaleConnectionCheckEnabled() {
return staleConnectionCheckEnabled;
}
public void setStaleConnectionCheckEnabled(boolean staleConnectionCheckEnabled) {
this.staleConnectionCheckEnabled = staleConnectionCheckEnabled;
}
public String getCookieSpec() {
return cookieSpec;
}
public void setCookieSpec(String cookieSpec) {
this.cookieSpec = cookieSpec;
}
public boolean isRedirectsEnabled() {
return redirectsEnabled;
}
public void setRedirectsEnabled(boolean redirectsEnabled) {
this.redirectsEnabled = redirectsEnabled;
}
public boolean isRelativeRedirectsAllowed() {
return relativeRedirectsAllowed;
}
public void setRelativeRedirectsAllowed(boolean relativeRedirectsAllowed) {
this.relativeRedirectsAllowed = relativeRedirectsAllowed;
}
public boolean isCircularRedirectsAllowed() {
return circularRedirectsAllowed;
}
public void setCircularRedirectsAllowed(boolean circularRedirectsAllowed) {
this.circularRedirectsAllowed = circularRedirectsAllowed;
}
public int getMaxRedirects() {
return maxRedirects;
}
public void setMaxRedirects(int maxRedirects) {
this.maxRedirects = maxRedirects;
}
public boolean isAuthenticationEnabled() {
return authenticationEnabled;
}
public void setAuthenticationEnabled(boolean authenticationEnabled) {
this.authenticationEnabled = authenticationEnabled;
}
public Collection<String> getTargetPreferredAuthSchemes() {
return targetPreferredAuthSchemes;
}
public void setTargetPreferredAuthSchemes(Collection<String> targetPreferredAuthSchemes) {
this.targetPreferredAuthSchemes = targetPreferredAuthSchemes;
}
public Collection<String> getProxyPreferredAuthSchemes() {
return proxyPreferredAuthSchemes;
}
public void setProxyPreferredAuthSchemes(Collection<String> proxyPreferredAuthSchemes) {
this.proxyPreferredAuthSchemes = proxyPreferredAuthSchemes;
}
public int getConnectionRequestTimeout() {
return connectionRequestTimeout;
}
public void setConnectionRequestTimeout(int connectionRequestTimeout) {
this.connectionRequestTimeout = connectionRequestTimeout;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public int getSocketTimeout() {
return socketTimeout;
}
public void setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
}
public RequestConfig build() {
RequestConfig.Builder builder = RequestConfig.custom();
builder.setExpectContinueEnabled(expectContinueEnabled);
builder.setProxy(proxy);
builder.setLocalAddress(localAddress);
builder.setStaleConnectionCheckEnabled(staleConnectionCheckEnabled);
builder.setCookieSpec(cookieSpec);
builder.setRedirectsEnabled(redirectsEnabled);
builder.setRelativeRedirectsAllowed(relativeRedirectsAllowed);
builder.setCircularRedirectsAllowed(circularRedirectsAllowed);
builder.setMaxRedirects(maxRedirects);
builder.setAuthenticationEnabled(authenticationEnabled);
builder.setTargetPreferredAuthSchemes(targetPreferredAuthSchemes);
builder.setProxyPreferredAuthSchemes(proxyPreferredAuthSchemes);
builder.setConnectionRequestTimeout(connectionRequestTimeout);
builder.setConnectTimeout(connectTimeout);
builder.setSocketTimeout(socketTimeout);
return builder.build();
}
}
package jojo.speedo.common.httpclient;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
public class InSecureSSLConnectionSocketFactoryBuilder {
public static final SSLConnectionSocketFactory build() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
SSLContext sslContext = new org.apache.http.ssl.SSLContextBuilder().loadTrustMaterial(null, new org.apache.http.ssl.TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
return sslSocketFactory;
}
}
package jojo.speedo.common.httpclient;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
public class SocketFactoryRegistryBuilder {
public static Registry<ConnectionSocketFactory> build(SSLConnectionSocketFactory sslSocketFactory){
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
return socketFactoryRegistry;
}
}
网友评论