2021SC@SDUSC
10-04

模块-dolphinsched

API接口层,主要负责处理前端UI层的请求。
该服务统一提供RESTful api向外部提供请求服务。 
接口包括:工作流的创建、定义、查询、修改、发布、下线、
手工启动、停止、暂停、恢复、从该节点开始执行等等。

启动API

org.apache.dolphinscheduler.api下面有两个类:
ApiApplicationServer、CombinedApplicationServer。

从ApiApplicationServer来看就是启动一个SpringBoot应用。

CombinedApplicationServer除了启动一个SprintBoot应用之外,
还启动了LoggerServer、AlertServer。
@SpringBootApplication
@ConditionalOnProperty(prefix = "server", name = "is-combined-server", havingValue = "true")
@ServletComponentScan
@ComponentScan("org.apache.dolphinscheduler")
@Import({MasterServer.class, WorkerServer.class})
@EnableSwagger2
public class CombinedApplicationServer extends SpringBootServletInitializer {

    public static void main(String[] args) throws Exception {

        ApiApplicationServer.main(args);

        LoggerServer server = new LoggerServer();
        server.start();

        AlertServer alertServer = AlertServer.getInstance();
        alertServer.start();
    }
}

CombinedApplicationServer与ApiApplicationServer的区别:是否内嵌LoggerServer、AlertServer。
而且当server.is-combined-server为true时,会自动启动CombinedApplicationServer。

对于SpringBoot应用,接口一般都在controller中。
org.apache.dolphinscheduler.api.controller包有多个Controller: 
AccessTokenController ProcessInstanceController 
AlertGroupController DataAnalysisController
ProjectController BaseController QueueController 
ResourcesController DataSourceController 
SchedulerController ExecutorController 
TaskInstanceController LoggerController 
TaskRecordController LoginController 
TenantController MonitorController UsersController 
ProcessDefinitionController WorkerGroupController

因为在DolphinScheduler调度中最重要的一个概念就是流程定义,
所以我们从ProcessDefinitionController入手简要分析这个模块的基本功能。

ProcessDefinitionController

batchDeletateProcessDefinitionByIds
DeletateProcessDefinitionByIds
exportProcessDefinitionByIds
getNodeListByDefinitionlistId
getNodeListByDefinitionlistIdList
queryProcessDefinitionList
queryProcessDefinitionListPaging
queryProcessDefinitionAllByProjectId
releaseProcessDefinition
save
queryProcessDefinitionById
updateProcessDefinition
verify-name
viewTree

可以看到org.apache.dolphinscheduler.api.controller.ProcessDefinitionController大概有14个接口。

ProcessDefinitionController中只有一个字段ProcessDefinitionService,从名称以及自身经验来看,可以知道ProcessDefinitionController会负责HTTP请求的参数解析、参数校验、返回值等等,与业务无关的逻辑;具体的业务逻辑会交给ProcessDefinitionService类处理。

由此我们可以类比分析其他所有的controller,都会有一个对应的service处理业务相关的逻辑。

public Result createProcessDefinition(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                  @ApiParam(name = "projectName", value = "PROJECT_NAME", required = true) @PathVariable String projectName,
                                  @RequestParam(value = "name", required = true) String name,
                                  @RequestParam(value = "processDefinitionJson", required = true) String json,
                                  @RequestParam(value = "locations", required = true) String locations,
                                  @RequestParam(value = "connects", required = true) String connects,
                                  @RequestParam(value = "description", required = false) String description) {

    try {
        logger.info("login user {}, create  process definition, project name: {}, process definition name: {}, " +
                        "process_definition_json: {}, desc: {} locations:{}, connects:{}",
                loginUser.getUserName(), projectName, name, json, description, locations, connects);
        Map<String, Object> result = processDefinitionService.createProcessDefinition(loginUser, projectName, name, json,
                description, locations, connects);
        return returnDataList(result);
    } catch (Exception e) {
        logger.error(Status.CREATE_PROCESS_DEFINITION.getMsg(), e);
        return error(Status.CREATE_PROCESS_DEFINITION.getCode(), Status.CREATE_PROCESS_DEFINITION.getMsg());
    }
}

上面是createProcessDefinition的源码,逻辑比较清晰,就是接收、校验HTTP的参数,然后调用processDefinitionService.createProcessDefinition函数,返回结果、处理异常。

但这段代码有一个controller与service分隔不清的地方:
HTTP返回的结果由谁处理。
此处返回结果是由service负责的,
service会创建一个Map<String, Object>类型的result字段,
再调用result.put("processDefinitionId",processDefine.getId());
设置最终返回的数据。
严格来说,service只返回与业务相关的实体,
HTTP具体返回什么信息应该交由controller处理。
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