feat:first commit
This commit is contained in:
22
xservice-common/pom.xml
Normal file
22
xservice-common/pom.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.xiang</groupId>
|
||||
<artifactId>xservice-basic</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>xservice-common</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.xiang.xservice.basic.common.req;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class BaseRequest {
|
||||
|
||||
/**
|
||||
* 操作人
|
||||
*/
|
||||
private String operator;
|
||||
|
||||
/**
|
||||
* 分页大小
|
||||
*/
|
||||
private Integer pageSize;
|
||||
|
||||
/**
|
||||
* 当前页数
|
||||
*/
|
||||
private Integer current;
|
||||
|
||||
/**
|
||||
* 时间
|
||||
*/
|
||||
private LocalDateTime dateTime;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.xiang.xservice.basic.common.req;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class IdRequest {
|
||||
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.xiang.xservice.basic.common.resp;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2025-05-09 14:09
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Result<T> {
|
||||
|
||||
private String code;
|
||||
|
||||
private String message;
|
||||
|
||||
private List<T> data;
|
||||
|
||||
public static Result<Void> success(String message) {
|
||||
return new Result<Void>("200", message, null);
|
||||
}
|
||||
|
||||
|
||||
public static <T> Result<T> success(String message, List<T> data) {
|
||||
return new Result<T>("200", message, data);
|
||||
}
|
||||
public static <T> Result<T> success(String message, T data) {
|
||||
return new Result<T>("200", message, Collections.singletonList(data));
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(String message) {
|
||||
return new Result<T>("500", message, null);
|
||||
}
|
||||
|
||||
public static <T> Result<T> error(String message, T data) {
|
||||
return new Result<T>("500", message, Collections.singletonList(data));
|
||||
}
|
||||
public static <T> Result<T> error(String message, List<T> data) {
|
||||
return new Result<T>("500", message, data);
|
||||
}
|
||||
|
||||
public static Result<Void> error(String code, String message) {
|
||||
return new Result<Void>(code, message, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.xiang.xservice.basic.config;
|
||||
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class MyThreadFactory implements ThreadFactory {
|
||||
private final String threadName;
|
||||
private final boolean daemon;
|
||||
|
||||
private final AtomicInteger threadNum = new AtomicInteger();
|
||||
|
||||
public MyThreadFactory(String threadName, boolean daemon) {
|
||||
this.threadName = threadName;
|
||||
this.daemon = daemon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
Thread thread = new Thread(r, this.threadName + "[#" + threadNum.incrementAndGet() + "]");
|
||||
thread.setDaemon(this.daemon);
|
||||
return thread;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.xiang.xservice.basic.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum HttpMethodEnums {
|
||||
|
||||
GET("get"),
|
||||
POST("post"),
|
||||
;
|
||||
final String method;
|
||||
|
||||
HttpMethodEnums(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.xiang.xservice.basic.utils;
|
||||
|
||||
public class DateUtils {
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.xiang.xservice.basic.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
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.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2025-05-08 14:39
|
||||
*/
|
||||
@Slf4j
|
||||
public class HttpUtils {
|
||||
|
||||
private static final int socketTimeOut = 60 * 1000;
|
||||
private static final int connectTimeout = 60 * 1000;
|
||||
private static final int connectionRequestTimeout = 15 * 1000;
|
||||
private static final int defaultMaxPerRoute = 500;
|
||||
private static final int maxTotal = 2000;
|
||||
|
||||
public static String doPost(String url, Map<String, String> header, String jsonParams) {
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
// 设置连接超时时间
|
||||
.setConnectTimeout(connectTimeout)
|
||||
// 设置Socket超时时间
|
||||
.setSocketTimeout(socketTimeOut)
|
||||
.setConnectionRequestTimeout(connectionRequestTimeout)
|
||||
.build();
|
||||
//创建httpClient对象
|
||||
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
|
||||
CloseableHttpResponse response = null;
|
||||
String result = "";
|
||||
try {
|
||||
// 创建http请求
|
||||
HttpPost httpPost = getPost(url, header, jsonParams);
|
||||
response = httpClient.execute(httpPost);
|
||||
result = EntityUtils.toString(response.getEntity(), "utf-8");
|
||||
} catch (Exception e) {
|
||||
log.error("doPost异常", e);
|
||||
} finally {
|
||||
//关闭资源
|
||||
closeResource(response, httpClient);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static HttpPost getPost(String url, Map<String, String> header, String jsonParams) {
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.addHeader("Content-Type", "application/json");
|
||||
// 创建请求内容
|
||||
StringEntity entity = new StringEntity(jsonParams, "utf-8");
|
||||
entity.setContentType("application/json");
|
||||
httpPost.setEntity(entity);
|
||||
// 设置请求头
|
||||
if (null != header && !header.isEmpty()) {
|
||||
Set<Map.Entry<String, String>> entries = header.entrySet();
|
||||
for (Map.Entry<String, String> e : entries) {
|
||||
httpPost.setHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
}
|
||||
return httpPost;
|
||||
}
|
||||
|
||||
public static String doGet(String url, Map<String, String> header, Map<String, String> param) {
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
// 设置连接超时时间
|
||||
.setConnectTimeout(connectTimeout)
|
||||
// 设置Socket超时时间
|
||||
.setSocketTimeout(socketTimeOut)
|
||||
.setConnectionRequestTimeout(connectionRequestTimeout)
|
||||
.build();
|
||||
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
|
||||
CloseableHttpResponse response;
|
||||
String result = "";
|
||||
try {
|
||||
String request = "";
|
||||
if (MapUtils.isNotEmpty(param)) {
|
||||
StringBuilder req = new StringBuilder("?");
|
||||
for (Map.Entry<String, String> entry : param.entrySet()) {
|
||||
req.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
|
||||
}
|
||||
request = req.substring(0, req.length() - 1);
|
||||
}
|
||||
|
||||
HttpGet httpGet = new HttpGet(url + request);
|
||||
httpGet.addHeader("Content-Type", "application/json");
|
||||
if (MapUtils.isNotEmpty(header)) {
|
||||
for (Map.Entry<String, String> entry : header.entrySet()) {
|
||||
httpGet.setHeader(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
log.info("doGet请求:请求地址:{}", url + request);
|
||||
response = httpClient.execute(httpGet);
|
||||
result = EntityUtils.toString(response.getEntity(), "utf-8");
|
||||
} catch (Exception e) {
|
||||
log.error("doGet异常:", e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 关闭资源
|
||||
*/
|
||||
private static void closeResource(Closeable... resources) {
|
||||
try {
|
||||
for (Closeable resource : resources) {
|
||||
if (resource != null) {
|
||||
resource.close();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.xiang.xservice.basic.utils;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2025-06-10 16:50
|
||||
*/
|
||||
public class IpUtils {
|
||||
|
||||
private final static String PUBLIC_IP_URL = "https://api-ipv4.ip.sb/ip";
|
||||
|
||||
public static String getPublicIp() throws IOException {
|
||||
Map<String, String> header = Maps.newHashMap();
|
||||
header.put("User-Agent", "Mozilla/5.0");
|
||||
return HttpUtils.doGet(PUBLIC_IP_URL, header, null).trim();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user