說(shuō)明
這里介紹四種平時(shí)常用的http請(qǐng)求方法:GET、POST、PUT、DELETE。
在官方的介紹文檔中關(guān)于InvokeHTTP處理器的描述是這么說(shuō)的:
An HTTP client processor which can interact with a configurable HTTP Endpoint. The destination URL and HTTP Method are configurable. FlowFile attributes are converted to HTTP headers and the FlowFile contents are included as the body of the request (if the HTTP Method is PUT, POST or PATCH).
大致意思是說(shuō):這是一個(gè)可以與 HTTP 端點(diǎn)交互的 HTTP 客戶端處理器。處理器中的 URL 和 HTTP Method是可以配置的。處理器中的屬性會(huì)轉(zhuǎn)換為 HTTP的請(qǐng)求頭,如果請(qǐng)求方法是 PUT、POST 或 PATCH那么處理器的內(nèi)容將作為請(qǐng)求的正文(請(qǐng)求體)包含在內(nèi)。
測(cè)試接口
這里給了幾個(gè)處理器測(cè)試用到的接口,主別對(duì)應(yīng)GET、POST、PUT、DELETE請(qǐng)求,測(cè)試的代碼不涉及業(yè)務(wù)邏輯,只是簡(jiǎn)單用來(lái)配合測(cè)試處理器使用。
package com.example.common.resp;
import com.example.common.enums.RespEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Result {
private Integer code;
private String message;
private Object data;
// 省略部分代碼。。。
public static Result ok(Object data) {
return ok(RespEnum.SUCCESS.getCode(), RespEnum.SUCCESS.getMessage(), data);
}
}
=====================================================================================
測(cè)試接口
=====================================================================================
package com.example.redis.controller;
import com.alibaba.fastjson2.JSONObject;
import com.example.common.resp.Result;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/{id}")
public Result getById(@PathVariable String id, @RequestParam Map<String, Object> query) {
JSONObject res = new JSONObject();
res.put("method", "GET");
res.put("desc", "這個(gè)是get方法");
res.put("id", id);
res.put("query", query);
return Result.ok(res);
}
@PostMapping("")
public Result add(@RequestBody JSONObject body) {
JSONObject res = new JSONObject();
res.put("method", "POST");
res.put("desc", "這個(gè)是post方法");
res.put("body", body);
return Result.ok(res);
}
@PutMapping("/{id}")
public Result editById(@PathVariable String id, @RequestBody JSONObject body) {
JSONObject res = new JSONObject();
res.put("method", "PUT");
res.put("desc", "這個(gè)是put方法");
res.put("id", id);
res.put("body", body);
return Result.ok(res);
}
@DeleteMapping("/{id}")
public Result deleteById(@PathVariable String id) {
JSONObject res = new JSONObject();
res.put("method", "DELETE");
res.put("desc", "這個(gè)是delete方法");
res.put("id", id);
return Result.ok(res);
}
}
發(fā)送GET請(qǐng)求
添加處理器:InvokeHTTP
點(diǎn)擊工具欄左上角的Processor拖拽到畫布中,篩選出InvokeHTTP處理器添加到畫布中
配置處理器
雙擊添加的處理器,切換到SETTINGS,給處理器起個(gè)名字
起好名字后切換到PROPERTIES,配置以下內(nèi)容,最后點(diǎn)擊APPLY
添加自定義請(qǐng)求頭
如果需要給請(qǐng)求頭添加自定義屬性的話,直接點(diǎn)擊右上角的加號(hào)+添加即可,例如我這里添加一個(gè)myToken的請(qǐng)求頭,這個(gè)時(shí)候發(fā)送的請(qǐng)求頭里面便會(huì)攜帶該參數(shù)
測(cè)試處理器
方便測(cè)試,將InvokeHTTP處理器設(shè)置為10秒執(zhí)行一次
同理添加LogAttribute處理器到畫布中
修改LogAttribute處理器的配置,將PROPERTIES選項(xiàng)卡中Log Payload的屬性值改為true
連接兩個(gè)處理器,關(guān)聯(lián)關(guān)系選擇Response,最后點(diǎn)擊ADD
將第一個(gè)InvokeHTTP處理器自身的RELATIONSHIPS除了Response之外全部選擇terminate
將第二個(gè)LogAttribute處理器自身的的RELATIONSHIPS選擇terminate
處理器全部顯示如圖所示則表示沒(méi)問(wèn)題,可以運(yùn)行了
啟動(dòng)兩個(gè)處理器,在畫布空白位置鼠標(biāo)右鍵,選擇Start
在nifi的日志中可以看到每隔十秒鐘便會(huì)打印一次接口返回的結(jié)果
發(fā)送POST請(qǐng)求
發(fā)送POST請(qǐng)求攜帶請(qǐng)求體數(shù)據(jù)我們需要借助另外一個(gè)處理器:GenerateFlowFile
添加處理器:GenerateFlowFile
配置處理器
雙擊添加的處理器,在SETTINGS選項(xiàng)卡中給處理器起個(gè)名稱
切換到PROPERTIES選項(xiàng)卡,然后分別配置Custom Text和Mime Type兩個(gè)屬性的值,其他屬性的值默認(rèn)即可。
Custom Text:(官方解釋)If Data Format is text and if Unique FlowFiles is false, then this custom text will be used as content of the generated FlowFiles and the File Size will be ignored。通俗的說(shuō)就是如果Custom Text中的值是文本類型的并且Unique FlowFiles屬性的值是false,則Custom Text中的值會(huì)當(dāng)做流文件的內(nèi)容,這樣Custom Text中的值傳到下一個(gè)處理器InvokeHTTP的時(shí)候便會(huì)作為請(qǐng)求體數(shù)據(jù)傳到接口中。
添加處理器:InvokeHTTP
和發(fā)送GET請(qǐng)求一樣,添加InvokeHTTP處理器,然后配置以下內(nèi)容,post請(qǐng)求中的請(qǐng)求體來(lái)自于上一個(gè)處理器的內(nèi)容
測(cè)試處理器
將第一個(gè)處理器GenerateFlowFile和第二個(gè)處理器InvokeHTTP連接,關(guān)聯(lián)關(guān)系選擇success
繼續(xù)將第二個(gè)處理器InvokeHTTP和之前添加過(guò)的LogAttribute處理器進(jìn)行連接,關(guān)聯(lián)關(guān)系選擇Response
同樣的將InvokeHTTP處理器自身的RELATIONSHIPS除了Response之外全部選擇terminate
同樣的方便測(cè)試,將第一個(gè)處理器GenerateFlowFile設(shè)置為10秒執(zhí)行一次,然后將POST方法對(duì)應(yīng)的三個(gè)處理器分別啟動(dòng)(鼠標(biāo)放到對(duì)應(yīng)的處理器上然后右鍵選擇Start啟動(dòng))
查看nifi的日志,可以看到每隔十秒鐘便會(huì)打印一次接口返回的結(jié)果
發(fā)送PUT請(qǐng)求
put請(qǐng)求和post請(qǐng)求是類似,處理器按照post請(qǐng)求的重新添加一遍即可,只需要將InvokeHTTP處理器里面的請(qǐng)求方法改為PUT,URL改為對(duì)應(yīng)的請(qǐng)求路徑即可。這里不再重復(fù)添加,下面是添加好之后的
測(cè)試結(jié)果
發(fā)送DELETE請(qǐng)求
put請(qǐng)求和get請(qǐng)求是類似,處理器按照get請(qǐng)求的重新添加一遍即可,只需要將InvokeHTTP處理器里面的請(qǐng)求方法改為DELETE,URL改為對(duì)應(yīng)的請(qǐng)求路徑即可。這里不再重復(fù)添加,下面是添加好之后的
測(cè)試結(jié)果
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-701221.html
?結(jié)束語(yǔ)
以上便是NIFI中關(guān)于InvokeHTTP的使用,如果有問(wèn)題歡迎大家評(píng)論。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-701221.html
到了這里,關(guān)于NIFI使用InvokeHTTP發(fā)送http請(qǐng)求的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!