mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: PowerJob Client support ClientExtension(current for dynamic server ip) #895
This commit is contained in:
parent
a1dad6c39e
commit
3e0088870a
@ -5,6 +5,7 @@ import lombok.Setter;
|
|||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
import lombok.experimental.Accessors;
|
import lombok.experimental.Accessors;
|
||||||
import tech.powerjob.client.common.Protocol;
|
import tech.powerjob.client.common.Protocol;
|
||||||
|
import tech.powerjob.client.extension.ClientExtension;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -62,4 +63,9 @@ public class ClientConfig implements Serializable {
|
|||||||
* 用于流量被基础设施识别
|
* 用于流量被基础设施识别
|
||||||
*/
|
*/
|
||||||
private Map<String, String> defaultHeaders;
|
private Map<String, String> defaultHeaders;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 客户端行为扩展
|
||||||
|
*/
|
||||||
|
private ClientExtension clientExtension;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
package tech.powerjob.client.extension;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扩展服务
|
||||||
|
*
|
||||||
|
* @author tjq
|
||||||
|
* @since 2024/8/11
|
||||||
|
*/
|
||||||
|
public interface ClientExtension {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态提供地址,适用于 server 部署在动态集群上的场景
|
||||||
|
* @param context 上下文
|
||||||
|
* @return 地址,格式要求同 ClientConfig#addressList
|
||||||
|
*/
|
||||||
|
List<String> addressProvider(ExtensionContext context);
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package tech.powerjob.client.extension;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扩展上下文
|
||||||
|
*
|
||||||
|
* @author tjq
|
||||||
|
* @since 2024/8/11
|
||||||
|
*/
|
||||||
|
public class ExtensionContext {
|
||||||
|
}
|
@ -2,11 +2,14 @@ package tech.powerjob.client.service.impl;
|
|||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import tech.powerjob.client.ClientConfig;
|
import tech.powerjob.client.ClientConfig;
|
||||||
|
import tech.powerjob.client.extension.ClientExtension;
|
||||||
|
import tech.powerjob.client.extension.ExtensionContext;
|
||||||
import tech.powerjob.client.service.HttpResponse;
|
import tech.powerjob.client.service.HttpResponse;
|
||||||
import tech.powerjob.client.service.PowerRequestBody;
|
import tech.powerjob.client.service.PowerRequestBody;
|
||||||
import tech.powerjob.client.service.RequestService;
|
import tech.powerjob.client.service.RequestService;
|
||||||
import tech.powerjob.common.OpenAPIConstant;
|
import tech.powerjob.common.OpenAPIConstant;
|
||||||
import tech.powerjob.common.exception.PowerJobException;
|
import tech.powerjob.common.exception.PowerJobException;
|
||||||
|
import tech.powerjob.common.utils.CollectionUtils;
|
||||||
|
|
||||||
import javax.net.ssl.X509TrustManager;
|
import javax.net.ssl.X509TrustManager;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -62,12 +65,10 @@ abstract class ClusterRequestService implements RequestService {
|
|||||||
* 封装集群请求能力
|
* 封装集群请求能力
|
||||||
* @param path 请求 PATH
|
* @param path 请求 PATH
|
||||||
* @param powerRequestBody 请求体
|
* @param powerRequestBody 请求体
|
||||||
* @param headers 请求头
|
|
||||||
* @return 响应
|
* @return 响应
|
||||||
*/
|
*/
|
||||||
protected HttpResponse clusterHaRequest(String path, PowerRequestBody powerRequestBody) {
|
protected HttpResponse clusterHaRequest(String path, PowerRequestBody powerRequestBody) {
|
||||||
|
|
||||||
List<String> addressList = config.getAddressList();
|
|
||||||
// 先尝试默认地址
|
// 先尝试默认地址
|
||||||
String url = getUrl(path, currentAddress);
|
String url = getUrl(path, currentAddress);
|
||||||
try {
|
try {
|
||||||
@ -76,6 +77,8 @@ abstract class ClusterRequestService implements RequestService {
|
|||||||
log.warn("[ClusterRequestService] request url:{} failed, reason is {}.", url, e.toString());
|
log.warn("[ClusterRequestService] request url:{} failed, reason is {}.", url, e.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<String> addressList = fetchAddressList();
|
||||||
|
|
||||||
// 失败,开始重试
|
// 失败,开始重试
|
||||||
for (String addr : addressList) {
|
for (String addr : addressList) {
|
||||||
if (Objects.equals(addr, currentAddress)) {
|
if (Objects.equals(addr, currentAddress)) {
|
||||||
@ -96,6 +99,19 @@ abstract class ClusterRequestService implements RequestService {
|
|||||||
throw new PowerJobException("no server available when send post request");
|
throw new PowerJobException("no server available when send post request");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<String> fetchAddressList() {
|
||||||
|
|
||||||
|
ClientExtension clientExtension = config.getClientExtension();
|
||||||
|
if (clientExtension != null) {
|
||||||
|
List<String> addressList = clientExtension.addressProvider(new ExtensionContext());
|
||||||
|
if (!CollectionUtils.isEmpty(addressList)) {
|
||||||
|
return addressList;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return config.getAddressList();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 不验证证书
|
* 不验证证书
|
||||||
* X.509 是一个国际标准,定义了公钥证书的格式。这个标准是由国际电信联盟(ITU-T)制定的,用于公钥基础设施(PKI)中数字证书的创建和分发。X.509证书主要用于在公开网络上验证实体的身份,如服务器或客户端的身份验证过程中,确保通信双方是可信的。X.509证书广泛应用于多种安全协议中,包括SSL/TLS,它是实现HTTPS的基础。
|
* X.509 是一个国际标准,定义了公钥证书的格式。这个标准是由国际电信联盟(ITU-T)制定的,用于公钥基础设施(PKI)中数字证书的创建和分发。X.509证书主要用于在公开网络上验证实体的身份,如服务器或客户端的身份验证过程中,确保通信双方是可信的。X.509证书广泛应用于多种安全协议中,包括SSL/TLS,它是实现HTTPS的基础。
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package tech.powerjob.client.test;
|
package tech.powerjob.client.test;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import tech.powerjob.client.PowerJobClient;
|
import tech.powerjob.client.PowerJobClient;
|
||||||
@ -19,6 +20,7 @@ import tech.powerjob.common.response.ResultDTO;
|
|||||||
* @author Echo009
|
* @author Echo009
|
||||||
* @since 2020/4/15
|
* @since 2020/4/15
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
class TestClient extends ClientInitializer {
|
class TestClient extends ClientInitializer {
|
||||||
|
|
||||||
public static final long JOB_ID = 1L;
|
public static final long JOB_ID = 1L;
|
||||||
@ -42,8 +44,10 @@ class TestClient extends ClientInitializer {
|
|||||||
newJobInfo.setMinMemorySpace(1.2);
|
newJobInfo.setMinMemorySpace(1.2);
|
||||||
newJobInfo.setMinDiskSpace(1.3);
|
newJobInfo.setMinDiskSpace(1.3);
|
||||||
|
|
||||||
|
log.info("[TestClient] [testSaveJob] SaveJobInfoRequest: {}", JSONObject.toJSONString(newJobInfo));
|
||||||
|
|
||||||
ResultDTO<Long> resultDTO = powerJobClient.saveJob(newJobInfo);
|
ResultDTO<Long> resultDTO = powerJobClient.saveJob(newJobInfo);
|
||||||
System.out.println(JSONObject.toJSONString(resultDTO));
|
log.info("[TestClient] [testSaveJob] result: {}", JSONObject.toJSONString(resultDTO));
|
||||||
Assertions.assertNotNull(resultDTO);
|
Assertions.assertNotNull(resultDTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user