自动化运维,主机资源同步报错修复

develop
Hoshi 2024-11-06 17:39:10 +08:00
parent 0e3e9d1705
commit 36eec7e73b
2 changed files with 42 additions and 3 deletions

View File

@ -2,13 +2,20 @@ package com.bocloud.cop.service.internal;
import com.alibaba.fastjson.JSON;
import com.megatron.common.encrypt.AESEncryptor;
import com.megatron.common.http.ConnectionManagerHolder;
import com.megatron.common.http.HttpClient;
import com.megatron.common.model.GeneralResult;
import com.megatron.common.model.Param;
import com.megatron.common.model.Sign;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@ -17,6 +24,9 @@ import org.springframework.util.Assert;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.List;
@ -42,7 +52,7 @@ public class CmpInternetService {
"?page=1" +
"&rows="+Integer.MAX_VALUE +
"&params="+ URLEncoder.encode(JSON.toJSONString(httpParams), StandardCharsets.UTF_8);
HttpClient httpClient = new HttpClient();
HttpClient httpClient = new IgnoreVerifyHttpClient();
HttpGet httpGet = new HttpGet(httpUrl);
httpGet.addHeader("bsm-token", this.getToken());
CloseableHttpResponse httpResponse = httpClient.getHttpClient().execute(httpGet);
@ -51,6 +61,7 @@ public class CmpInternetService {
Assert.isTrue(httpCode == 200, httpBody);
return JSON.parseObject(httpBody, GeneralResult.class);
} catch (IOException e) {
log.error(e.getMessage(), e);
return new GeneralResult<>(false, e.getMessage());
}
}
@ -64,7 +75,7 @@ public class CmpInternetService {
"?page=1" +
"&rows="+Integer.MAX_VALUE +
"&params="+ URLEncoder.encode(JSON.toJSONString(httpParams), StandardCharsets.UTF_8);
HttpClient httpClient = new HttpClient();
HttpClient httpClient = new IgnoreVerifyHttpClient();
HttpGet httpGet = new HttpGet(httpUrl);
httpGet.addHeader("bsm-token", this.getToken());
CloseableHttpResponse httpResponse = httpClient.getHttpClient().execute(httpGet);
@ -73,6 +84,7 @@ public class CmpInternetService {
Assert.isTrue(httpCode == 200, httpBody);
return JSON.parseObject(httpBody, GeneralResult.class);
} catch (IOException e) {
log.error(e.getMessage(), e);
return new GeneralResult<>(false, e.getMessage());
}
}
@ -86,7 +98,7 @@ public class CmpInternetService {
"?page=1" +
"&rows="+Integer.MAX_VALUE +
"&params="+ URLEncoder.encode(JSON.toJSONString(httpParams), StandardCharsets.UTF_8);
HttpClient httpClient = new HttpClient();
HttpClient httpClient = new IgnoreVerifyHttpClient();
HttpGet httpGet = new HttpGet(httpUrl);
httpGet.addHeader("bsm-token", this.getToken());
CloseableHttpResponse httpResponse = httpClient.getHttpClient().execute(httpGet);
@ -95,6 +107,7 @@ public class CmpInternetService {
Assert.isTrue(httpCode == 200, httpBody);
return JSON.parseObject(httpBody, GeneralResult.class);
} catch (IOException e) {
log.error(e.getMessage(), e);
return new GeneralResult<>(false, e.getMessage());
}
}

View File

@ -0,0 +1,26 @@
package com.bocloud.cop.service.internal;
import com.megatron.common.http.ConnectionManagerHolder;
import com.megatron.common.http.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
public class IgnoreVerifyHttpClient extends HttpClient {
@Override
protected void init() {
try {
this.config = RequestConfig.custom().setSocketTimeout(getTimeout()).setConnectTimeout(getTimeout()).setProxy(this.proxy).setCookieSpec("standard-strict").build();
this.httpClient = HttpClientBuilder.create()
.setSSLContext(new SSLContextBuilder().loadTrustMaterial((chain, authType) -> true).build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setConnectionManager(ConnectionManagerHolder.getConnectionManager())
.setConnectionManagerShared(true)
.setDefaultRequestConfig(this.config).build();
} catch (Exception e) {
throw new RuntimeException("IgnoreVerifyHttpClient init error", e);
}
}
}