客户端id是计算机的名吗,Spring Boot - 获取Spring.Kafka客户端Id的application.properties中的主机名(Spring Boot - Getting the...

Spring Boot - 获取Spring.Kafka客户端Id的application.properties中的主机名(Spring Boot - Getting the hostname in application.properties for Spring-Kafka client Id)

我正在使用Spring-Kafka和Boot开发一个项目,并希望在application.properties中获取属性spring.kafka.consumer.client-Id的主机名,以便在服务器端日志中区分每个使用者应该有问题吗?

有没有办法可以做到这一点? 我检查了spring引导参考指南和java.lang.System类,但找不到富有成效的指针。

I am working on a project with Spring-Kafka and Boot and am wanting to get the hostname in the application.properties for the property spring.kafka.consumer.client-Id so that each of my consumers can be distinguished in the server side logs should there be an issue.

Is there a way i could do that? I check on the spring boot reference guide and java.lang.System class but could not find fruitful pointers.

原文:https://stackoverflow.com/questions/43191948

更新时间:2019-11-15 13:30

最满意答案

这是一种方法 - 在您的配置中添加EnvironmentAware bean ...

@SpringBootApplication

public class So43191948Application implements EnvironmentAware {

public static void main(String[] args) throws Exception {

ConfigurableApplicationContext context = SpringApplication.run(So43191948Application.class, args);

@SuppressWarnings("unchecked")

KafkaTemplate template = context.getBean(KafkaTemplate.class);

template.send("so43191948", "foo");

Thread.sleep(10000);

context.close();

}

@KafkaListener(topics = "so43191948")

public void foo(String in) {

System.out.println(in);

}

@Override

public void setEnvironment(Environment environment) {

Properties props = new Properties();

try {

props.setProperty("spring.kafka.consumer.client-id", InetAddress.getLocalHost().getHostName() + ".client");

PropertiesPropertySource propertySource = new PropertiesPropertySource("myProps", props);

if (environment instanceof StandardEnvironment) {

((StandardEnvironment) environment).getPropertySources().addFirst(propertySource);

}

}

catch (UnknownHostException e) {

e.printStackTrace();

}

}

}

在这种情况下,我在启动应用程序本身中完成了它,但它可以在任何bean中完成。

2017-04-03 16:12:32.646 INFO 64879 --- [ main] o.a.k.clients.consumer.ConsumerConfig

: ConsumerConfig values:

auto.commit.interval.ms = 5000

auto.offset.reset = earliest

bootstrap.servers = [localhost:9092]

check.crcs = true

client.id = myhost.client

...

application.properties:

spring.kafka.consumer.client-id=foo

spring.kafka.consumer.group-id=myGroup

spring.kafka.consumer.auto-offset-reset=earliest

如您所见, client-id被覆盖。

Here's one way - add an EnvironmentAware bean to your config...

@SpringBootApplication

public class So43191948Application implements EnvironmentAware {

public static void main(String[] args) throws Exception {

ConfigurableApplicationContext context = SpringApplication.run(So43191948Application.class, args);

@SuppressWarnings("unchecked")

KafkaTemplate template = context.getBean(KafkaTemplate.class);

template.send("so43191948", "foo");

Thread.sleep(10000);

context.close();

}

@KafkaListener(topics = "so43191948")

public void foo(String in) {

System.out.println(in);

}

@Override

public void setEnvironment(Environment environment) {

Properties props = new Properties();

try {

props.setProperty("spring.kafka.consumer.client-id", InetAddress.getLocalHost().getHostName() + ".client");

PropertiesPropertySource propertySource = new PropertiesPropertySource("myProps", props);

if (environment instanceof StandardEnvironment) {

((StandardEnvironment) environment).getPropertySources().addFirst(propertySource);

}

}

catch (UnknownHostException e) {

e.printStackTrace();

}

}

}

In this case, I did it in the boot app itself but it can be done in any bean.

2017-04-03 16:12:32.646 INFO 64879 --- [ main] o.a.k.clients.consumer.ConsumerConfig

: ConsumerConfig values:

auto.commit.interval.ms = 5000

auto.offset.reset = earliest

bootstrap.servers = [localhost:9092]

check.crcs = true

client.id = myhost.client

...

application.properties:

spring.kafka.consumer.client-id=foo

spring.kafka.consumer.group-id=myGroup

spring.kafka.consumer.auto-offset-reset=earliest

As you can see, the client-id is overridden.

相关问答

使用-D设置系统属性。 Spring Boot可以使用System属性中的配置,因此,一般来说,它都可以工作。 但是,如果spring-boot:run正在为您的应用程序分配一个单独的JVM,则它将无法工作,因为将在错误的JVM上设置System属性。 由于它不起作用,我猜这就是发生的事情。 您可以使用-Drun.arguments将参数传递给正在运行的应用程序,而不-Drun.arguments是否在分叉的JVM中运行。 参数应该是逗号分隔的列表,每个列表都以--为前缀。 例如,要设置my.ur

...

这是一种方法 - 在您的配置中添加EnvironmentAware bean ... @SpringBootApplication

public class So43191948Application implements EnvironmentAware {

public static void main(String[] args) throws Exception {

ConfigurableApplicationContext context = SpringAp

...

不知道我在想什么,可以使用JNDI来设置application.properties文件的位置,/或覆盖任何应用程序变量。 例如 spring.config.location = file:/some/location/properties/

my.env.myCustomStringField = foobar

不要打扰java:comp/env/ ,Spring会让你远离它。 Not sure what I was thinking, you can use JNDI to set the

...

有许多方法可以覆盖属性键而不会禁用整个外部化配置功能; 这实际上就是目标。 您可以在此处查看属性的顺序 。 例如,您可以将该外部属性文件添加到打包的JAR旁边的config文件夹中,甚至可以自己配置文件位置 。 现在,如果您真的想要禁用所有这些(并且Boot团队强烈建议不要这样做),您可以注册自己的EnvironmentPostProcessor ( 请参阅此处 )并从MutablePropertySources删除PropertySources ,您可以使用configurableEnviron

...

要在spring boot中从application.properties获取值,我们需要指定一些注释。 application.properties必须位于src / main / resources路径中 class必须包含@RestController注释 @Value(“$ {name}”)私有字符串名称; Ah, one important difference: @RestController delivers the response directly, meaning: Inste

...

如果你想以优雅的方式做到这一点,我建议你使用Spring Cloud配置 。 Spring的这项服务可以帮助您集中管理不同服务的任何配置。 了解更多信息: https://spring.io/guides/gs/centralized-configuration/ http://www.baeldung.com/spring-cloud-configuration http://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.4.

...

删除PersistenceConfiguration类不是必需的。 Spring Boot会自动配置您和flyway使用的数据源。 HikariCP现在是Spring Boot 2中的默认池实现。 还要除去spring.datasource.url之外的所有与application.properties相关的数据源属性。 Remove PersistenceConfiguration class it’s not required. Spring Boot autoconfigures the d

...

解决了添加到application.properties: spring.mail.properties.mail.smtp.from=teste@teste.com

Solved adding to application.properties: spring.mail.properties.mail.smtp.from=teste@teste.com

测试代码有几个错误; 首先,你的依赖关系是不好的 - Spock 1.0不支持@SpringBootTest注解,所以没有上下文将被初始化,并且不会进行后期处理,因此空指针异常:什么都不会自动装配。 在Spock 1.1中添加了对该注释的支持,Spock 1.1仍然是候选版本,因此您必须使用它: dependencies {

compile('org.springframework.boot:spring-boot-starter-data-jpa')

compile('org.s

...

试试这个-Dspring.config.location=file:/etc/test/application.properties (我用-Dspring.config.location=file:c:\application.properties在windows上测试过它) Try this -Dspring.config.location=file:/etc/test/application.properties (I've tested it on windows with -Dsprin

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值