Commit cf68a533 by QIANGLU

Merge branch 'dev'

parents 6b053ac5 71f27b11
......@@ -31,6 +31,20 @@
<artifactId>apollo-client</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.secoo.mall.logs.boot;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.util.StringUtils;
/**
* 用于启动后数据加载
* @author qianglu
*/
public class MatrixApplicationRunner implements ApplicationRunner {
private String url;
public MatrixApplicationRunner(String url) {
if(!StringUtils.isEmpty(url)){
this.url = url;
}
}
@Override
public void run(ApplicationArguments args) throws Exception {
}
}
......@@ -5,12 +5,16 @@ import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration;
import com.secoo.mall.logs.endpoint.LogEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
......@@ -19,6 +23,7 @@ import java.util.Set;
/**
* 用于提供动态日志切换
*
* @author qianglu
*/
@Configuration
......@@ -44,11 +49,11 @@ public class MatrixLogListenerConfiguration implements InitializingBean {
private void refreshLoggingLevels() {
Set<String> keyNames = config.getPropertyNames();
for (String key : keyNames) {
if (StringUtils.startsWithIgnoreCase(key,LOGGER_TAG)) {
if (StringUtils.startsWithIgnoreCase(key, LOGGER_TAG)) {
String strLevel = config.getProperty(key, "info");
LogLevel level = LogLevel.valueOf(strLevel.toUpperCase());
loggingSystem.setLogLevel(key.replace(LOGGER_TAG, ""), level);
logger.info("update class {} logger level to {}", key, strLevel);
logger.info("update class {} logger level to {}" , key, strLevel);
}
}
}
......@@ -58,4 +63,11 @@ public class MatrixLogListenerConfiguration implements InitializingBean {
public void afterPropertiesSet() throws Exception {
refreshLoggingLevels();
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public LogEndpoint logEndpoint() {
return new LogEndpoint();
}
}
package com.secoo.mall.logs.endpoint;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import org.slf4j.impl.StaticLoggerBinder;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import java.util.LinkedList;
import java.util.List;
/**
* @author qianglu
*/
@Endpoint(id = "matrixlog")
public class LogEndpoint {
@ReadOperation
public List<String> matrixlog() {
List<String> result = new LinkedList<>();
LoggerContext factory = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();
List<Logger> li = factory.getLoggerList();
li.forEach(logs -> result.add(logs.getName()));
return result;
}
}
package com.secoo.mall.logs.utils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* httpClient封装
*
* @author QIANG
*/
public class HttpClientUtils {
private static Logger LOG = LoggerFactory.getLogger(HttpClientUtils.class);
private static PoolingHttpClientConnectionManager connMgr;
private static RequestConfig requestConfig;
private static final int CONNECT_TIMEOUT = 2000;
private static final int SOCKET_TIMEOUT = 2000;
private static final int REQUEST_TIMEOUT = 2000;
private static final String CHARSET = "UTF-8";
static {
// 设置连接池
connMgr = new PoolingHttpClientConnectionManager();
// 设置连接池大小
connMgr.setMaxTotal(300);
connMgr.setDefaultMaxPerRoute(300);
RequestConfig.Builder configBuilder = RequestConfig.custom();
// 请求超时时间
configBuilder.setConnectTimeout(CONNECT_TIMEOUT);
// 连接不够时等待超时时间,不设置将阻塞线程
configBuilder.setConnectionRequestTimeout(REQUEST_TIMEOUT);
// 等待数据超时时间
configBuilder.setSocketTimeout(SOCKET_TIMEOUT);
LOG.debug("Http参数设置,连接超时时间[{}],Socket超时时间[{}],请求超时时间[{}]", CONNECT_TIMEOUT, SOCKET_TIMEOUT);
requestConfig = configBuilder.build();
}
/**
* 设置连接超时和请求超时
*
* @param connectTimeout
* @param socketTimeout
* @param connectionRequestTimeout
*/
public static void setHttpParam(int connectTimeout, int socketTimeout, int connectionRequestTimeout) {
RequestConfig.Builder configBuilder = RequestConfig.custom();
configBuilder.setConnectTimeout(connectTimeout);
configBuilder.setSocketTimeout(socketTimeout);
configBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
LOG.debug("Http参数自定义设置,请求超时时间[{}],等待数据超时时间[{}],连接不够时等待超时时间[{}]", connectTimeout, socketTimeout,
connectionRequestTimeout);
requestConfig = configBuilder.build();
}
/**
* 发送 GET 请求(HTTP),不带输入数据
*
* @param url
* @return
* @throws Exception
*/
public static String doGet(String url) throws Exception {
return doGet(url, new HashMap<>(10));
}
/**
* 发送 GET 请求(HTTP),K-V形式
*
* @param url
* @param params
* @return
* @throws Exception
*/
public static String doGet(String url, Map<String, Object> params) throws Exception {
return doGet(url, params, CHARSET, new HashMap<>(16));
}
public static String doGet(String url, Map<String, Object> params, String charset, Map<String, String> headers) throws Exception {
String result = null;
if (StringUtils.isEmpty(url)) {
LOG.info("warn:doGet url is null or '' ");
return result;
}
List<NameValuePair> pairList = new ArrayList<>(params.size());
for (Map.Entry<String, Object> entry : params.entrySet()) {
pairList.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setConnectionManager(connMgr).build();
CloseableHttpResponse response = null;
InputStream instream = null;
try {
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.setCharset(Charset.forName(StringUtils.isEmpty(charset) ? CHARSET : charset));
uriBuilder.addParameters(pairList);
URI uri = uriBuilder.build();
HttpGet httpGet = new HttpGet(uri);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
response = httpclient.execute(httpGet);
LOG.info("doGet statusCode:{}", response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
if (entity != null) {
instream = entity.getContent();
result = IOUtils.toString(instream, charset);
}
} finally {
if (null != instream) {
instream.close();
}
if (null != response) {
response.close();
}
LOG.info("close instream response httpClient connection succ");
}
return result;
}
/**
* 发送 PUT 请求(HTTP),K-V形式
*
* @param url
* @param params
* @return
* @throws Exception
*/
public static String doPut(String url, Map<String, Object> params, String charset, Map<String, String> headers) throws Exception {
String result = null;
if (StringUtils.isEmpty(url)) {
LOG.info("warn:doGet url is null or '' ");
return result;
}
List<NameValuePair> pairList = new ArrayList<>(params.size());
for (Map.Entry<String, Object> entry : params.entrySet()) {
pairList.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setConnectionManager(connMgr).build();
CloseableHttpResponse response = null;
InputStream instream = null;
try {
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.setCharset(Charset.forName(StringUtils.isEmpty(charset) ? CHARSET : charset));
uriBuilder.addParameters(pairList);
URI uri = uriBuilder.build();
HttpPut httpPut = new HttpPut(uri);
if (headers != null && headers.size() > 0) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPut.setHeader(entry.getKey(), entry.getValue());
}
}
response = httpclient.execute(httpPut);
LOG.info("doGet statusCode:{}", response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
if (entity != null) {
instream = entity.getContent();
result = IOUtils.toString(instream, charset);
}
} finally {
if (null != instream) {
instream.close();
}
if (null != response) {
response.close();
}
LOG.info("close instream response httpClient connection succ");
}
return result;
}
/**
* 发送 POST 请求(HTTP),不带输入数据
*
* @param url
* @return
* @throws Exception
*/
public static String doPost(String url) throws Exception {
return doPost(url, new HashMap<>(16));
}
/**
* 发送 POST 请求(HTTP),K-V形式
*
* @param url API接口URL
* @param params 参数map
* @return
* @throws Exception
*/
public static String doPost(String url, Map<String, Object> params) throws Exception {
if (StringUtils.isEmpty(url)) {
LOG.info("warn:doPost url is null or '' ");
return null;
}
List<NameValuePair> pairList = new ArrayList<>(params.size());
for (Map.Entry<String, Object> entry : params.entrySet()) {
pairList.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName(CHARSET)));
return send(httpPost, false);
}
/**
* 发送 POST 请求(HTTP),xml
*
* @param url API接口URL
* @param xml xml 参数
* @return
* @throws Exception
*/
public static String doPost(String url, String xml) throws Exception {
if (StringUtils.isEmpty(url)) {
LOG.info("warn:doPost url is null or '' ");
return null;
}
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new StringEntity(xml, "GBK"));
return send(httpPost, false);
}
/**
* 发送 POST 请求(HTTP),JSON形式
*
* @param url
* @param json json对象
* @return
* @throws Exception
*/
public static String doPost(String url, Object json) throws Exception {
if (StringUtils.isEmpty(url)) {
LOG.error("warn:doPostByJson url is null or '' ");
return null;
}
HttpPost httpPost = new HttpPost(url);
StringEntity stringEntity = new StringEntity(json.toString(), CHARSET);
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
return send(httpPost, false);
}
public static String doPost(String url, StringEntity entity, Map<String, String> headers) throws Exception {
if (StringUtils.isEmpty(url)) {
LOG.error("warn:doPostByJson url is null or '' ");
return null;
}
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
headers.forEach((k, v) -> {
httpPost.setHeader(k, v);
});
return send(httpPost, false);
}
/**
* 发送 SSL POST 请求(HTTPS),K-V形式
*
* @param apiUrl API接口URL
* @param params 参数map
* @return
* @throws Exception
*/
public static String doPostSSL(String apiUrl, Map<String, Object> params) throws Exception {
if (StringUtils.isEmpty(apiUrl)) {
LOG.info("warn:doPostSSL url is null or '' ");
return null;
}
List<NameValuePair> pairList = new ArrayList<>(params.size());
for (Map.Entry<String, Object> entry : params.entrySet()) {
NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
pairList.add(pair);
}
HttpPost httpPost = new HttpPost(apiUrl);
httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("utf-8")));
return send(httpPost, true);
}
/**
* 发送 SSL POST 请求(HTTPS),JSON形式
*
* @param apiUrl API接口URL
* @param json JSON对象
* @return
* @throws Exception
*/
public static String doPostSSL(String apiUrl, Object json) throws Exception {
if (StringUtils.isEmpty(apiUrl)) {
LOG.info("warn:doPostSSL By Json url is null or '' ");
return null;
}
StringEntity stringEntity = new StringEntity(json.toString(), CHARSET);
HttpPost httpPost = new HttpPost(apiUrl);
httpPost.setEntity(stringEntity);
stringEntity.setContentEncoding(CHARSET);
stringEntity.setContentType("application/json");
return send(httpPost, true);
}
/**
* 发送Post请求
*
* @param httpPost
* @return
* @throws IOException
* @throws ClientProtocolException
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
private static String send(HttpPost httpPost, boolean isSsl) throws ClientProtocolException, IOException,
KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
CloseableHttpClient httpClient = null;
InputStream instream = null;
String result = null;
CloseableHttpResponse response = null;
try {
if (isSsl) {
SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
httpClient = HttpClients.custom().setConnectionManager(connMgr).setSSLSocketFactory(sslsf)
.setDefaultRequestConfig(requestConfig).build();
} else {
httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(connMgr)
.build();
}
response = httpClient.execute(httpPost);
LOG.info("doPost statusCode:{}", response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
if (entity != null) {
instream = entity.getContent();
result = IOUtils.toString(instream, CHARSET);
LOG.info("doPost Result:{}", result);
}
} finally {
if (null != instream) {
instream.close();
}
if (null != response) {
response.close();
}
LOG.info("close instream response httpClient connection succ");
}
return result;
}
}
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