如何在 yml 中获取我的配置值 - 使用 dropwizard(微服务)Jersey D.I @Injection?
how to get my configuration values in yml - using dropwizard (microservice) Jersey D.I @Injection?
这是我的代码片段。
这是我的 yml 文件:
productionServer:
host: production-server.amazonaws.com
publicIp: xx.xx.xx.xx
privateIp: xx.xx.xx.xx
userName: xx.xx.xx.xx
password: xx.xx.xx.xx
remoteFilePath: fake/path/
fileName: test.txt
privateKey: private-public-key.ppk
server:
applicationConnectors:
- type: http
port: 8080
- type: https
port: 8443
keyStorePath: key.keystore
keyStorePassword: password
validateCerts: false
adminConnectors:
- type: http
port: 8081
- type: https
port: 8444
keyStorePath: key.keystore
keyStorePassword: password
validateCerts: false
我的配置class:
import io.dropwizard.Configuration;
public class MyConfiguration extends Configuration{
@NotNull
@JsonProperty
private ProductionServer productionServer;
// getters
public class ProdctionServer{
@NotEmpty
@JsonProperty
private host;
@NotEmpty
@JsonProperty
private publicIp;
// getters
申请class:
import io.dropwizard.Application;
public class MyApplication extends Application<MyConfiguration> {
public static void main(String[] args) throws Exception{
new MysApplication().run(args);
}
@Override
public String getName(){ return "micro-service"; }
@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap){}
@Override
public void run(MyConfiguration conf, Environment environment ){
final MyResource myResource = new MyResource();
// health check
// environment.healthChecks().register("template",healthCheck);
System.out.println( "==> " + conf );
System.out.println( "==> " + conf.getProductionServer() );
// register
environment.jersey().register( MyResource );
当 运行 这个应用程序时:
我收到如下日志:
==> MyConfiguration{server=DefaultServerFactory{applicationConnectors=[io.dropwizard.jetty.HttpConnectorFactory@623e088f, io.dropwizard.jetty.HttpsConnectorFactory@39fcbef6], adminConnectors=[io.dropwizard.jetty.HttpConnectorFactory@34f22f9d, io.dropwizard.jetty.HttpsConnectorFactory@77d67cf3], adminMaxThreads=64, adminMinThreads=1, applicationContextPath=/, adminContextPath=/}, logging=DefaultLoggingFactory{level=INFO, loggers={}, appenders=[io.dropwizard.logging.ConsoleAppenderFactory@663411de]}}
==> com.mycompany.myproject.model.ProductionServer@5b04476e
表示成功获取到我的yaml的值。
但我的问题是在 D.I 或 MyConfiguration class 的依赖注入期间。尽管对象 MyConfiguration 在我的服务中似乎不为空,但我无法获得 ProductionServer 的值。
这是我的依赖绑定代码片段 MyService.class 和 MyConfiguration.class
DependencyBinder.class
导入org.glassfish.hk2.utilities.binding.AbstractBinder;
public class DependencyBinder 扩展了 AbstractBinder {
@Override
protected void configure() {
bind(MyConfiguration.class).to(MyConfiguration.class);
bind(MyService.class).to(MyService.class);
}
MyService.class
public class MyService {
@Inject
MyConfiguration conf;
public void invoke(){
System.out.println( "=============================== " );
System.out.println( "==> " + conf );
System.out.println("==> " + conf.getProductionServer() );
}
并且在调用方法 invoke() 期间...
我得到如下记录:
===============================
==> MyConfiguration{server=DefaultServerFactory{applicationConnectors=[io.dropwizard.jetty.HttpConnectorFactory@34e82c4d], adminConnectors=[io.dropwizard.jetty.HttpConnectorFactory@19b70fbd], adminMaxThreads=64, adminMinThreads=1, applicationContextPath=/, adminContextPath=/}, logging=DefaultLoggingFactory{level=INFO, loggers={}, appenders=[io.dropwizard.logging.ConsoleAppenderFactory@543f81c9]}}
==> null
现在我的问题出在 D.I 或 MyService.class 中 MyConfiguration class 的依赖注入期间。尽管对象 MyConfiguration 在我的服务中似乎不为空,但我无法获得 ProductionServer 的值。
请给我一些解决方案?谢谢。
问题是,使用此配置
bind(MyConfiguration.class).to(MyConfiguration.class);
HK2 将创建 MyConfiguration
的新实例。它不会是 DW 填充的同一个实例。但是,您可以 做的是使用 DW 创建的实例,只需在您的 HK2 配置
中绑定 相同的实例
public class MyApplication extends Application<MyConfiguration> {
@Override
public void run(final MyConfiguration config, Environment env) {
env.jersey().register(new AbstractBinder() {
@Override
protected void configure() {
bind(config).to(MyConfiguration.class);
}
});
}
}
这是我的代码片段。
这是我的 yml 文件:
productionServer:
host: production-server.amazonaws.com
publicIp: xx.xx.xx.xx
privateIp: xx.xx.xx.xx
userName: xx.xx.xx.xx
password: xx.xx.xx.xx
remoteFilePath: fake/path/
fileName: test.txt
privateKey: private-public-key.ppk
server:
applicationConnectors:
- type: http
port: 8080
- type: https
port: 8443
keyStorePath: key.keystore
keyStorePassword: password
validateCerts: false
adminConnectors:
- type: http
port: 8081
- type: https
port: 8444
keyStorePath: key.keystore
keyStorePassword: password
validateCerts: false
我的配置class:
import io.dropwizard.Configuration;
public class MyConfiguration extends Configuration{
@NotNull
@JsonProperty
private ProductionServer productionServer;
// getters
public class ProdctionServer{
@NotEmpty
@JsonProperty
private host;
@NotEmpty
@JsonProperty
private publicIp;
// getters
申请class:
import io.dropwizard.Application;
public class MyApplication extends Application<MyConfiguration> {
public static void main(String[] args) throws Exception{
new MysApplication().run(args);
}
@Override
public String getName(){ return "micro-service"; }
@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap){}
@Override
public void run(MyConfiguration conf, Environment environment ){
final MyResource myResource = new MyResource();
// health check
// environment.healthChecks().register("template",healthCheck);
System.out.println( "==> " + conf );
System.out.println( "==> " + conf.getProductionServer() );
// register
environment.jersey().register( MyResource );
当 运行 这个应用程序时:
我收到如下日志:
==> MyConfiguration{server=DefaultServerFactory{applicationConnectors=[io.dropwizard.jetty.HttpConnectorFactory@623e088f, io.dropwizard.jetty.HttpsConnectorFactory@39fcbef6], adminConnectors=[io.dropwizard.jetty.HttpConnectorFactory@34f22f9d, io.dropwizard.jetty.HttpsConnectorFactory@77d67cf3], adminMaxThreads=64, adminMinThreads=1, applicationContextPath=/, adminContextPath=/}, logging=DefaultLoggingFactory{level=INFO, loggers={}, appenders=[io.dropwizard.logging.ConsoleAppenderFactory@663411de]}}
==> com.mycompany.myproject.model.ProductionServer@5b04476e
表示成功获取到我的yaml的值。 但我的问题是在 D.I 或 MyConfiguration class 的依赖注入期间。尽管对象 MyConfiguration 在我的服务中似乎不为空,但我无法获得 ProductionServer 的值。
这是我的依赖绑定代码片段 MyService.class 和 MyConfiguration.class
DependencyBinder.class
导入org.glassfish.hk2.utilities.binding.AbstractBinder;
public class DependencyBinder 扩展了 AbstractBinder {
@Override
protected void configure() {
bind(MyConfiguration.class).to(MyConfiguration.class);
bind(MyService.class).to(MyService.class);
}
MyService.class
public class MyService {
@Inject
MyConfiguration conf;
public void invoke(){
System.out.println( "=============================== " );
System.out.println( "==> " + conf );
System.out.println("==> " + conf.getProductionServer() );
}
并且在调用方法 invoke() 期间... 我得到如下记录:
===============================
==> MyConfiguration{server=DefaultServerFactory{applicationConnectors=[io.dropwizard.jetty.HttpConnectorFactory@34e82c4d], adminConnectors=[io.dropwizard.jetty.HttpConnectorFactory@19b70fbd], adminMaxThreads=64, adminMinThreads=1, applicationContextPath=/, adminContextPath=/}, logging=DefaultLoggingFactory{level=INFO, loggers={}, appenders=[io.dropwizard.logging.ConsoleAppenderFactory@543f81c9]}}
==> null
现在我的问题出在 D.I 或 MyService.class 中 MyConfiguration class 的依赖注入期间。尽管对象 MyConfiguration 在我的服务中似乎不为空,但我无法获得 ProductionServer 的值。 请给我一些解决方案?谢谢。
问题是,使用此配置
bind(MyConfiguration.class).to(MyConfiguration.class);
HK2 将创建 MyConfiguration
的新实例。它不会是 DW 填充的同一个实例。但是,您可以 做的是使用 DW 创建的实例,只需在您的 HK2 配置
public class MyApplication extends Application<MyConfiguration> {
@Override
public void run(final MyConfiguration config, Environment env) {
env.jersey().register(new AbstractBinder() {
@Override
protected void configure() {
bind(config).to(MyConfiguration.class);
}
});
}
}