FISCO BCOS是由国内企业主导研发、对外开源、安全可控的企业级金融联盟链底层平台,由金链盟开源工作组协作打造,并于2017年正式对外开源。
目前,成熟的区块链的平台不少,之所以选择FISCO BCOS,主要是因为文档细致,容易入门。
官方地址入口
本篇文章介绍的 Spring Boot 整合 Fisco Bcos的案例,是在阿里云服务器上部署验证的。 我这边共有两台电脑: 电脑1:本地的Mac电脑,没有区块链环境,只是用于SpringBoot工程的开发; 电脑2:阿里云上的CentOS服务器,配置了区块链环境,合约部署、编译、SpringBoot工程的jar包运行都是在该电脑上;
大家可根据自己的电脑环境,对比该案例进行开发即可。
--> 1、Fisco Bcos环境搭建与验证 --> 2、创建SpringBoot工程并配置依赖 --> 3、编写案例代码 --> 4、生成jar包、部署服务器验证
Fisco Bcos环境搭建参考的是官方的文档: 搭建第一个区块链网络 我这边测试服务器的操作系统是CentOS,如果是其他操作系统,也是参照该文档进行配置,流程是类似的。 详细流程,大家参照文档进行配置就行了,这里主要说下关键的细节:
我这边使用的是国密版本命令: 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就会报错。
成功启动所有节点后,在/fisco/node/127.0.0.1/sdk/目录下验证所有证书是否存在(gm代表国密),如下图:
启动节点后,我们可以使用Fisco Bcos提供的本地控制台程序console对节点进行验证。 大家参照文档,先下载、配置控制台程序。 注意:为控制台程序配置节点证书(即:将/fisco/node/127.0.0.1/sdk/下的证书全部复制到控制台程序的 /console/conf/目录下) 启动控制台,测试节点,例如:获取区块链数据高度:getBlockNumber: 如果能正常部署合约,且能获得数据高度,则区块链环境没什么问题,如下图:
服务端区块链环境已完成验证,接下来,我们创建SpringBoot工程,并集成Fisco Bcos Java版SDK。 Java SDK 文档
仅勾选Spring Web即可:
注意:SpringBoot版本不宜过高(已与官方技术人员确认),我这边试过2.6.2+,Demo案例调用节点时会异常闪退,当把版本降低为2.4.2就正常了: 配置Fisco Bcos Java版SDK依赖:
<?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>
<?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>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Java SDK ? 配置说明 为了简单,本案例将Fisco Bcos的参数通过xml统一配置,并在代码中自动读取。 在/src/main/resources/目录下创建文件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" class="org.fisco.bcos.sdk.config.model.ConfigProperty"> <property name="cryptoMaterial"> <map> <entry key="certPath" value="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="account" /> <entry key="accountAddress" value="" /> <entry key="accountFileFormat" value="pem" /> <entry key="password" value="" /> <entry key="accountFilePath" value="" /> </map> </property> <property name="threadPool"> <map> <entry key="channelProcessorThreadSize" value="16" /> <entry key="receiptProcessorThreadSize" value="16" /> <entry key="maxBlockingQueueSize" value="102400" /> </map> </property> </bean> <bean id="defaultConfigOption" class="org.fisco.bcos.sdk.config.ConfigOption"> <constructor-arg name="configProperty"> <ref bean="defaultConfigProperty"/> </constructor-arg> </bean> <bean id="bcosSDK" class="org.fisco.bcos.sdk.BcosSDK"> <constructor-arg name="configOption"> <ref bean="defaultConfigOption"/> </constructor-arg> </bean></beans>
<?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" class="org.fisco.bcos.sdk.config.model.ConfigProperty">
<property name="cryptoMaterial">
<map>
<entry key="certPath" value="conf" />
</map>
</property>
<property name="network">
<entry key="peers">
<list>
<value>127.0.0.1:20200</value>
<value>127.0.0.1:20201</value>
</list>
</entry>
<property name="account">
<entry key="keyStoreDir" value="account" />
<entry key="accountAddress" value="" />
<entry key="accountFileFormat" value="pem" />
<entry key="password" value="" />
<entry key="accountFilePath" value="" />
<property name="threadPool">
<entry key="channelProcessorThreadSize" value="16" />
<entry key="receiptProcessorThreadSize" value="16" />
<entry key="maxBlockingQueueSize" value="102400" />
</bean>
<bean id="defaultConfigOption" class="org.fisco.bcos.sdk.config.ConfigOption">
<constructor-arg name="configProperty">
<ref bean="defaultConfigProperty"/>
</constructor-arg>
<bean id="bcosSDK" class="org.fisco.bcos.sdk.BcosSDK">
<constructor-arg name="configOption">
<ref bean="defaultConfigOption"/>
</beans>
把区块链节点下的证书拷贝到/src/main/resources/conf/目录下(conf目录为fisco-config.xml配置的证书路径):
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;@RestControllerpublic 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 ""; }}
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 "";
为了简单,端口我这边直接使用8080,主要是个人比较懒,什么都懒得配置了,O(∩_∩)O~
SpringBoot的开发是在我本地的Mac电脑上进行的,为了能稳妥的部署到远程CentOS服务器上, 先在本地跑一把,看看工程编译运行是否正常(此时不用测试区块链功能,因为我本地并没有区块链环境): 没问题,完美。
案例程序已在服务器端跑起来了,回到本地mac电脑,远程连服务器试一下吧(IP就不展示给大家看了哈):
至此,使用Spring Boot 整合 Fisco Bcos 最最基本的案例已完成。
Fisco Bcos 的使用还是很简单的,如果有问题大家可以直接查询官方技术文档,也欢迎留言讨论,咱们共同学习、共同进步,哈哈~~。
下一节:Spring Boot 整合 Fisco Bcos(部署、调用区块链合约)
原文链接:http://www.cnblogs.com/qixingchao/p/15761854.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728