conductor服务编排http任务安排

conductor服务编排http任务安排测试

# 编排
1.定义任务:http://localhost:8080/api/metadata/taskdefs
[{
  "name": "user_task",
  "retryCount": 3,
  "timeoutSeconds": 1200,
  "inputKeys": [
    "type"
  ],
  "outputKeys": [
    "id",
    "name"
  ],
  "timeoutPolicy": "TIME_OUT_WF",
  "retryLogic": "FIXED",
  "retryDelaySeconds": 600,
  "responseTimeoutSeconds": 3600
},
{
  "name": "group_task",
  "retryCount": 3,
  "timeoutSeconds": 1200,
  "inputKeys": [
    "id",
    "name"
  ],
  "outputKeys": [
    "response",
    "result"
  ],
  "timeoutPolicy": "TIME_OUT_WF",
  "retryLogic": "FIXED",
  "retryDelaySeconds": 600,
  "responseTimeoutSeconds": 3600
}
]
name    任务类型    唯一
retryCount    任务标记为失败时尝试重试的次数    -
retryLogic    重试机制
timeoutSeconds    以毫秒为单位的时间,在此之后,如果在转换到IN_PROGRESS状态后未完成任务,则将任务标记为TIMED_OUT    如果设置为0,则不会超时
timeoutPolicy    任务的超时策略
responseTimeoutSeconds    如果大于0,则在此时间之后未更新状态时,将重新安排任务。当work轮询任务但由于错误/网络故障而无法完成时很有用。    -
outputKeys    任务输出的键集。用于记录任务的输出
2.定义工作流:http://localhost:8080/api/metadata/workflow
{
  "name": "user_and_group",
  "description": "Encodes a file and deploys to CDN",
  "version": 1,
  "tasks": [
    {
      "name": "user_task",
      "taskReferenceName": "user",
      "inputParameters": {
            "http_request": {
                "method": "POST",
                "uri": "http://localhost:8000/user/getUser",
                "body": {
                    "type": "${workflow.input.type}"
                }
            }
        },
      "type": "HTTP"
    },
    {
      "name": "group_task",
      "taskReferenceName": "group",
      "inputParameters": {
            "http_request": {
                "method": "POST",
                "uri": "http://localhost:8010/group/getGroup",
                "body": {
                    "id": "${user.output.response.body.id}",
                    "name": "${user.output.response.body.name}"
                }
            }
      },
      "type": "HTTP"
    }
  ],
  "outputParameters": {
    "response": "${group.output.response.body.response}",
    "result": "${group.output.response.body.result}"
  },
  "schemaVersion": 2
}
3.任务执行:http://localhost:8080/api/workflow/{workname}
在body中定义参数:
{
"type":"1"
}
4.根据上一步返回的id获取任务信息:http://localhost:8080/api/workflow/{workid}:
输出内容:
{
"output": {
    "response": [
      "1",
      "2",
      "3",
      "4"
    ],
    "result": "李四gaga"
  }
}
 

 

 

#### 安装node.js
    curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -
    sudo yum install -y nodejs
    node -v
    
#### gradle 安装
    wget http://downloads.gradle.org/distributions/gradle-4.8-bin.zip
    unzip gradle-4.8-bin.zip

    配置环境变量 
    打开profile 文件 
    vim /etc/profile 

    export GRADLE_HOME=/root/proess/gradle48 
    export PATH=$GRADLE_HOME/bin:$PATH 

    source /etc/profile 
    gradle -v 


#### 环境启动conductor    
    git clone https://github.com/Netflix/conductor.git
    
    cd conductor/server
    ../gradlew server
    
    cd ui
    npm install gulp --save-dev
    npm install  --save-dev
    gulp watch
    
    
#### 例子一 conductor服务编排http任务安排    

