In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Spring Boot to integrate Fisco Bcos". In daily operation, I believe many people have doubts about how to use Spring Boot to integrate Fisco Bcos. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Spring Boot to integrate Fisco Bcos"! Next, please follow the editor to study!
Brief introduction
FISCO BCOS is an enterprise-level financial alliance chain underlying platform led by domestic enterprises, open source, secure and controllable. It was created by the Golden chain Alliance Open Source working Group and officially opened to the outside world in 2017.
At present, there are many mature blockchain platforms, and FISCO BCOS is chosen mainly because the documentation is detailed and easy to get started.
Official address entrance
Local environment
The case of Spring Boot integrating Fisco Bcos introduced in this article is deployed and verified on Ali CVM.
There are two computers on my side:
Computer 1: local Mac computer, no blockchain environment, only used for SpringBoot project development
Computer 2: the CentOS server on Aliyun is configured with a blockchain environment. Contract deployment, compilation and jar package of SpringBoot project are all run on this computer.
According to your own computer environment, you can compare this case for development.
Main process:
-- > 1. Build and verify Fisco Bcos environment
-- > 2. Create SpringBoot project and configure dependencies
-- > 3. Write case code
-- > 4. Generate jar package and deploy server verification
1. Construction and verification of Fisco Bcos environment
The Fisco Bcos environment is built with reference to the official documentation:
Build the first block chain network
The operating system of my test server is CentOS. If it is another operating system, it is also configured with reference to this document, and the process is similar.
For the detailed process, please refer to the documentation for configuration. Here are the key details:
1.1. Build a single group 4-node alliance chain:
What I use here is the national secret version of the command:
Bash build_chain.sh-l 127.0.0.1 4-p 30300 Magi 20200 Magi 8545-g-G
Main reasons:
The Fisco Bcos SDK version I use here is 2.8.0. The national secret certificate is loaded by default when the certificate is loaded (there should be an option to load the certificate type. The relevant API has not been found yet).
If you are using a non-secret version of the command, no national secret certificate will be generated in the / fisco/node/127.0.0.1/sdk/ directory, and an error will be reported if you use this SDK.
1.2. Check the certificate
After successfully starting all nodes, verify the existence of all certificates under the / fisco/node/127.0.0.1/sdk/ directory (gm represents the national secret), as shown below:
1.3. Use certificates to verify the correctness of nodes
After starting the node, we can use the local console program console provided by Fisco Bcos to verify the node.
Please refer to the documentation and download and configure the console program first.
Note: configure node certificates for the console program (that is, copy all the certificates under / fisco/node/127.0.0.1/sdk/ to the / console/conf/ directory of the console program)
Start the console and test the node, for example: get the data height of the blockchain: getBlockNumber:
If the contract can be deployed normally and the data height can be obtained, there is no problem with the blockchain environment, as shown below:
2. Create a SpringBoot project and configure dependencies
The server-side blockchain environment has been verified. Next, we create the SpringBoot project and integrate the Fisco Bcos Java version of SDK.
Java SDK document
2.1.Create a SpringBoot project:
Just check Spring Web:
2.2.Configuring pom.xml
Note: the SpringBoot version should not be too high (it has been confirmed with the official technician). When I have tried the 2.6.2 demo case, the node will flicker abnormally. When the version is reduced to 2.4.2, it will be normal:
Configure SDK dependencies for Fisco Bcos Java:
Pom.xml complete code:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.2 com.qxc demo_bcos 0.0.1-SNAPSHOT demo_bcos Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework .boot spring-boot-starter-test test org.fisco-bcos.java-sdk fisco-bcos-java-sdk 2.8.0 org.slf4j slf4j-log4j12 Org.springframework.boot spring-boot-maven-plugin
3. Write the case code
3.1Config Fisco Bcos:
Java SDK »configuration instructions
For simplicity, this case uniformly configures the parameters of Fisco Bcos through xml and automatically reads them in the code.
Create the file fisco-config.xml under the / src/main/resources/ directory:
Fisco-config.xml complete code:
127.0.0.1:20200 127.0.0.1:20201 3.2 、 Configure the node certificate:
Copy the certificate under the blockchain node to the / src/main/resources/conf/ directory (the conf directory is the certificate path configured by fisco-config.xml):
3.3.Writing controller
BcosController complete code:
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 "";} 3.4.No modification is made by DemoBcosApplication by default
Application.properties is not configured with anything.
For simplicity, I directly use 8080 on the port, mainly because individuals are lazy and don't bother to configure anything. O (∩ _ ∩) O ~
4. Generate jar package, deploy server verification 4.1, and run first locally.
The development of SpringBoot is carried out on my local Mac computer, in order to securely deploy to a remote CentOS server
First, run locally to see if the project is compiled and running properly (there is no need to test the block chain function at this time, because I do not have a block chain environment locally):
No problem. Perfect.
4.2.Packaging jar
Send the jar package to the remote server and run:
The case program is already running on the server. Go back to your local mac computer and try connecting to the server remotely (IP will not be shown to you):
At this point, the study on "how to integrate Fisco Bcos with Spring Boot" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.