配置说明

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

1. 快速配置

配置步骤:

  1. 在您的应用的主目录下新建一个conf目录。

  2. 从节点nodes/${ip}/sdk/ 目录下的证书拷贝到新建的conf目录。

  3. 将配置文件config-example.toml, 存放在应用的主目录下。

  4. config-example.toml可以在java-sdk的源文件以下位置找到:src/test/resources/config-example.toml

  5. 本文的“3. 配置示例” 部分也可以看到config-example.toml的内容。

  6. 修改config-example.toml中节点的IP和端口,与您要连接的节点所匹配。

toml [network] peers=["127.0.0.1:20200", "127.0.0.1:20201"]

  1. 在您的应用中使用该配置文件初始化Java SDK,您就可以开发区块链应用了。

java String configFile = "config-example.toml"; BcosSDK sdk = BcosSDK.build(configFile);

2. 配置解读

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

证书配置

基于安全考虑,java sdk与节点间采用SSL加密通信,目前同时支持非国密SSL连接以及国密SSL连接,[cryptoMaterial]配置SSL连接的证书信息,具体包括如下配置项:

.. note::
    - 大部分场景仅需要配置 `certPath` 配置项即可,其他配置项不需额外配置;
    - SDK证书获取:若参考 `安装 <../../installation.html>`_ 搭建区块链,则参考 `这里 <../../installation.html#id7>`_ 将 `nodes/${ip}/sdk/` 目录下的证书拷贝到 `certPath` 指定的路径;若区块链节点参考 `运维部署工具 <../../enterprise_tools/index.html>`_ 搭建,则参考 `这里 <../../enterprise_tools/tutorial_one_click.html#id15>`_ 将 `generator/meta` 文件夹下的SDK证书拷贝到 `certPath`指定路径;
    - SDK与节点间SSL连接方式,可通过节点配置项 `sm_crypto_channel` 判断,该配置项详细说明请参考 `FISCO BCOS配置文件与配置项说明 <../../manual/configuration.html#id10>`_ .

SDK证书配置示例如下:

[cryptoMaterial]

certPath = "conf"                           # The certification path  

# The following configurations take the certPath by default if commented
# caCert = "conf/ca.crt"                    # CA cert file path
                                            # If connect to the GM node, default CA cert path is ${certPath}/gm/gmca.crt

# sslCert = "conf/sdk.crt"                  # SSL cert file path
                                            # If connect to the GM node, the default SDK cert path is ${certPath}/gm/gmsdk.crt

# sslKey = "conf/sdk.key"                   # SSL key file path
                                            # If connect to the GM node, the default SDK privateKey path is ${certPath}/gm/gmsdk.key

# enSslCert = "conf/gm/gmensdk.crt"         # GM encryption cert file path
                                            # default load the GM SSL encryption cert from ${certPath}/gm/gmensdk.crt

# enSslKey = "conf/gm/gmensdk.key"          # GM ssl cert file path
                                            # default load the GM SSL encryption privateKey from ${certPath}/gm/gmensdk.key

网络连接配置

SDK与FISCO BCOS节点通信,必须配置SDK连接的节点的IPPort[network]配置了java sdk连接的节点信息,具体包括如下配置项:

.. note::
    节点与网络之间的连接信息
    SDK与节点间通过 `ChannelServer` 进行通信,SDK需要连接 `ChannelServer` 的监听端口,该端口可通过节点 `config.ini` 的 `rpc.channel_listen_port` 获取,具体请参考 `这里 <../../manual/configuration.html#rpc>`_

SDK与节点间的网络配置示例如下:

[network]
peers=["127.0.0.1:20200", "127.0.0.1:20201"]    # The peer list to connect

AMOP配置

AMOP支持私有话题的功能,配置文件中提供了AMOP相关配置项于 [[amop]]中。

私有话题订阅配置

AMOP私有话题订阅者需要配置私钥用于进行私有话题认证,具体配置项包括:

AMOP订阅私有话题的配置项示例如下:

# Configure a private topic as a topic subscriber.
[[amop]]
topicName = "PrivateTopic"
privateKey = "conf/amop/consumer_private_key.p12"         # Your private key that used to subscriber verification.
password = "123456"

私有话题消息发布配置

AMOP私有话题认证成功后,消息发布方可向订阅方发送私有话题消息,发布私有话题消息的配置包括:

AMOP发布私有话题消息的配置示例如下:

# Configure a private topic as a topic message sender.
[[amop]]
topicName = "PrivateTopic"
publicKeys = [ "conf/amop/consumer_public_key_1.pem" ]    # Public keys of the nodes that you want to send AMOP message of this topic to.

