适配Fabric¶
WeEvent支持区块链Fabric 1.4。以下安装以CentOS 7.2为例。
前置条件¶
安装
weevent-broker, 具体安装步骤weevent-broker章节以
weevent-broker安装到/usr/local/weevent/weevent-broker-1.3.0目录为例。安装
Fabric 1.4区块链节点(以官网Fabric Samples安装为例),具体安装步骤, 请参见Fabric安装。
以
Fabric 1.4安装到/usr/local/fabric/fabric-sample目录为例。
修改Fabric的配置文件¶
配置
Fabric节点的证书文件配置
WeEvent访问区块链$ cd /usr/local/weevent/weevent-broker-1.3.0/ $ vi ./conf/weevent.properties
修改
broker.blockchain.type配置项为:broker.blockchain.type=fabric。配置证书文件
将
Fabric目录/usr/local/fabric/fabric-sample/first-network/crypto-config/所有文件,拷贝到
weevent-broker安装目录下/conf/fabric/crypto-config/里。区块链节点配置文件fabric.properties
$ vi ./conf/fabric/fabric.properties
修改配置项
chain.organizations.user.keyfile=fabric/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/xxx_sk chain.peer.address=127.0.0.1:7051 chain.orderer.address=127.0.0.1:7050
替换
127.0.0.1为部署Fabric节点的ip (peer端口默认为7051,orderer端口默认为7050)
部署系统合约
运行脚本
./deploy-fabric-topic-control.sh部署合约。例如:$ ./deploy-fabric-topic-control.sh deploy begin deploy topic and topicController contract. begin add topic into topicController contract. deploy contract success.
重启服务¶
停止服务
$ ./broker.sh stop stop broker success remove the crontab job success
启动服务
$ ./broker.sh start start broker success (PID=89054) add the crontab job success
RESTful请求样例¶
创建Topic,发布事件
$ curl "http://localhost:8080/weevent/rest/open?topic=com.weevent.test&groupId=mychannel" [1] 16414 $ true $ curl "http://localhost:8080/weevent/rest/publish?topic=com.weevent.test&groupId=mychannel&content=123456&weevent-format=json" $ {"status":"SUCCESS","topic":"com.weevent.test","eventId":"317e7c4c-301009-6705"}
代码样例¶
API里所有的groupId就是Fabric的channelname, 以发布事件为例:
public class Rest {
public static void main(String[] args) {
System.out.println("This is WeEvent restful sample.");
try {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
RestTemplate rest = new RestTemplate(requestFactory);
// ensure topic exist "com.weevent.test"
String topic = "com.weevent.test";
ResponseEntity<BaseResponse<Boolean>> rsp = rest.exchange("http://localhost:7000/weevent-broker/rest/open?topic={topic}&groupId={groupId}", HttpMethod.GET, null, new ParameterizedTypeReference<BaseResponse<Boolean>>() {
}, topic, "mychannel");
System.out.println(rsp.getBody().getData());
// publish event to topic "com.weevent.test"
SendResult sendResult = rest.getForEntity("http://localhost:8080/weevent/rest/publish?topic={topic}&groupId={groupId}&content={content}",
SendResult.class,
topic,
"mychannel",
"hello weevent".getBytes(StandardCharsets.UTF_8)).getBody();
System.out.println(sendResult.getStatus());
System.out.println(sendResult.getEventId());
} catch (RestClientException e) {
e.printStackTrace();
}
}
}