# 文档
http://wiki.ttxit.com/pages/viewpage.action?pageId=25919490
# 编排
1.定义任务:http://localhost:8080/api/metadata/taskdefs
[{
  "name": "user_task",
  "retryCount": 3,
  "timeoutSeconds": 1200,
  "inputKeys": [
    "type"
  ],
  "outputKeys": [
    "id",
    "name"
  ],
  "timeoutPolicy": "TIME_OUT_WF",
  "retryLogic": "FIXED",
  "retryDelaySeconds": 600,
  "responseTimeoutSeconds": 3600
},
{
  "name": "group_task",
  "retryCount": 3,
  "timeoutSeconds": 1200,
  "inputKeys": [
    "id",
    "name"
  ],
  "outputKeys": [
    "response",
    "result"
  ],
  "timeoutPolicy": "TIME_OUT_WF",
  "retryLogic": "FIXED",
  "retryDelaySeconds": 600,
  "responseTimeoutSeconds": 3600
}
]
name    任务类型    唯一
retryCount    任务标记为失败时尝试重试的次数    -
retryLogic    重试机制
timeoutSeconds    以毫秒为单位的时间,在此之后,如果在转换到IN_PROGRESS状态后未完成任务,则将任务标记为TIMED_OUT    如果设置为0,则不会超时
timeoutPolicy    任务的超时策略
responseTimeoutSeconds    如果大于0,则在此时间之后未更新状态时,将重新安排任务。当work轮询任务但由于错误/网络故障而无法完成时很有用。    -
outputKeys    任务输出的键集。用于记录任务的输出

2.定义工作流:http://localhost:8080/api/metadata/workflow
{
  "name": "user_and_group",
  "description": "Encodes a file and deploys to CDN",
  "version": 1,
  "tasks": [
    {
      "name": "user_task",
      "taskReferenceName": "user",
      "inputParameters": {
            "http_request": {
                "method": "POST",
                "uri": "http://localhost:8000/user/getUser",
                "body": {
                    "type": "${workflow.input.type}"
                }
            }
        },
      "type": "HTTP"
    },
    {
      "name": "group_task",
      "taskReferenceName": "group",
      "inputParameters": {
            "http_request": {
                "method": "POST",
                "uri": "http://localhost:8010/group/getGroup",
                "body": {
                    "id": "${user.output.response.body.id}",
                    "name": "${user.output.response.body.name}"
                }
            }
      },
      "type": "HTTP"
    }
  ],
  "outputParameters": {
    "response": "${group.output.response.body.response}",
    "result": "${group.output.response.body.result}"
  },
  "schemaVersion": 2
}

3.任务执行:http://localhost:8080/api/workflow/{workname}
在body中定义参数:
{
"type":"1"
}

4.根据上一步返回的id获取任务信息:http://localhost:8080/api/workflow/{workid}:
输出内容:
{
"output": {
    "response": [
      "1",
      "2",
      "3",
      "4"
    ],
    "result": "李四gaga"
  }
}

#### 例子二 创建任务(Metadata Management)
参考:深入浅出Netflix Conductor使用
https://www.jianshu.com/p/4eae1af8afa8


http://192.168.13.183:8080/index.html#/
定义位置:post /metadata/taskdefs Create new task definition(s)
查看结果:http://192.168.13.183:3000/#/workflow/metadata/tasks?_k=dkzgv2

[
  {
    "name": "leaderRatify",
    "retryCount": 3,
    "timeoutSeconds": 1200,
    "inputKeys": [
      "staffName",
      "staffDepartment"
    ],
    "outputKeys": [
      "leaderAgree",
      "leaderDisagree"
    ],
    "timeoutPolicy": "TIME_OUT_WF",
    "retryLogic": "FIXED",
    "retryDelaySeconds": 600,
    "responseTimeoutSeconds": 800
  },
  {
    "name": "managerRatify",
    "retryCount": 3,
    "timeoutSeconds": 1200,
    "inputKeys": [
      "managerName",
      "managerDeparment"
    ],
    "outputKeys": [
      "managerAgree",
      "managerDisagree"
    ],
    "timeoutPolicy": "TIME_OUT_WF",
    "retryLogic": "FIXED",
    "retryDelaySeconds": 600,
    "responseTimeoutSeconds": 800
  }
]

    
#### 创建工作流
http://192.168.13.183:8080/index.html#/
定义位置:post /metadata/taskdefs Create new task definition(s)
查看结果:http://192.168.13.183:3000/#/workflow/metadata/Leave%20process/1?_k=te80nu

