文档

边缘应用服务总线对接

更新时间:
一键部署

1. 整体介绍

服务依赖方:(即使用该服务的应用)能够清晰而简洁的表达他所依赖的接口有哪些,分别期望这些接口完成什么样的具体功能,并且任何为其提供服务的应用,只要遵循相同的服务模型,即可实现服务提供方的替换。服务提供方:通过服务标准化,能够清晰而简洁的表达本服务提供了哪些接口、定义、以及他们所应实现的具体功能,并且任何对该服务所提供的能力有依赖的应用,都必须按照这套接口来实现服务供应;

2.服务依赖方

1.接口描述

API版本

服务模型版本号(一般为1.0)

授权类型

APPSIGN

协议

HTTPS

请求方法

Post

域名(环境变量中获取)

System.getenv(“iot.hosting.mesh.domain”)

路径

/模型名称/接口名称/MeterService/ChargeMete

2.入参说明

入参名称

数据类型

是否必须

入参示例

入参描述

ApiVer

字符串

1.0

服务模型API版本

Version

字符串

1.0

服务模型接口版本

body

复杂对象

request.putParam(key, value);

接口请求字段

3.出参列表

出参名称

数据类型

出参描述

code

整形

响应码, 200: 成功

message

字符串

错误消息

localizedMsg

字符串

本地语言错误消息

data

长整型

响应结果返回的长整型数据是增加的数据的id

4.请求示例

    /**
     * 系统环境变量中获取的
     */
    public static final String appKey = System.getenv("iot.hosting.appKey");
    public static final String AppSecret = System.getenv("iot.hosting.appSecret");
    //服务模型请求的路由
    private static final String SERVICE_EDGE__PATH = System.getenv("iot.hosting.mesh.domain");
public static void main(String[] args) throws UnsupportedEncodingException {
    IoTApiClientBuilderParams ioTApiClientBuilderParams =
      new IoTApiClientBuilderParams();
    ioTApiClientBuilderParams.setAppKey("你的<AppKey>");
    ioTApiClientBuilderParams.setAppSecret("你的<AppSecret>");
    SyncApiClient syncClient = new SyncApiClient(ioTApiClientBuilderParams);

    IoTApiRequest request = new IoTApiRequest();
    //设置api的版本
        request.setApiVer("1.0");
        request.putParam("requestId", "213123");
        request.putParam("meterNo", "213123");
        request.putParam("amount", "213123");
        request.putParam("sourceAppId", "213123");
        request.putParam("targetAppId", "213123");

        //请求参数版本,域名、path、request
        request.setVersion("1.0");

    //请求参数域名、path、request
    ApiResponse response = syncClient.postBody(SERVICE_EDGE__PATH,
      "/服务名称/接口名称", request, true);
    System.out.println( "response code = " + response.getCode()
      + " response = " + new String(response.getBody(), "UTF-8"));
}

5.返回结果示例 JSON

{   
    "id": "3826f64d-6c7b-4d21-8214-8b23771b763a"
    "code": 200,
    "localizedMsg": null,
    "message": null,
    "data": 791
}

6.失败返回结果示例 JSON

{
    "id": "f561d973-9094-479f-81fd-95ec3e7271f5",
    "code": 52009,
    "localizedMsg": "传入的参数和模型字段不匹配:name1",
    "message": "传入的参数和模型字段不匹配:name1",
    "data": null
}

3.服务提供方

1.接口描述

正常编码方式,提供对应服务模型接口的调用的响应结果。

2.请求示例

/**
 * 服务模型测试,服务提供方开发参考
 */
@RestController
@PostMapping(value = "/queryBodyService", method = {RequestMethod.POST, RequestMethod.GET})//一般第一层为服务名称
public class ServiceProvideController {
    @RequestMapping("/queryBloodPressure")
    public DTO test(HttpServletRequest request) throws Exception {

        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
            String sign = map.get("x-ca-signature");
            System.out.println(sign);

        }
        String json = "";
        String signHeaders = request.getHeader("HEADER_SM_ROUTER_DESTINATION");
        System.out.println(signHeaders);

        //iot传递请求参数需要用流的方式读取,@request的方式是获取不到参数的
        json = new String(readInputStream(request.getInputStream()), "UTF-8");

        System.out.println(json);
        System.out.println("++++调用成功+++");
        DTO dto = new DTO();
        dto.setCode(200);

        dto.setMessage("success");
        dto.setLocalizedMsg("OK");
        JSONObject object = new JSONObject();
        object.put("abc", "123123");
        dto.setData(object);

        return dto;

    }

    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        boolean var3 = false;

        int len;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        return outSteam.toByteArray();
    }
}
  • 本页导读 (0)
文档反馈