基于Fisco-Bcos的java应用开发
工具
- IDEA
- 一台linux服务器或linux虚拟机
一:搭建区块链网络
搭建第一个区块链网络
这一个看文档就可以了,没什么需要特别注意的
搭建完之后,启动节点
cd ~/fisco/console
bash nodes/127.0.0.1/start_all.sh
启动成功后会输出
try to start node0
try to start node1
try to start node2
try to start node3
node1 start successfully
node2 start successfully
node0 start successfully
node3 start successfully
二:将sol合约编译成java文件
准备一个智能合约,官方在fisco/console/contracts/solidity
中提供了HelloWorld.sol
智能合约,现在使用sol2java.sh将contracts/solidity下的所有合约编译产生bin,abi,java工具类。
cd ~/fisco/console
bash sol2java.sh org.com.fisco
得到返回
*** Compile solidity TableTest.sol***
INFO: Compile for solidity TableTest.sol success.
*** Convert solidity to java for TableTest.sol success ***
*** Compile solidity KVTableTest.sol***
INFO: Compile for solidity KVTableTest.sol success.
*** Convert solidity to java for KVTableTest.sol success ***
*** Compile solidity HelloWorld.sol***
INFO: Compile for solidity HelloWorld.sol success.
*** Convert solidity to java for HelloWorld.sol success ***
*** Compile solidity Table.sol***
INFO: Compile for solidity Table.sol success.
*** Convert solidity to java for Table.sol success ***
*** Compile solidity ShaTest.sol***
INFO: Compile for solidity ShaTest.sol success.
*** Convert solidity to java for ShaTest.sol success ***
看一下新生成的目录结构
cd ~/fisco/console/contracts
tree sdk
sdk
├── abi
│ ├── Asset.abi
│ ├── HelloWorld.abi
│ ├── KVTableTest.abi
│ ├── ShaTest.abi
│ ├── sm
│ │ ├── Asset.abi
│ │ ├── HelloWorld.abi
│ │ ├── KVTableTest.abi
│ │ ├── ShaTest.abi
│ │ ├── Table.abi
│ │ └── TableTest.abi
│ ├── Table.abi
│ └── TableTest.abi
├── bin
│ ├── Asset.bin
│ ├── HelloWorld.bin
│ ├── KVTableTest.bin
│ ├── ShaTest.bin
│ ├── sm
│ │ ├── Asset.bin
│ │ ├── HelloWorld.bin
│ │ ├── KVTableTest.bin
│ │ ├── ShaTest.bin
│ │ ├── Table.bin
│ │ └── TableTest.bin
│ ├── Table.bin
│ └── TableTest.bin
└── java
└── org
├── com
│ └── fisco
│ ├── Asset.java
│ ├── HelloWorld.java
│ ├── KVTableTest.java
│ ├── ShaTest.java
│ ├── Table.java
│ └── TableTest.java
└── fisco
└── bcos
└── asset
└── contract
├── Asset.java
├── HelloWorld.java
├── KVTableTest.java
├── ShaTest.java
├── Table.java
└── TableTest.java
12 directories, 36 files
上面就是编译生成的java文件以及abi,bin目录
三:在Idea创建项目以及创建各种配置文件
- 创建一个Gradle工程
- 在build.gradle中加入以下依赖
- Spring
- fisco-bcos
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
//需要加入的↓
def spring_version = "4.3.27.RELEASE"
List spring = [
"org.springframework:spring-core:$spring_version",
"org.springframework:spring-beans:$spring_version",
"org.springframework:spring-context:$spring_version",
"org.springframework:spring-tx:$spring_version",
]
//需要加入的↑
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
//需要加入的↓
compile ('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:2.7.1')
compile spring
compile ('org.slf4j:slf4j-log4j12:1.7.25')
runtime ('org.slf4j:slf4j-log4j12:1.7.25')
//需要加入的↑
}
test {
useJUnitPlatform()
}
- 将sol2java.sh生的java文件放到工程中,目录结构如下:
- 将节点证书以及生成的abi、bin目录放到资源目录下
节点证书就是fisco/nod