[{
    "updateTime": 1540448903202,
    "name": "Leave process",
    "description": "a demo for workflow",
    "version": 1,
    "tasks": [{
            "name": "leaderRatify",
            "taskReferenceName": "node1",
            "inputParameters": {
                "staffName": "${workflow.input.staffName}",
                "staffDepartment": "${workflow.input.staffDepartment}"
            },
            "type": "SIMPLE",
            "startDelay": 0
        },
        {
            "name": "managerRatify",
            "taskReferenceName": "node2",
            "inputParameters": {
                "managerName": "${node1.output.leaderName}",
                "managerDepartment": "${node1.output.leaderDepartment}"
            },
            "type": "SIMPLE",
            "startDelay": 0
        }
    ],
    "outputParameters": {
        "leaderName": "${node1.output.leaderName}",
        "leaderDepartment": "${node1.output.leaderDepartment}",
        "managerAgree": "${node2.output.managerAgree}",
        "managerDisagree": "${node2.output.managerDisagree}"
    },
    "restartable": true,
    "schemaVersion": 2
}]

#### 创建任务worker
class LeaderRatifyWorker implements Worker {
    private String taskDefName;
    public SampleWorker(String taskDefName) {
        this.taskDefName = taskDefName;
    }
    @Override
    public String getTaskDefName() {
        return taskDefName;
    }
    @Override
    public TaskResult execute(Task task) {
        System.out.printf("Executing %s%n", taskDefName);
        System.out.println("staffName:" + task.getInputData().get("staffName"));
        System.out.println("staffDepartment:" + task.getInputData().get("staffDepartment"));
        TaskResult result = new TaskResult(task);
        result.setStatus(TaskResult.Status.COMPLETED);
        //Register the output of the task
        result.getOutputData().put("outputKey1", "value");
        result.getOutputData().put("oddEven", 1);
        result.getOutputData().put("mod", 4);
        result.getOutputData().put("leaderAgree", "yes");
        result.getOutputData().put("leaderDisagree", "no");
        return result;
    }
}
class ManagerRatifyWorker implements Worker {
    private String taskDefName;
    public SampleWorker2(String taskDefName) {
        this.taskDefName = taskDefName;
    }
    @Override
    public String getTaskDefName() {
        return taskDefName;
    }
    @Override
    public TaskResult execute(Task task) {
        System.out.printf("Executing %s\n", taskDefName);
        System.out.println("managerName:" + task.getInputData().get("managerName"));
        System.out.println("managerDepartment:" + task.getInputData().get("managerDepartment"));
        TaskResult result = new TaskResult(task);
        result.setStatus(TaskResult.Status.COMPLETED);
        //Register the output of the task
        result.getOutputData().put("managerAgree", String.valueOf(task.getInputData().get("managerName")));
        result.getOutputData().put("managerDisagree", String.valueOf(task.getInputData().get("managerDepartment")));
 
        return result;
    }
}
  
//在main方法中创建工作Worker以及设置需要访问的Conductor Server端api地址,并将流程进入初始化
 public static void main(String[] args) {
        TaskClient taskClient = new TaskClient();
        taskClient.setRootURI("http://localhost:8080/api/");       //Point this to the server API
        int threadCount = 2;         //number of threads used to execute workers.  To avoid starvation, should be same or more than number of workers
        Worker worker1 = new LeaderRatifyWorker("leaderRatify");
        Worker worker2 = new ManagerRatifyWorker("managerRatify");
        //Create WorkflowTaskCoordinator
        WorkflowTaskCoordinator.Builder builder = new WorkflowTaskCoordinator.Builder();
        WorkflowTaskCoordinator coordinator = builder.withWorkers(worker1, worker2).withThreadCount(threadCount).withTaskClient(taskClient).build();
        //Start for polling and execution of the tasks
        coordinator.init();
}


 

你可能感兴趣的:(09-01-源码框架)