JsonRPC

    WeEvent除了少量系统管理功能外,主题的CRUD和事件的订阅发布等都支持JsonRPC协议访问。

    • JsonRPCRESTful实现的功能和参数都相同,只是不同的承载协议,可以自由选择使用。

    • WeEvent 使用JsonRPC 2.0 协议规范

    • 调用异常返回的信息BrokerException如下

    1. public class JsonRPC {
    2. private final static String groupId = "1";
    3. private final static Map<String, String> extensions = new HashMap<>();
    4.  
    5. public static void main(String[] args) {
    6. System.out.println("This is WeEvent json rpc sample.");
    7. try {
    8. URL remote = new URL("http://localhost:8080/weevent/jsonrpc");
    9. // init jsonrpc client
    10. JsonRpcHttpClient client = new JsonRpcHttpClient(remote);
    11. // init IBrokerRpc object
    12. IBrokerRpc rpc = ProxyUtil.createClientProxy(client.getClass().getClassLoader(), IBrokerRpc.class, client);
    13. // open topic
    14. rpc.open("com.weevent.test", groupId);
    15. // publish event
    16. SendResult sendResult = rpc.publish("com.weevent.test", groupId, "hello weevent".getBytes(StandardCharsets.UTF_8), extensions);
    17. System.out.println(sendResult.getStatus());
    18. } catch (MalformedURLException e) {
    19. e.printStackTrace();
    20. } catch (BrokerException e) {
    21. e.printStackTrace();
    22. }
    23. }
    24. }

    上述样例演示了使用JsonRPC如何创建主题和发布事件。完整代码,请参见 。

    接口包括两大类功能:一类是主题TopicCRUD管理,包括opencloseexiststatelist;一类是事件的发布和订阅,包括publishsubscribeunsubscribe

    • 请求
    1. $ curl -H"Content-Type: application/json" -d'{"id":"1","jsonrpc":"2.0","method":"open","params":{"topic":"com.weevent.test","groupId":"1"}}' http://localhost:8080/weevent/jsonrpc
    • 应答
    1. {
    2. "jsonrpc": "2.0",
    3. "id": "1",
    4. "result": "true"
    5. }
      • topic:主题。ascii值在[32,128]之间。支持通配符按层次订阅,因’+’、’#’为通配符的关键字故不能为topic的一部分,详情参见MQTT通配符

      • groupId:群组Idfisco-bcos 2.0+版本支持多群组功能,2.0以下版本不支持该功能可以不传。

    可以重复open,也是返回true

    关闭Topic

    • 请求
    1. $ curl -H"Content-Type: application/json" -d'{"id":"1","jsonrpc":"2.0","method":"close","params":{"topic":"com.weevent.test","groupId":"1"}}' http://localhost:8080/weevent/jsonrpc
    1. {
    2. "jsonrpc": "2.0",
    3. "id": "1",
    4. "result": "true"
    5. }

    检查Topic是否存在

    • 请求
    • 应答
    1. {
    2. "jsonrpc": "2.0",
    3. "id": "1",
    4. "result": "true"
    5. }
    • 请求
    1. $ curl -H "Content-Type: application/json" -d'{"id":"1","jsonrpc":"2.0","method":"publish","params":{"topic":"com.weevent.test","groupId":"1","content":"MTIzNDU2","extensions":{"weevent-format": "json","userId":"3924261998"}}}' http://localhost:8080/weevent/jsonrpc
    • 应答
    1. {
    2. "jsonrpc": "2.0",
    3. "id": "1",
    4. "result": {
    5. "topic": "com.weevent.test",
    6. "status": "SUCCESS",
    7. "eventId": "10-123"
    8. }
    9. }
    • 说明:
      • content:是123456进行Base64编码后的值。
      • extensions:用户自定义数据以weevent-开头。可选参数。
      • result : 返回字段result ,是一个WeEvent对象的序列化,可查看WeEvent对象。“status”:“SUCCESS”表示成功。”eventId“:”10-123”表示发布事件成功ID。

    订阅事件

    • 请求
    1. $ curl -H"Content-Type: application/json" -d'{"id":"1","jsonrpc":"2.0","method":"subscribe","params":{"topic":"com.weevent.test","groupId":"1","subscriptionId":"df68c385-f62d-437f-b32c-669211d51d88","url":"http://localhost:8080/weevent/mock/rest/onEvent"}}' http://localhost:8080/weevent/jsonrpc
    • 应答
    1. {
    2. "jsonrpc": "2.0",
    3. "id": "1",
    4. "result": "df68c385-f62d-437f-b32c-669211d51d88"
    5. }
    • 说明
      • topic:主题。ascii值在[32,128]之间。支持通配符按层次订阅,因’+’、’#’为通配符的关键字故不能为topic的一部分,详情参见MQTT通配符
      • url:事件通知回调CGI,当有生产者发布事件时,所有的事件都会通知到这个URL
      • subscriptionId:第一次订阅可以不填。继续上一次订阅subscriptionId为上次订阅ID。
      • result:订阅ID。

    取消订阅

    • 请求
    • 应答
    1. {
    2. "jsonrpc": "2.0",
    3. "id": "1",
    4. "result": "true"
    5. }
    • 说明
      • 如果Broker没有配置Zookeeper模块,该接口无法使用。
      • subscriptionId:Subscribe成功订阅后,返回的订阅ID。
    • 请求
    1. $ curl -H"Content-Type: application/json" -d '{"id":"1","jsonrpc":"2.0","method":"getEvent","params":{"eventId":"2cf24dba-59-1124","groupId":"1"}}' http://localhost:8080/weevent/jsonrpc
    1. {
    2. "jsonrpc": "2.0",
    3. "id": "1",
    4. "result": {
    5. "topic": "hello",
    6. "content": "MTIzNDU2",
    7. "extensions":{"weevent-format":"json"},
    8. "eventId": "2cf24dba-59-1124"
    9. }
    10. }

    当前Topic列表

    • 请求
    1. $ curl -H"Content-Type: application/json" -d'{"id":"1","jsonrpc":"2.0","method":"list","params":{"pageIndex":0,"pageSize":10,"groupId":"1"}}' http://localhost:8080/weevent/jsonrpc
    • 应答
    1. {
    2. "jsonrpc": "2.0",
    3. "id": "1",
    4. "result": {
    5. "total": 51,
    6. "pageIndex": 1,
    7. "pageSize": 10,
    8. "topicInfoList": [
    9. {
    10. "topicName": "123456",
    11. "topicAddress": "0x420f853b49838bd3e9466c85a4cc3428c960dde2",
    12. "senderAddress": "0x64fa644d2a694681bd6addd6c5e36cccd8dcdde3",
    13. "createdTimestamp": 1548211117753
    14. }
    15. ]
    16. }
    17. }
    • 说明
      • total:Topic的总数量
      • pageIndex::查询第几页,从0开始
      • pageSize: 分页大小(0,100),默认每页10条数据。
      • topicInfoList:Topic 详细信息列表

    查询某个Topic详情

    • 请求
    • 应答
    1. {
    2. "jsonrpc": "2.0",
    3. "id": "1",
    4. "result": {
    5. "topicName": "com.weevent.test",
    6. "topicAddress": "0x171befab4c1c7e0d33b5c3bd932ce0112d4caecd",
    7. "senderAddress": "0x64fa644d2a694681bd6addd6c5e36cccd8dcdde3",
    8. "createdTimestamp": 1548328570965,
    9. "sequenceNumber": 9,
    10. "blockNumber": 2475
    11. }
    12. }
    • 说明
      • topicAddress: 区块链上的合约地址。
      • createdTimestamp:Topic创建的时间。
      • sequenceNumber:已发布事件数。