elasticsearch,plugins,config,typesafe,Java,elasticsearch,Plugins,Config,Typesafe" /> elasticsearch,plugins,config,typesafe,Java,elasticsearch,Plugins,Config,Typesafe" />

Java ElasticSearch劫持typesafe配置文件内容

Java ElasticSearch劫持typesafe配置文件内容,java,elasticsearch,plugins,config,typesafe,Java,elasticsearch,Plugins,Config,Typesafe,我正在尝试加载弹性插件的自定义配置,myConfig.conf,如下所示: conf = ConfigFactory.load("myConfig.conf"); 只有以下内容: myInteger: 1234 尝试访问变量myInteger时失败: int bar1 = conf.getInt("myInteger"); 出现错误消息: com.typesafe.config.ConfigException$Missing: system prop

我正在尝试加载弹性插件的自定义配置,
myConfig.conf
,如下所示:

conf = ConfigFactory.load("myConfig.conf");
只有以下内容:

myInteger: 1234
尝试访问变量
myInteger
时失败:

int bar1 = conf.getInt("myInteger");
出现错误消息:

com.typesafe.config.ConfigException$Missing: system properties: No configuration setting found for key 'myInteger'
当我打印出
myConfig.conf
的内容时,它显示了弹性配置的转储,如下所示:

Config(SimpleConfigObject({"es":{"bundled_jdk":"false","distribution":{"flavor":"oss","type":"zip"},"logs":{"base_path":"/Users/me/Downloads/project/build/testclusters/integTest-0/logs","cluster_name":"integTest","node_name":"integTest-0"},"networkaddress":{"cache":{"negative":{"ttl":"10"},"ttl":"60"}},"path":{"conf":"/Users/me/Downloads/project/build/testclusters/integTest-0/config","home":"/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST"}},"file":{"encoding":"UTF-8","separator":"/"},"ftp":{"nonProxyHosts":"local|*.local|169.254/16|*.169.254/16"},"http":{"nonProxyHosts":"local|*.local|169.254/16|*.169.254/16"},"io":{"netty":{"allocator":{"numDirectArenas":"0"},"noKeySetOptimization":"true","noUnsafe":"true","recycler":{"maxCapacityPerThread":"0"}}},"java":{"awt":{"headless":"true"},"class":{"path":"/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-queries-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/hppc-0.8.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jackson-core-2.10.4.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/log4j-api-2.11.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-suggest-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-analyzers-common-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jopt-simple-5.0.2.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-highlighter-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jackson-dataformat-cbor-2.10.4.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-spatial3d-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-secure-sm-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-join-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/log4j-core-2.11.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/java-version-checker-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-cli-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-x-content-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-spatial-extras-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/snakeyaml-1.26.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-queryparser-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-geo-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jackson-dataformat-smile-2.10.4.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-plugin-classloader-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/t-digest-3.2.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-misc-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-sandbox-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-core-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jna-4.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-backward-codecs-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/spatial4j-0.7.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-grouping-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jackson-dataformat-yaml-2.10.4.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-memory-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/elasticsearch-launchers-7.8.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/HdrHistogram-2.1.9.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/lucene-core-8.5.1.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/jts-core-1.15.0.jar:/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST/lib/joda-time-2.10.4.jar","version":"59.0"},"home":"/usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home","io":{"tmpdir":"/Users/me/Downloads/project/build/testclusters/integTest-0/tmp"},"library":{"path":"/Users/me/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:."},"locale":{"providers":"SPI,COMPAT"},"runtime":{"name":"OpenJDK Runtime Environment","version":"15.0.1+9"},"specification":{"name":"Java Platform API Specification","vendor":"Oracle Corporation","version":"15"},"vendor":{"url":{"bug":"https://bugreport.java.com/bugreport/"}},"version":"15.0.1","vm":{"compressedOopsMode":"Zero based","info":"mixed mode, sharing","name":"OpenJDK 64-Bit Server VM","specification":{"name":"Java Virtual Machine Specification","vendor":"Oracle Corporation","version":"15"},"vendor":"Oracle Corporation","version":"15.0.1+9"}},"jdk":{"debug":"release"},"jna":{"loaded":"true","nosys":"true","platform":{"library":{"path":"/usr/lib:/usr/lib"}}},"jnidispatch":{"path":"/Users/me/Downloads/project/build/testclusters/integTest-0/tmp/jna-518060194/jna19175516516881411.tmp"},"line":{"separator":"\n"},"log4j":{"shutdownHookEnabled":"false"},"log4j2":{"disable":{"jmx":"true"}},"os":{"arch":"x86_64","name":"Mac OS X","version":"10.15.5"},"path":{"separator":":"},"socksNonProxyHosts":"local|*.local|169.254/16|*.169.254/16","sun":{"arch":{"data":{"model":"64"}},"boot":{"library":{"path":"/usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home/lib"}},"cpu":{"endian":"little"},"io":{"unicode":{"encoding":"UnicodeBig"}},"java":{"command":"org.elasticsearch.bootstrap.Elasticsearch","launcher":"SUN_STANDARD"},"jnu":{"encoding":"UTF-8"},"management":{"compiler":"HotSpot 64-Bit Tiered Compilers"},"nio":{"ch":{"bugLevel":""}}},"user":{"country":"GB","dir":"/Users/me/Downloads/project/build/testclusters/integTest-0/distro/7.8.0-INTEG_TEST","home":"/Users/me","language":"en","name":"me","timezone":"Europe/London"}}))
它成功地识别出文件存在(如果我伪造文件路径,它将无法工作)

但是没有识别/读取
myConfig.conf
的任何内容

为什么会这样?我怎样才能解决这个问题


编辑

我还应该注意,要在不抱怨的情况下读取配置文件,我必须执行以下操作:

AccessController.doPrivileged((PrivilegedAction<AssignmentConfig>) () -> {
  try {
    return AssignmentConfig.configure();
  } catch (Exception e) {
    e.printStackTrace();
  }
  return null;
});
AccessController.doPrivileged((PrivilegedAction)(->{
试一试{
返回AssignmentConfig.configure();
}捕获(例外e){
e、 printStackTrace();
}
返回null;
});

在elasticsearch插件中使用外部配置文件是个坏主意。ES提供了扩展elasticsearch配置的机制。所有自定义配置都应放入elasticsearch.yml中,并在插件中注册自定义设置,如下所示:

public class MyESPlugin extends Plugin implements ... {

@Override
public List<Setting<?>> getSettings() {
    return Arrays.asList(new Setting<>("setting1", "", Function.identity(), Setting.Property.NodeScope),
        new Setting<>("setting2", "", Function.identity(), Setting.Property.NodeScope), ...);
}
但是请注意,必须在启动节点之前安装插件,否则节点将无法启动,因为它无法识别自定义设置

setting1: ...
setting2: ...