Solve the problem of slow instantiation of hyperledger fabric java node chaincode and easy timeout

Hyperledger fabric instantiate java chaincode and node chaincode take a long time and sometimes time out. How to solve this problem?

java articles

After my research, I found that the reason why the node instantiating java chaincode is slower is because java generally uses a gradle project or a maven project, and these two projects need to download related dependent jars when instantiating. The main reason is that the execution of this step is relatively slow. Slow, the key to improving the problem lies in how to solve the problem of slow downloading of jars. Changing the project dependency to use local jars can effectively solve this problem.

1 Switch to local jar

Package the jar into the project, so that there is no need to download dependent files at startup, which can effectively speed up. According to my actual measurement, using this method can speed up the instantiation of java chaincode to about 40s, of course, it is also related to the computer configuration

Here will demonstrate how gradle uses local jars. The principle of maven is the same (refer to the original blog link: https://www.cnblogs.com/adderhuang/p/12155834.html ). Change build.gralde to the following,

plugins {
    id 'com.github.johnrengelman.shadow' version '2.0.3'
    id 'java'
}
group 'org.hyperledger.fabric-chaincode-java'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    //chaincode需要的依赖
    compile group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '1.4.1'
//    https://mvnrepository.com/artifact/com.alibaba/fastjson
    compile group: 'com.alibaba', name: 'fastjson', version: '1.2.62'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    //从项目的libs目录加载依赖的jar包
//    compile fileTree(dir:'libs',includes:['*.jar'])
}
shadowJar {
    baseName = 'chaincode'
    version = null
    classifier = null

    manifest {
        attributes 'Main-Class': 'org.hyperledger.fabric.example.SimpleChaincode'
    }
}
//将依赖的jar包导入到项目的libs目录下
task copyJars(type: Copy) {
    from configurations.runtime
    into 'libs' // 目标位置
}

Manually create the libs folder

carried out

gradle build copyJars

Then modify build.gradle as follows:

plugins {
    id 'com.github.johnrengelman.shadow' version '2.0.3'
    id 'java'
}
group 'org.hyperledger.fabric-chaincode-java'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    //chaincode需要的依赖
/*    compile group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '1.4.1'

    compile group: 'com.alibaba', name: 'fastjson', version: '1.2.62'
    testCompile group: 'junit', name: 'junit', version: '4.12'*/
//    从项目的libs目录加载依赖的jar包
    compile fileTree(dir:'libs',includes:['*.jar'])
}
shadowJar {
    baseName = 'chaincode'
    version = null
    classifier = null

    manifest {
        attributes 'Main-Class': 'org.hyperledger.fabric.example.SimpleChaincode'
    }
}
//将依赖的jar包导入到项目的libs目录下
task copyJars(type: Copy) {
    from configurations.runtime
    into 'libs' // 目标位置
}

Can

2 Configure domestic mirrors, such as Ali mirrors

The configuration of domestic maven warehouse mirroring can also achieve acceleration. There are many tutorials, not in the demonstration, you can find it yourself. 

node version

The slow node instantiation is mainly stuck in the npm install step. The solution is similar to java. In the root directory of the node chaincode, execute the npm install command to install node_modules into the chaincode in advance, which can speed up the installation. The disadvantage is that it leads to the chaincode. File is too large

 

Guess you like

Origin blog.csdn.net/qq_27348837/article/details/108052520