四、Fabric2.4.6 智能合约(java)(ubuntu 22.04)
1、pom.xml 引入依赖
# pom.xml中加入fabric依赖
<dependency>
<groupId>org.hyperledger.fabric-sdk-java</groupId>
<artifactId>fabric-sdk-java</artifactId>
<version>2.2.12</version>
</dependency>
<dependency>
<groupId>org.hyperledger.fabric</groupId>
<artifactId>fabric-gateway</artifactId>
<version>1.0.1</version>
</dependency>
2、拷贝crypto-config
# 拷贝身份信息文件 crypto-config 到resources目录下
3、用户身份配置
# application.properties
# 下面配置是用哪个用户连接到网络
fabric.mspId=Org1MSP
# 证书路径
fabric.certificatePath=src/main/resources/crypto-config/peerOrganizations/org1.supervisor.com/users/User1@org1.supervisor.com/msp/signcerts/User1@org1.supervisor.com-cert.pem
# 私钥路径
fabric.privateKeyPath=src/main/resources/crypto-config/peerOrganizations/org1.supervisor.com/users/User1@org1.supervisor.com/msp/keystore/priv_sk
# tls证书路径
fabric.tlsCertPath=src/main/resources/crypto-config/peerOrganizations/org1.supervisor.com/peers/peer0.org1.supervisor.com/tls/ca.crt
# 通道
fabric.channel=mychannel
logging.level.org.hyperledger=trace
4、增加属性类
# 在FabricContractApiApplication 启动类,需要加入@EnableConfigurationProperties注解,否则程序启动读取不到appliation.properties属性;
@Configuration
@ConfigurationProperties(prefix = "fabric")
@Data
public class HyperLedgerFabricProperties {
String mspId;
String certificatePath;
String privateKeyPath;
String tlsCertPath;
String channel;
}
5、gateway配置
@Configuration
@AllArgsConstructor
@Slf4j
public class HyperLedgerFabricGatewayConfig {
@Autowired
final HyperLedgerFabricProperties hyperLedgerFabricProperties;
@Bean
public Gateway gateway() throws Exception {
BufferedReader certificateReader = Files.newBufferedReader(Paths.get(hyperLedgerFabricProperties.getCertificatePath()), StandardCharsets.UTF_8);
X509Certificate certificate = Identities.readX509Certificate(certificateReader);
BufferedReader privateKeyReader = Files.newBufferedReader(Paths.get(hyperLedgerFabricProperties.getPrivateKeyPath()), StandardCharsets.UTF_8);
PrivateKey privateKey = Identities.readPrivateKey(privateKeyReader);
Gateway gateway = Gateway.newInstance()
.identity(new X509Identity(hyperLedgerFabricProperties.getMspId() , certificate))
.signer(Signers.newPrivateKeySigner(privateKey))
.connection(newGrpcConnection())
.evaluateOptions(CallOption.deadlineAfter(5, TimeUnit.SECONDS))
.endorseOptions(CallOption.deadlineAfter(15, TimeUnit.SECONDS))
.submitOptions(CallOption.deadlineAfter(5, TimeUnit.SECONDS))
.commitStatusOptions(CallOption.deadlineAfter(1, TimeUnit.MINUTES))
.connect();
log.info("=========================================== connected fabric gateway {} " , gateway);
return gateway;
}
private ManagedChannel newGrpcConnection() throws IOException, CertificateException {
Reader tlsCertReader = Files.newBufferedReader(Paths.get(hyperLedgerFabricProperties.getTlsCertPath()));
X509Certificate tlsCert = Identities.readX509Certificate(tlsCertReader);
// fabric 2.4以后,支持连接一个节点即可;
return NettyChannelBuilder.forTarget("peer0.org1.supervisor.com:7051")
.sslContext(GrpcSslContexts.forClient().trustManager(tlsCert).build())
.overrideAuthority("peer0.org1.supervisor.com")
.build();
}
@Bean
public Network network(Gateway gateway) {
return gateway.getNetwork(hyperLedgerFabricProperties.getChannel());
}
@Bean
public Contract hashInfoContract(Network network) {
// fabric-smart-contract 链码
// HashInfoContract 合约
return network.getContract("fabric-smart-contract" , "HashInfoContract");
}
@Bean
public ChaincodeEventListener chaincodeEventListener(Network network) {
return new ChaincodeEventListener(network);
}
}
6、事件监听
@Slf4j
public class ChaincodeEventListener implements Runnable{
final Network network;
public ChaincodeEventListener(Network network) {
this.network = network;
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
thread.setName(this.getClass() + "chaincode_event_listener");
return thread;
}
});
executor.execute(this);
}
@Override
public void run() {
CloseableIterator<ChaincodeEvent> events = network.getChaincodeEvents("fabric-smart-contract");
log.info("chaincodeEvents {} " , events);
// events.hasNext() 会阻塞等待
while (events.hasNext()) {
ChaincodeEvent event = events.next();
log.info("receive chaincode event {} , transaction id {} , block number {} , payload {} "
, event.getEventName() , event.getTransactionId() , event.getBlockNumber() , StringUtils.newStringUtf8(event.getPayload()));
}
}
}
# 在java应用访问智能合约
# git地址:https://gitee.com/baihu_shusheng/fabric-contract-api.git
六、Fabric2.4.6 prometheus & grafana 监控网络 (ubuntu 22.04)
参考:
https://www.bilibili.com/video/BV1eS4y1L7Zo/?spm_id_from=333.788.recommend_more_video.2&vd_source=ef83736476a46166544cd38458b6b130
https://www.bilibili.com/video/BV1yR4y1G7fZ/?spm_id_from=333.337.search-card.all.click&vd_source=ef83736476a46166544cd38458b6b130
https://github.com/hyperledger/fabric-gateway-java/
https://github.com/hyperledger/fabric-gateway
网友评论