如何使用
2025/9/7大约 2 分钟
如何使用
1. 引入依赖
在项目中添加组件依赖
<dependency>
<groupId>top.codelong</groupId>
<artifactId>server-send-sdk</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>2. 配置属性
在 application.yml 中添加配置:
api:
gateway:
sdk:
baseUrl: http://127.0.0.1:8080 # 网关中心地址
safeKey: your-safe-key # 安全密钥
safeSecret: your-safe-secret # 安全凭证
connectTimeout: 5000 # 连接超时时间(毫秒)
socketTimeout: 10000 # 读取超时时间(毫秒)
maxConnections: 100 # 最大连接数3. 注入GatewayClient
在需要调用网关的服务中注入 GatewayClient:
@Service
public class MyService {
@Autowired
private GatewayClient gatewayClient;
public void callGateway() {
// 使用GatewayClient进行请求
}
}4. 发送GET请求
// 不带参数的GET请求
String result = gatewayClient.get("/api/users", null);
// 带参数的GET请求
Map<String, Object> params = new HashMap<>();
params.put("id", 1);
params.put("name", "test");
String result = gatewayClient.get("/api/users", params);5. 发送POST请求
// POST请求
Map<String, Object> params = new HashMap<>();
params.put("page", 1);
Map<String, Object> body = new HashMap<>();
body.put("username", "admin");
body.put("password", "123456");
String result = gatewayClient.post("/api/login", params, body);6. 发送PUT请求
// PUT请求
Map<String, Object> body = new HashMap<>();
body.put("id", 1);
body.put("name", "updated-name");
String result = gatewayClient.put("/api/users/1", null, body);7. 发送DELETE请求
// DELETE请求
Map<String, Object> params = new HashMap<>();
params.put("id", 1);
String result = gatewayClient.delete("/api/users", params);8. 异常处理
try {
String result = gatewayClient.get("/api/users", null);
// 处理结果
} catch (GatewayException e) {
// 处理网关异常
log.error("网关请求失败: {}", e.getMessage());
} catch (AuthException e) {
// 处理认证异常
log.error("认证失败: {}", e.getMessage());
} catch (TimeoutException e) {
// 处理超时异常
log.error("请求超时: {}", e.getMessage());
}9. 完整示例
@Service
public class UserService {
@Autowired
private GatewayClient gatewayClient;
public void getUserList() {
try {
Map<String, Object> params = new HashMap<>();
params.put("page", 1);
params.put("pageSize", 10);
String result = gatewayClient.get("/api/users", params);
log.info("获取用户列表成功: {}", result);
} catch (GatewayException e) {
log.error("获取用户列表失败: {}", e.getMessage());
}
}
public void createUser(String username, String email) {
try {
Map<String, Object> body = new HashMap<>();
body.put("username", username);
body.put("email", email);
String result = gatewayClient.post("/api/users", null, body);
log.info("创建用户成功: {}", result);
} catch (GatewayException e) {
log.error("创建用户失败: {}", e.getMessage());
}
}
public void updateUser(Long userId, String username) {
try {
Map<String, Object> body = new HashMap<>();
body.put("username", username);
String result = gatewayClient.put("/api/users/" + userId, null, body);
log.info("更新用户成功: {}", result);
} catch (GatewayException e) {
log.error("更新用户失败: {}", e.getMessage());
}
}
public void deleteUser(Long userId) {
try {
Map<String, Object> params = new HashMap<>();
params.put("id", userId);
String result = gatewayClient.delete("/api/users", params);
log.info("删除用户成功: {}", result);
} catch (GatewayException e) {
log.error("删除用户失败: {}", e.getMessage());
}
}
}