use of com.jd.blockchain.tools.initializer.LedgerBindingConfig in project jdchain-core by blockchain-jd-com.
the class LedgerBindingConfigTest method assertLedgerBindingConfig.
// @Test
// public void testLedgerBindingRegister() throws IOException {
// LedgerManager ledgerManager = new LedgerManager();
// ClassPathResource ledgerBindingConfigFile = new ClassPathResource("ledger-binding-1.conf");
// InputStream in = ledgerBindingConfigFile.getInputStream();
// Exception ex = null;
// try {
// LedgerBindingConfig conf = LedgerBindingConfig.resolve(in);
// // assertLedgerBindingConfig(conf);
//
// HashDigest[] existingLedgerHashs = ledgerManager.getLedgerHashs();
// for (HashDigest lh : existingLedgerHashs) {
// ledgerManager.unregister(lh);
// }
// HashDigest[] ledgerHashs = conf.getLedgerHashs();
// for (HashDigest ledgerHash : ledgerHashs) {
// // setConfig(conf,ledgerHash);
// LedgerBindingConfig.BindingConfig bindingConfig = conf.getLedger(ledgerHash);
// }
// } catch (Exception e) {
// ex =e;
// } finally {
// in.close();
// }
//
// assertNull(ex);
// }
/**
* 判断指定的对象跟测试模板是否一致;
*
* @param conf
*/
private void assertLedgerBindingConfig(LedgerBindingConfig conf) {
String[] expectedHashs = { "j5ptBmn67B2p3yki3ji1j2ZMjnJhrUvP4kFpGmcXgvrhmk", "j5kLUENMvcUooZjKfz2bEYU6zoK9DAqbdDDU8aZEZFR4qf" };
HashDigest[] hashs = conf.getLedgerHashs();
for (int i = 0; i < hashs.length; i++) {
assertEquals(expectedHashs[i], hashs[i].toBase58());
}
BindingConfig bindingConf_0 = conf.getLedger(hashs[0]);
assertEquals("1", bindingConf_0.getParticipant().getAddress());
assertEquals("keys/jd-com.priv", bindingConf_0.getParticipant().getPkPath());
assertEquals("AdSXsf5QJpy", bindingConf_0.getParticipant().getPk());
assertNull(bindingConf_0.getParticipant().getPassword());
assertEquals("redis://ip:port/1", bindingConf_0.getDbConnection().getUri());
assertEquals("kksfweffj", bindingConf_0.getDbConnection().getPassword());
BindingConfig bindingConf_1 = conf.getLedger(hashs[1]);
assertEquals("2", bindingConf_1.getParticipant().getAddress());
assertEquals("keys/jd-com-1.priv", bindingConf_1.getParticipant().getPkPath());
assertNull(bindingConf_1.getParticipant().getPk());
assertEquals("kksafe", bindingConf_1.getParticipant().getPassword());
assertEquals("redis://ip:port/2", bindingConf_1.getDbConnection().getUri());
assertNull(bindingConf_1.getDbConnection().getPassword());
}
use of com.jd.blockchain.tools.initializer.LedgerBindingConfig in project jdchain-core by blockchain-jd-com.
the class PeerServerBooter method startServer.
/**
* 启动服务;
*
* @param ledgerBindingConfig 账本绑定配置;
* @param hostAddress 服务地址;如果为空,则采用默认配置;
* @param port 端口地址;如果小于等于 0 ,则采用默认配置;
* @return
*/
private static ConfigurableApplicationContext startServer(LedgerBindingConfig ledgerBindingConfig, String hostAddress, int port, String springConfigLocation, Object... externalBeans) {
List<String> argList = new ArrayList<String>();
String logConfig = System.getProperty(LOG_CONFIG_FILE);
if (!StringUtils.isEmpty(logConfig)) {
argList.add(String.format("--logging.config=%s", logConfig));
}
String argServerAddress = String.format("--server.address=%s", "0.0.0.0");
argList.add(argServerAddress);
if (port > 0) {
String argServerPort = String.format("--server.port=%s", port);
argList.add(argServerPort);
} else {
port = 8080;
}
if (springConfigLocation != null) {
argList.add(String.format("--spring.config.location=%s", springConfigLocation));
}
String[] args = argList.toArray(new String[argList.size()]);
SpringApplication app = new SpringApplication(PeerConfiguration.class);
if (externalBeans != null && externalBeans.length > 0) {
app.addInitializers((ApplicationContextInitializer<ConfigurableApplicationContext>) applicationContext -> {
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
for (Object bean : externalBeans) {
if (bean != null) {
beanFactory.registerSingleton(bean.toString(), bean);
}
}
});
}
// 启动 web 服务;
ConfigurableApplicationContext ctx = app.run(args);
RuntimeConstant.setMonitorProperties(port, Boolean.valueOf(ctx.getEnvironment().getProperty("server.ssl.enabled")));
// 配置文件为空,则说明目前没有账本,不需要配置账本相关信息
if (ledgerBindingConfig != null) {
// 建立共识网络;
Map<String, LedgerBindingConfigAware> bindingConfigAwares = ctx.getBeansOfType(LedgerBindingConfigAware.class);
for (LedgerBindingConfigAware aware : bindingConfigAwares.values()) {
aware.setConfig(ledgerBindingConfig);
}
ConsensusManage consensusManage = ctx.getBean(ConsensusManage.class);
consensusManage.runAllRealms();
}
// 释放定时任务许可
LedgerLoadTimer loadTimerBean = ctx.getBean(LedgerLoadTimer.class);
loadTimerBean.release();
return ctx;
}
use of com.jd.blockchain.tools.initializer.LedgerBindingConfig in project jdchain-core by blockchain-jd-com.
the class LedgerBindingConfigTest method testResolveAndStore.
@Test
public void testResolveAndStore() throws IOException {
ClassPathResource ledgerBindingConfigFile = new ClassPathResource("ledger-binding.conf");
InputStream in = ledgerBindingConfigFile.getInputStream();
try {
LedgerBindingConfig conf = LedgerBindingConfig.resolve(in);
assertLedgerBindingConfig(conf);
conf.store(System.out);
ByteArrayOutputStream out = new ByteArrayOutputStream();
conf.store(out);
ByteArrayInputStream newIn = new ByteArrayInputStream(out.toByteArray());
LedgerBindingConfig newConf = LedgerBindingConfig.resolve(newIn);
assertLedgerBindingConfig(newConf);
} finally {
in.close();
}
}
use of com.jd.blockchain.tools.initializer.LedgerBindingConfig in project jdchain-core by blockchain-jd-com.
the class PeerServerBooter method handle.
public static void handle(String[] args) {
LedgerBindingConfig ledgerBindingConfig = null;
ArgumentSet arguments = ArgumentSet.resolve(args, ArgumentSet.setting().prefix(LEDGERBIND_ARG, HOST_ARG, PORT_ARG, SPRING_CF_LOCATION));
try {
ArgumentSet.ArgEntry argLedgerBindConf = arguments.getArg(LEDGERBIND_ARG);
ledgerBindConfigFile = argLedgerBindConf == null ? null : argLedgerBindConf.getValue();
if (ledgerBindConfigFile == null) {
LOGGER.info("Load build-in default configuration ...");
ClassPathResource configResource = new ClassPathResource(LEDGER_BIND_CONFIG_NAME);
if (configResource.exists()) {
try (InputStream in = configResource.getInputStream()) {
ledgerBindingConfig = LedgerBindingConfig.resolve(in);
}
}
} else {
LOGGER.info("Load configuration,ledgerBindConfigFile position=" + ledgerBindConfigFile);
File file = new File(ledgerBindConfigFile);
if (file.exists()) {
try {
ledgerBindingConfig = LedgerBindingConfig.resolve(file);
} catch (LedgerBindingConfigException e) {
LOGGER.info("Load ledgerBindConfigFile content is empty !!!");
}
}
}
String host = null;
ArgumentSet.ArgEntry hostArg = arguments.getArg(HOST_ARG);
if (hostArg != null) {
host = hostArg.getValue();
}
int port = 0;
ArgumentSet.ArgEntry portArg = arguments.getArg(PORT_ARG);
if (portArg != null) {
try {
port = Integer.parseInt(portArg.getValue());
} catch (NumberFormatException e) {
// ignore NumberFormatException of port argument;
}
}
// spring config location;
String springConfigLocation = null;
ArgumentSet.ArgEntry spConfigLocation = arguments.getArg(SPRING_CF_LOCATION);
if (spConfigLocation != null) {
springConfigLocation = spConfigLocation.getValue();
}
PeerServerBooter booter = new PeerServerBooter(ledgerBindingConfig, host, port, springConfigLocation);
LOGGER.debug("PeerServerBooter's urls=" + Arrays.toString(((URLClassLoader) booter.getClass().getClassLoader()).getURLs()));
booter.start();
} catch (Exception e) {
LOGGER.error("Peer start error", e);
}
}
use of com.jd.blockchain.tools.initializer.LedgerBindingConfig in project jdchain-core by blockchain-jd-com.
the class LedgerLoadTimer method ledgerLoad.
// 每5秒执行一次
@Scheduled(cron = "*/5 * * * * * ")
public void ledgerLoad() {
lock.lock();
try {
LOGGER.debug("--- Ledger loader tasks start... ");
boolean acquire = false;
try {
/**
* 5秒内获取许可
*/
acquire = loadSemaphore.tryAcquire(5, TimeUnit.SECONDS);
if (acquire) {
LedgerBindingConfig ledgerBindingConfig = loadLedgerBindingConfig();
if (ledgerBindingConfig == null) {
// print debug
LOGGER.warn("Can not load any ledgerBindingConfigs !!!");
return;
}
HashDigest[] totalLedgerHashs = ledgerBindingConfig.getLedgerHashs();
Set<HashDigest> existedHashSet = existedHashSet();
Set<HashDigest> newAddHashs = new HashSet<>();
for (HashDigest ledgerHash : totalLedgerHashs) {
if (!existedHashSet.contains(ledgerHash)) {
newAddHashs.add(ledgerHash);
}
}
if (!newAddHashs.isEmpty()) {
// 由线程单独执行
ledgerLoadExecutor.execute(new LedgerLoadRunnable(newAddHashs, ledgerBindingConfig));
}
} else {
LOGGER.warn("--- Can not get semaphore of load ledger !!!");
}
} catch (Exception e) {
LOGGER.error("--- Ledger loader execute error !!!", e);
} finally {
if (acquire) {
// 获取到许可的情况下释放,以便于后续线程处理
release();
}
}
} finally {
lock.unlock();
}
}
Aggregations