账户配置

账户配置主要用于设置SDK向节点发交易的账户信息,SDK初始化client时,默认读取[account]配置项加载账户信息,具体如下:

.. note::
    当没有配置 `accountAddress` 和 `accountFilePath` 时,SDK会生成随机的账户向节点交易,生成的账户信息均保存在 `keyStoreDir` 配置项指定的目录下:当SDK连接节点是非国密节点时,生成的临时账户以 `pem` 的格式保存在 `${keyStoreDir}/ecdsa/` 目录下;当SDK连接的节点时国密节点时,生成的临时账户以 `p12` 的格式保存在 `${keyStoreDir}/gm` 目录下

账户配置文件示例如下:

[account]
keyStoreDir = "account"         # The directory to load/store the account file, default is "account"
# accountFilePath = ""          # The account file path (default load from the path specified by the keyStoreDir)
accountFileFormat = "pem"       # The storage format of account file (Default is "pem", "p12" as an option)

# accountAddress = ""           # The transactions sending account address
                                # Default is a randomly generated account
                                # The randomly generated account is stored in the path specified by the keyStoreDir

# password = ""                 # The password used to load the account file

线程池配置

为了方便业务根据机器实际负载调整SDK的处理线程,java sdk将其线程配置项暴露在配置中,[threadPool]是线程池相关配置,具体包括:

.. note::
    大多数场景下,不需要手工配置线程池配置;压测场景下,可将 `maxBlockingQueueSize` 配置大一些。

线程池配置示例如下:

[threadPool]
# channelProcessorThreadSize = "16"         # The size of the thread pool to process channel callback
                                            # Default is the number of cpu cores

# receiptProcessorThreadSize = "16"         # The size of the thread pool to process transaction receipt notification
                                            # Default is the number of cpu cores

maxBlockingQueueSize = "102400"             # The max blocking queue size of the thread pool

3. 配置示例

config-example.toml

[cryptoMaterial]

certPath = "conf"                           # The certification path  

# The following configurations take the certPath by default if commented
# caCert = "conf/ca.crt"                    # CA cert file path
                                            # If connect to the GM node, default CA cert path is ${certPath}/gm/gmca.crt

# sslCert = "conf/sdk.crt"                  # SSL cert file path
                                            # If connect to the GM node, the default SDK cert path is ${certPath}/gm/gmsdk.crt

# sslKey = "conf/sdk.key"                   # SSL key file path
                                            # If connect to the GM node, the default SDK privateKey path is ${certPath}/gm/gmsdk.key

# enSslCert = "conf/gm/gmensdk.crt"         # GM encryption cert file path
                                            # default load the GM SSL encryption cert from ${certPath}/gm/gmensdk.crt

# enSslKey = "conf/gm/gmensdk.key"          # GM ssl cert file path
                                            # default load the GM SSL encryption privateKey from ${certPath}/gm/gmensdk.key

[network]
peers=["127.0.0.1:20200", "127.0.0.1:20201"]    # The peer list to connect

# AMOP configuration
# You can use following two methods to configure as a private topic message sender or subscriber.
# Usually, the public key and private key is generated by subscriber.
# Message sender receive public key from topic subscriber then make configuration.
# But, please do not config as both the message sender and the subscriber of one private topic, or you may send the message to yourself.

# Configure a private topic as a topic message sender.
# [[amop]]
# topicName = "PrivateTopic"
# publicKeys = [ "conf/amop/consumer_public_key_1.pem" ]    # Public keys of the nodes that you want to send AMOP message of this topic to.

# Configure a private topic as a topic subscriber.
# [[amop]]
# topicName = "PrivateTopic"
# privateKey = "conf/amop/consumer_private_key.p12"         # Your private key that used to subscriber verification.
# password = "123456"


[account]
keyStoreDir = "account"         # The directory to load/store the account file, default is "account"
# accountFilePath = ""          # The account file path (default load from the path specified by the keyStoreDir)
accountFileFormat = "pem"       # The storage format of account file (Default is "pem", "p12" as an option)

# accountAddress = ""           # The transactions sending account address
                                # Default is a randomly generated account
                                # The randomly generated account is stored in the path specified by the keyStoreDir

# password = ""                 # The password used to load the account file

[threadPool]
# channelProcessorThreadSize = "16"         # The size of the thread pool to process channel callback
                                            # Default is the number of cpu cores

# receiptProcessorThreadSize = "16"         # The size of the thread pool to process transaction receipt notification
                                            # Default is the number of cpu cores

maxBlockingQueueSize = "102400"             # The max blocking queue size of the thread pool