Spring Boot 整合 Fisco Bcos(区块链)
要在 Spring Boot 项目中整合 FISCO BCOS 并实现区块链合约的部署和调用,可以按照以下步骤进行:
backend/ ├── src/ │ ├── main/ │ │ ├── java/ │ │ │ └── com/ │ │ │ └── example/ │ │ │ ├── controller/ │ │ │ ├── service/ │ │ │ ├── config/ │ │ │ └── model/ │ │ ├── resources/ │ │ ├── application.properties └── pom.xml
在 pom.xml 中添加 FISCO BCOS SDK 的依赖:
pom.xml
<dependencies> <dependency> <groupId>org.fisco-bcos</groupId> <artifactId>fisco-bcos-java-sdk</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 其他依赖 --> </dependencies>
在 src/main/resources 目录下创建 application.properties 文件,添加 FISCO BCOS 的配置:
src/main/resources
application.properties
fisco.bcos.channelServiceGroupId=1 fisco.bcos.groupId=1 fisco.bcos.channelConnectionsPath=classpath:config.toml fisco.bcos.keystoreFile=classpath:keystore.p12 fisco.bcos.keystorePassword=YourKeystorePassword
确保 config.toml 和 keystore.p12 文件放在 src/main/resources 目录下。
config.toml
keystore.p12
创建配置类来初始化 FISCO BCOS 客户端:
package com.example.config; import org.fisco.bcos.sdk.client.Client; import org.fisco.bcos.sdk.client.ClientImpl; import org.fisco.bcos.sdk.config.ConfigOption; import org.fisco.bcos.sdk.config.model.ConfigProperty; import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair; import org.fisco.bcos.sdk.model.ConstantConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.io.File; import java.util.HashMap; import java.util.Map; @Configuration public class FiscoBcosConfig { @Value("${fisco.bcos.channelConnectionsPath}") private String channelConnectionsPath; @Value("${fisco.bcos.keystoreFile}") private String keystoreFile; @Value("${fisco.bcos.keystorePassword}") private String keystorePassword; @Bean public Client getClient() throws Exception { Map<String, Object> configMap = new HashMap<>(); configMap.put("channelConnectionsPath", new File(channelConnectionsPath).getAbsolutePath()); configMap.put("keystoreFile", new File(keystoreFile).getAbsolutePath()); configMap.put("keystorePassword", keystorePassword); ConfigProperty configProperty = new ConfigProperty(); configProperty.setAmopChannel(null); configProperty.setAccount(configMap); ConfigOption configOption = new ConfigOption(configProperty); return ClientImpl.build(ConstantConfig.DEFAULT_GROUP_ID, configOption); } @Bean public CryptoKeyPair cryptoKeyPair(Client client) { return client.getCryptoSuite().getCryptoKeyPair(); } }
创建服务类来部署和调用区块链合约:
package com.example.service; import org.fisco.bcos.sdk.client.Client; import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair; import org.fisco.bcos.sdk.model.TransactionReceipt; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ContractService { @Autowired private Client client; @Autowired private CryptoKeyPair cryptoKeyPair; public String deployContract() { // 使用 Solidity 合约编译生成的 Wrapper 类进行部署 // 例如,如果合约名为 HelloWorld,则自动生成的 Wrapper 类名为 HelloWorld HelloWorld helloWorld = HelloWorld.deploy(client, cryptoKeyPair); return helloWorld.getContractAddress(); } public String callContract(String contractAddress) { // 使用生成的 Wrapper 类调用合约方法 HelloWorld helloWorld = HelloWorld.load(contractAddress, client, cryptoKeyPair); return helloWorld.get(); } public TransactionReceipt setContract(String contractAddress, String newValue) { // 使用生成的 Wrapper 类调用合约方法 HelloWorld helloWorld = HelloWorld.load(contractAddress, client, cryptoKeyPair); return helloWorld.set(newValue); } }
创建控制器类来处理前端请求:
package com.example.controller; import com.example.service.ContractService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api/contract") public class ContractController { @Autowired private ContractService contractService; @PostMapping("/deploy") public String deployContract() { return contractService.deployContract(); } @GetMapping("/call") public String callContract(@RequestParam String contractAddress) { return contractService.callContract(contractAddress); } @PostMapping("/set") public String setContract(@RequestParam String contractAddress, @RequestParam String newValue) { contractService.setContract(contractAddress, newValue); return "success"; } }
启动 Spring Boot 应用并通过 REST API 部署和调用合约。例如:
部署合约: bash curl -X POST http://localhost:8080/api/contract/deploy
bash curl -X POST http://localhost:8080/api/contract/deploy
调用合约: bash curl -X GET "http://localhost:8080/api/contract/call?contractAddress=<contractAddress>"
bash curl -X GET "http://localhost:8080/api/contract/call?contractAddress=<contractAddress>"
设置合约值: bash curl -X POST "http://localhost:8080/api/contract/set?contractAddress=<contractAddress>&newValue=<newValue>"
bash curl -X POST "http://localhost:8080/api/contract/set?contractAddress=<contractAddress>&newValue=<newValue>"
在实际项目中,确保配置文件和私钥的安全性,不要将敏感信息硬编码到代码中,并且在生产环境中使用更强的安全措施来保护密钥和配置。
这样,一个简单的 Spring Boot 应用已经整合了 FISCO BCOS,并实现了区块链合约的部署和调用。可以根据实际需求进一步扩展和优化。