详情介绍
2025/9/7大约 3 分钟
详情介绍
组件工作逻辑
请求处理流程
异常处理流程
文件详细说明
1. GatewayClient
路径: top.codelong.sendsdk.client
作用: 网关客户端核心类,提供HTTP请求API
关键方法:
// GET请求
public String get(String path, Map<String, Object> params) throws GatewayException
// POST请求
public String post(String path, Map<String, Object> params, Object body) throws GatewayException
// PUT请求
public String put(String path, Map<String, Object> params, Object body) throws GatewayException
// DELETE请求
public String delete(String path, Map<String, Object> params) throws GatewayException核心逻辑:
private String execute(HttpMethod method, String path,
Map<String, Object> params, Object body) throws GatewayException {
// 1. 构建完整URL
String url = buildUrl(path, params);
// 2. 创建HTTP请求
HttpUriRequest request = createRequest(method, url, body);
// 3. 设置认证头
setAuthHeader(request);
// 4. 执行请求
HttpResponse response = httpClient.execute(request);
// 5. 处理响应
return handleResponse(response);
}2. GatewaySDKProperties
路径: top.codelong.sendsdk.config
作用: SDK配置属性类,管理网关地址、认证信息等
配置项:
@ConfigurationProperties(prefix = "api.gateway.sdk")
public class GatewaySDKProperties {
private String baseUrl; // 网关基础URL
private String safeKey; // 安全密钥
private String safeSecret; // 安全凭证
private int connectTimeout = 5000; // 连接超时(毫秒)
private int socketTimeout = 10000; // 读取超时(毫秒)
private int maxConnections = 100; // 最大连接数
}3. JwtUtils
路径: top.codelong.sendsdk.utils
作用: JWT令牌生成工具类
关键逻辑:
public String generateToken(String key, String secret) {
// 1. 设置过期时间(30天)
Date expireDate = new Date(System.currentTimeMillis() + TOKEN_EXPIRE_TIME);
// 2. 创建HMAC256算法
Algorithm algorithm = Algorithm.HMAC256(secret);
// 3. 构建JWT Token
return JWT.create()
.withIssuedAt(new Date())
.withExpiresAt(expireDate)
.withClaim("safe-key", key)
.sign(algorithm);
}Token特性:
- 有效期:30天
- 算法:HMAC256
- 包含声明:safe-key(安全密钥)
- 包含签发时间和过期时间
4. HttpClientFactory
路径: top.codelong.sendsdk.client
作用: HTTP客户端工厂类,负责创建和配置HttpClient
关键配置:
public static CloseableHttpClient createHttpClient(GatewaySDKProperties properties) {
// 1. 创建连接管理器
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(properties.getMaxConnections());
cm.setDefaultMaxPerRoute(properties.getMaxConnections() / 2);
// 2. 设置请求配置
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(properties.getConnectTimeout())
.setSocketTimeout(properties.getSocketTimeout())
.build();
// 3. 创建HttpClient
return HttpClients.custom()
.setConnectionManager(cm)
.setDefaultRequestConfig(config)
.evictIdleConnections(60L, TimeUnit.SECONDS) // 60秒清理空闲连接
.build();
}连接池特性:
- 支持连接复用
- 自动清理空闲连接(60秒)
- 可配置的连接超时和读取超时
- 支持高并发请求
5. GatewayAutoConfiguration
路径: top.codelong.sendsdk.config
作用: 自动配置类,创建SDK所需的Bean
关键配置:
@Configuration
@EnableConfigurationProperties(GatewaySDKProperties.class)
public class GatewayAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
@Bean
@ConditionalOnMissingBean
public JwtUtils jwtUtils() {
return new JwtUtils();
}
@Bean
@ConditionalOnMissingBean
public CloseableHttpClient httpClient(GatewaySDKProperties properties) {
// 创建HTTP客户端
}
@Bean
@ConditionalOnMissingBean
public GatewayClient gatewayClient(...) {
// 创建网关客户端
}
}6. 异常体系
GatewayException - 网关异常基类
public class GatewayException extends RuntimeException {
public GatewayException(String message) {
super(message);
}
public GatewayException(String message, Throwable cause) {
super(message, cause);
}
}AuthException - 认证异常
public class AuthException extends GatewayException {
public AuthException(String message) {
super(message);
}
}TimeoutException - 超时异常
public class TimeoutException extends GatewayException {
public TimeoutException(String message) {
super(message);
}
}7. Result
路径: top.codelong.sendsdk.common
作用: 统一响应结果封装类
数据结构:
public class Result<T> implements Serializable {
private Integer code; // 状态码 (1:成功, 0:失败)
private String msg; // 消息
private T data; // 业务数据
}工厂方法:
// 成功响应
Result.success();
Result.success(data);
// 失败响应
Result.error(msg);