feat: abstract ServerElectionService interface to support developers to customize #191

This commit is contained in:
tjq 2021-02-09 22:40:50 +08:00
parent a575b65320
commit a1a5ade215
3 changed files with 26 additions and 13 deletions

View File

@ -0,0 +1,12 @@
package com.github.kfcfans.powerjob.server.extension;
/**
* 调度服务器选举服务默认实现为先到先得可自行接入 Zookeeper 等实现"负载均衡"策略
*
* @author tjq
* @since 2021/2/9
*/
public interface ServerElectionService {
String elect(Long appId, String protocol, String currentServer);
}

View File

@ -5,12 +5,13 @@ import akka.pattern.Patterns;
import com.github.kfcfans.powerjob.common.PowerJobException;
import com.github.kfcfans.powerjob.common.Protocol;
import com.github.kfcfans.powerjob.common.response.AskResponse;
import com.github.kfcfans.powerjob.server.transport.TransportService;
import com.github.kfcfans.powerjob.server.transport.starter.AkkaStarter;
import com.github.kfcfans.powerjob.server.extension.LockService;
import com.github.kfcfans.powerjob.server.extension.ServerElectionService;
import com.github.kfcfans.powerjob.server.handler.inner.requests.Ping;
import com.github.kfcfans.powerjob.server.persistence.core.model.AppInfoDO;
import com.github.kfcfans.powerjob.server.persistence.core.repository.AppInfoRepository;
import com.github.kfcfans.powerjob.server.extension.LockService;
import com.github.kfcfans.powerjob.server.transport.TransportService;
import com.github.kfcfans.powerjob.server.transport.starter.AkkaStarter;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@ -27,14 +28,14 @@ import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
/**
* Worker请求分配Server服务
* Default server election policy, first-come, first-served, no load balancing capability
*
* @author tjq
* @since 2020/4/5
* @since 2021/2/9
*/
@Slf4j
@Service
public class ServerSelectService {
public class DefaultServerElectionService implements ServerElectionService {
@Resource
private LockService lockService;
@ -50,8 +51,8 @@ public class ServerSelectService {
private static final long PING_TIMEOUT_MS = 1000;
private static final String SERVER_ELECT_LOCK = "server_elect_%d";
public String getServer(Long appId, String currentServer, String protocol) {
@Override
public String elect(Long appId, String protocol, String currentServer) {
if (!accurate()) {
// 如果是本机就不需要查数据库那么复杂的操作了直接返回成功
if (getThisServerAddress(protocol).equals(currentServer)) {

View File

@ -5,11 +5,11 @@ import com.alibaba.fastjson.JSONObject;
import com.github.kfcfans.powerjob.common.response.ResultDTO;
import com.github.kfcfans.powerjob.common.utils.CommonUtils;
import com.github.kfcfans.powerjob.common.utils.NetUtils;
import com.github.kfcfans.powerjob.server.transport.starter.AkkaStarter;
import com.github.kfcfans.powerjob.server.extension.ServerElectionService;
import com.github.kfcfans.powerjob.server.persistence.core.model.AppInfoDO;
import com.github.kfcfans.powerjob.server.persistence.core.repository.AppInfoRepository;
import com.github.kfcfans.powerjob.server.service.ha.ServerSelectService;
import com.github.kfcfans.powerjob.server.service.ha.WorkerManagerService;
import com.github.kfcfans.powerjob.server.transport.starter.AkkaStarter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -31,7 +31,7 @@ import java.util.TimeZone;
public class ServerController {
@Resource
private ServerSelectService serverSelectService;
private ServerElectionService serverElectionService;
@Resource
private AppInfoRepository appInfoRepository;
@ -43,8 +43,8 @@ public class ServerController {
}
@GetMapping("/acquire")
public ResultDTO<String> acquireServer(Long appId, String currentServer, String protocol) {
return ResultDTO.success(serverSelectService.getServer(appId, currentServer, protocol));
public ResultDTO<String> acquireServer(Long appId, String protocol, String currentServer) {
return ResultDTO.success(serverElectionService.elect(appId, protocol, currentServer));
}
@GetMapping("/hello")