簡介
FISCO BCOS是由國內企業主導研發、對外開源、安全可控的企業級金融聯盟鏈底層平臺,由金鏈盟開源作業組協作打造,并于2017年正式對外開源,
目前,成熟的區塊鏈的平臺不少,之所以選擇FISCO BCOS,主要是因為檔案細致,容易入門,
官方地址入口
流程說明:
本篇文章介紹的 Spring Boot 整合 Fisco Bcos的案例,是在阿里云服務器上部署驗證的:
--> 1、Fisco Bcos環境搭建與驗證
--> 2、創建SpringBoot工程并配置依賴
--> 3、撰寫案例代碼
--> 4、生成jar包、部署服務器驗證
1、Fisco Bcos環境搭建與驗證
Fisco Bcos環境搭建參考的是官方的檔案:
搭建第一個區塊鏈網路
我這邊測驗服務器的作業系統是CentOS,如果是其他作業系統,也是參照該檔案進行配置,流程是類似的,
詳細流程,大家參照檔案進行配置就行了,這里主要說下關鍵的細節:
1.1、搭建單群組4節點聯盟鏈:
我這邊使用的是國密版本命令:
bash build_chain.sh -l 127.0.0.1:4 -p 30300,20200,8545 -g -G
主要原因:
我這邊使用的Fisco Bcos SDK版本是2.8.0,加載證書時默認會加載國密證書(應該有加載證書型別的選項配置,目前暫未找到相關API),
如果使用的是非國密版本命令,在/fisco/node/127.0.0.1/sdk/目錄下不會生成國密證書,使用該SDK就會報錯,
1.2、檢查證書
成功啟動所有節點后,在/fisco/node/127.0.0.1/sdk/目錄下驗證所有證書是否存在(gm代表國密),如下圖:

1.3、使用證書驗證節點正確性
啟動節點后,我們可以使用Fisco Bcos提供的本地控制臺程式console對節點進行驗證,
大家參照檔案,先下載、配置控制臺程式,
注意:為控制臺程式配置節點證書(即:將/fisco/node/127.0.0.1/sdk/下的證書全部復制到控制臺程式的 /console/conf/目錄下)

啟動控制臺,測驗節點,例如:獲取區塊鏈資料高度:getBlockNumber:
如果能正常部署合約,且能獲得資料高度,則區塊鏈環境沒什么問題,如下圖:

2、創建SpringBoot工程并配置依賴
服務端區塊鏈環境已完成驗證,接下來,我們創建SpringBoot工程,并集成Fisco Bcos Java版SDK,
Java SDK 檔案
2.1、創建SpringBoot工程:


僅勾選Spring Web即可:

2.2、配置pom.xml
注意:SpringBoot版本不宜過高(已與官方技術人員確認),我這邊試過2.6.2+,Demo案例呼叫節點時會例外閃退,當把版本降低為2.4.2就正常了:

配置Fisco Bcos Java版SDK依賴:

pom.xml完整代碼:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.qxc</groupId>
<artifactId>demo_bcos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo_bcos</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fisco-bcos.java-sdk</groupId>
<artifactId>fisco-bcos-java-sdk</artifactId>
<version>2.8.0</version>
<!--排除這個slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、撰寫案例代碼
3.1、配置Fisco Bcos:
Java SDK ? 配置說明
為了簡單,本案例將Fisco Bcos的引數通過xml統一配置,并在代碼中自動讀取,
在/src/main/resources/目錄下創建檔案fisco-config.xml:

fisco-config.xml完整代碼:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="defaultConfigProperty" >
<property name="cryptoMaterial">
<map>
<entry key="certPath" value="https://www.cnblogs.com/qixingchao/p/conf" />
</map>
</property>
<property name="network">
<map>
<entry key="peers">
<list>
<value>127.0.0.1:20200</value>
<value>127.0.0.1:20201</value>
</list>
</entry>
</map>
</property>
<property name="account">
<map>
<entry key="keyStoreDir" value="https://www.cnblogs.com/qixingchao/p/account" />
<entry key="accountAddress" value="" />
<entry key="accountFileFormat" value="https://www.cnblogs.com/qixingchao/p/pem" />
<entry key="password" value="" />
<entry key="accountFilePath" value="" />
</map>
</property>
<property name="threadPool">
<map>
<entry key="channelProcessorThreadSize" value="https://www.cnblogs.com/qixingchao/p/16" />
<entry key="receiptProcessorThreadSize" value="https://www.cnblogs.com/qixingchao/p/16" />
<entry key="maxBlockingQueueSize" value="https://www.cnblogs.com/qixingchao/p/102400" />
</map>
</property>
</bean>
<bean id="defaultConfigOption" >
<constructor-arg name="configProperty">
<ref bean="defaultConfigProperty"/>
</constructor-arg>
</bean>
<bean id="bcosSDK" >
<constructor-arg name="configOption">
<ref bean="defaultConfigOption"/>
</constructor-arg>
</bean>
</beans>
3.2、配置節點證書:
把區塊鏈節點下的證書拷貝到/src/main/resources/conf/目錄下(conf目錄為fisco-config.xml配置的證書路徑):

3.3、撰寫controller

BcosController完整代碼:
package com.qxc.demo_bcos.controller;
import org.fisco.bcos.sdk.BcosSDK;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.client.protocol.response.BlockNumber;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BcosController {
@GetMapping("/test")
public String test(){
System.out.println("-----test------");
return "this is bcos demo";
}
@GetMapping("/block")
public String getBlockNumber(){
System.out.println("-----getBlockNumber getBlockNumber------");
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:fisco-config.xml");
System.out.println("-----getBlockNumber ClassPathXmlApplicationContext ok------");
BcosSDK bcosSDK = context.getBean(BcosSDK.class);
System.out.println("-----getBlockNumber BcosSDK ok------");
Client client = bcosSDK.getClient(Integer.valueOf(1));
System.out.println("-----getBlockNumber client ok------");
BlockNumber blockNumber = client.getBlockNumber();
return "getBlockNumber: "+blockNumber.getBlockNumber().toString();
// return "";
}
}
3.4、DemoBcosApplication默認不做修改

3.5、application.properties什么也沒配置
為了簡單,埠我這邊直接使用8080,主要是個人比較懶,什么都懶得配置了,O(∩_∩)O~

4、生成jar包、部署服務器驗證
4.1、本地先跑一把
SpringBoot的開發是在我本地的Mac電腦上進行的,為了能穩妥的部署到遠程CentOS服務器上,
先在本地跑一把,看看工程編譯運行是否正常(此時不用測驗區塊鏈功能,因為我本地并沒有區塊鏈環境):


沒問題,完美,
4.2、打包jar

4.3、把jar包發送到遠程服務器上,并運行:

案例程式已在服務器端跑起來了,回到本地mac電腦,遠程連服務器試一下吧(IP就不展示給大家看了哈):

至此,使用Spring Boot 整合 Fisco Bcos 最最基本的案例已完成,
總結
Fisco Bcos 的使用還是很簡單的,如果有問題大家可以直接查詢官方技術檔案,也歡迎留言討論,咱們共同學習、共同進步,哈哈~~,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/403547.html
標籤:區塊鏈
