1 package util; 2 import org.web3j.protocol.geth.Geth; 3 import org.web3j.protocol.http.HttpService; 4 5 public class GethClientUtil { 6 7 private volatile static Geth geth; 8 9 public static Geth getClient() {10 if (geth == null) {11 synchronized (GethClientUtil.class) {12 if (geth == null) {13 try {14 geth = Geth.build(new HttpService(PropUtil.getProps().getProperty("RPC_ADDR")));15 } catch (Exception e) {16 e.printStackTrace();17 }18 }19 }20 }21 return geth;22 }23 24 }
配置文件工具类
1 package util; 2 3 import java.io.InputStream; 4 import java.util.Properties; 5 6 public class PropUtil { 7 8 private static Properties props = null; 9 10 static {11 readProperties("ether.properties");12 }13 14 private static void readProperties(String fileName) { 15 try { 16 props = new Properties();17 InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);18 props.load(inputStream);19 inputStream.close();20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 } 24 25 public static Properties getProps() {26 return props;27 }28 29 }
配置文件
1 #调用RPC地址2 RPC_ADDR=http://127.0.0.1:85453 #矿工费参数4 GAS_PRICE=220000000005 GAS_LIMIT=43000006 #私钥文件存储路径7 KEYSTORE_PATH=E:/Project/TestGeth/keystore