Springboot整合FISCO

Springboot整合FISCO2.9.2

配置文件

整合FSICO2.9.2有三种方式

  • properties格式
  • yml格式
  • xml格式(推荐这种,只需要写一个配置文件引入资源即可)

Java sdk主要包括五个配置选项,分别是

  • 证书配置(必须)
  • 网络连接配置 (必须)
  • AMOP配置(非必须)
  • 账户配置(非必须,不配置则使用默认配置值)
  • 线程池配置(非必须,不配置则使用默认配置值)

properties格式

cryptoMaterial.certPath=conf                       # The certification path  

# The following configurations take the certPath by default if commented
# cryptoMaterial.caCert=conf/ca.crt 
# cryptoMaterial.sslCert=conf/sdk.crt
# cryptoMaterial.sslKey=conf/sdk.key
# cryptoMaterial.enSslCert=conf/gm/gmensdk.crt
# cryptoMaterial.enSslKey=conf/gm/gmensdk.key


# The peer list to connect
network.peers[0]=127.0.0.1:20200
network.peers[0]=127.0.0.1:21200


# AMOP configuration

# Configure a private topic as a topic message sender.
# amop[0].publicKeys[0]=conf/amop/consumer_public_key_1.pem
# amop[0].topicName=PrivateTopic1

# Configure a private topic as a topic subscriber.
# amop[1].password=123456 
# amop[1].privateKey=conf/amop/consumer_private_key.p12
# amop[1].topicName=PrivateTopic2


account.keyStoreDir=account
# account.accountFilePath=conf
account.accountFileFormat=pem
# account.accountAddress=0x
# account.password=123456

# threadPool.channelProcessorThreadSize=16
# threadPool.receiptProcessorThreadSize=16
threadPool.maxBlockingQueueSize=102400

需要创建配置类

@Data
@ToString
@Component
@ConfigurationProperties
@PropertySource(value = "classpath:fisco-config.properties", ignoreResourceNotFound = true, encoding = "UTF-8")
public class BcosConfig {
    private Map<String, Object> cryptoMaterial;
    public Map<String, List<String> > network;
    public List<AmopTopic> amop;
    public Map<String, Object> account;
    public Map<String, Object> threadPool;
}

然后初始化BcosSDK

@Slf4j
@Data
@Component
public class FiscoBcos {

    @Autowired
    BcosConfig bcosConfig;

    BcosSDK bcosSDK;

    public void init() {
        ConfigProperty configProperty = loadProperty();
        ConfigOption configOption;
        try {
            configOption = new ConfigOption(configProperty, CryptoType.ECDSA_TYPE);
        } catch (ConfigException e) {
            log.error("init error:" + e.toString());
            return ;
        }
        bcosSDK = new BcosSDK(configOption);
    }

    public ConfigProperty loadProperty() {
        ConfigProperty configProperty = new ConfigProperty();
        configProperty.setCryptoMaterial(bcosConfig.getCryptoMaterial());
        configProperty.setAccount(bcosConfig.getAccount());
        configProperty.setNetwork(new HashMap<String, Object>(){{
            put("peers", bcosConfig.getNetwork().get("peers"));
        }} );
        configProperty.setAmop(bcosConfig.getAmop());
        configProperty.setThreadPool(bcosConfig.getThreadPool());
        return configProperty;
    }
}

yml格式

cryptoMaterial:                     
  certPath: "conf"                   
#  caCert: "conf/ca.crt"               
#  sslCert: "conf/sdk.crt"             
#  sslKey: "conf/sdk.key"
#  enSslCert: "conf/gm/gmensdk.crt"
#  enSslKey: "conf/gm/gmensdk.key"

network:
  peers:
    - "127.0.0.1:20201"
    - "127.0.0.1:20200"

amop:
#  - publicKeys: [ "conf/amop/consumer_public_key_1.pem" ]
#    topicName: "PrivateTopic1"
#  - password: "123456"
#    privateKey: "conf/amop/consumer_private_key.p12"
#    topicName: "PrivateTopic2"

account:
  keyStoreDir: "account"
#  accountFilePath: "conf"
  accountFileFormat: "pem"
#  accountAddress: "0x"
#  password: ""


threadPool:
#  channelProcessorThreadSize: "16"
#  receiptProcessorThreadSize: "16"
#  maxBlockingQueueSize: "102400"

与上述方法一致

xml格式

重点讲述xml格式,因为这种方式比较简单只需要写xml即可,配置类上加注释

application.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>

配置类

/**
 * @Author: zyt
 * @Description: 加载配置文件
 * @Date: Created in 10:04 2021/1/7
 */
@ImportResource(locations = "classpath:applicationContext.xml")
@Configuration
public class FiscoConfig {
}

导入jar包

配置完成后需要导入fisco所需的jar包,需要经过调试爆红后依次去maven仓库里面下载jar最后导入

fisco-bcos-java-sdk-2.9.2.jar
commons-lang3-3.4.jar
bcprov-jdk15on-1.67.jar
netty-all-4.1.42.Final.jar
netty-sm-ssl-context-1.3.0.jar
solcJ-0.4.25.1.jar
tcnative-2.0.34.0.jar
webank-blockchain-java-crypto-1.0.3.jar
weid-contract-java-1.2.24.jar

在这里插入图片描述
在这里插入图片描述

在项目里创建一个libraries的文件夹并点击右键标记即可

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9fx5rkY5-1677114630488)(E:\啊哈\JAVA学习笔记\assets\1675393133910.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WcHpB6UB-1677114630488)(E:\啊哈\JAVA学习笔记\assets\1675393089660.png)]

测试

最后在主程序测试运行,查看容器中是否包含有bcosSDK这个实例

@SpringBootApplication
public class FiscoJavaSdkApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(FiscoJavaSdkApplication.class, args);
        boolean b = run.containsBean("bcosSDK");
        System.out.println(b);
    }
}

测试成功说明配置完成。
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值