Fisco-Bcos区块链java应用开发

基于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创建项目以及创建各种配置文件

  1. 创建一个Gradle工程

在这里插入图片描述

  1. 在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()
}
  1. 将sol2java.sh生的java文件放到工程中,目录结构如下:

在这里插入图片描述

  1. 将节点证书以及生成的abi、bin目录放到资源目录下

在这里插入图片描述

节点证书就是fisco/nod

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值