微服务调用(http)

微服务调用(http)

微服务可以是http的RESTful API也可以是二进制流服务等。

restful api调用演示

procedure TForm2.Button2Click(Sender: TObject);
//rest 查询
begin
  var req: TIdMultiPartFormDataStream := TIdMultiPartFormDataStream.Create;
  req.AddFormField('cmd', IntToStr(cmd_query));
  req.AddFormField('accountno', '1');
  req.AddFormField('sql', 'select top 2 * from tunit');
  Memo1.Text := IdHTTP1.Post(FUrl2, req);  //'http://localhost:1580/rest?service=dal';
  req.Free;
  ClientDataSet1.FromJson(Memo1.Text);
end;

 二进制流调用演示

procedure TForm2.Button1Click(Sender: TObject);
//msgpack 查询
begin
  var pack: TMsgPack := TMsgPack.Create;
  pack.Force('cmd').AsInteger := cmd_query;                //命令字
  pack.Force('accountno').AsString := '1';                 //数据库帐套号
  pack.Force('tablenum').AsInteger := 2;                   //查几个表
  pack.Force('sql1').AsString := 'select * from tgoods';
  pack.Force('sql2').AsString := 'select * from tunit';
  var req: TBytesStream := TBytesStream.Create;
  var res: TBytesStream := TBytesStream.Create;
  pack.EncodeToStream(req);
  req.Position := 0;
  IdHTTP1.Post(FUrl, req, res); //'http://localhost:1580/msgpack?service=dal'
  pack.Clear;
  res.Position := 0;
  pack.DecodeFromStream(res);
  if pack.Force('return').AsBoolean then
  begin
    ClientDataSet1.Data := pack.Force('dataset1').AsVariant;
    ClientDataSet2.Data := pack.Force('dataset2').AsVariant;
  end
  else
    ShowMessage(pack.Force('err').AsString);
  pack.Free;
  req.Free;
  res.Free;
end; 
二进制流调用url
http://localhost:1580/msgpack?service=dal
rest json调用url
http://localhost:1580/rest?service=dal
service=dal,表示调用dal微服务

 

posted @ 2021-02-23 17:09  delphi中间件  阅读(724)  评论(0编辑  收藏  举报