java嵌入的kafka appender不使用log4j2 xml

juzqafwq  于 2021-06-06  发布在  Kafka
关注(0)|答案(0)|浏览(285)

我想使用log4j2xml为kafka appender编写单元测试。我正在使用嵌入式kafka,但它无法初始化${spring.embedded.kafka.brokers}此属性。
我得到这个错误

java.lang.ExceptionInInitializerError
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:273)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:96)
    ... 17 more
Caused by: org.apache.kafka.common.config.ConfigException: Invalid url in bootstrap.servers: ${spring.embedded.kafka.brokers}
    at org.apache.kafka.clients.ClientUtils.parseAndValidateAddresses(ClientUtils.java:51)
    at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:406)
    ... 38 more

log4j2.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
    <Properties>
        <Property name="bootstrapServers">${bundle:application:kafka.bootstrap-servers}</Property>
        <Property name="securityProtocol">${bundle:application:kafka.security.protocol}</Property>
    </Properties>
    <Appenders>

        <Kafka name="kafkaAppender" topic="ERROR_TOPIC_DEV">
            <Filters>
                <ThresholdFilter level="ERROR" onMatch="ACCEPT" />
            </Filters>
            <JSONLayout />

            <Property name="bootstrap.servers">${bootstrapServers}</Property>
            <Property name="security.protocol">${securityProtocol}</Property>  
        </Kafka>
        <Console name="consoleAppender" target="SYSTEM_OUT">
            <Filters>
                <ThresholdFilter level="INFO" onMatch="ACCEPT" />
            </Filters>
           <JSONLayout/>
        </Console>

    </Appenders>
    <Loggers>
        <Root level="Info">
            <AppenderRef ref="kafkaAppender"/>
            <AppenderRef ref="consoleAppender"/>
        </Root>
        <Logger name="org.apache.kafka" level="Info" />
    </Loggers>
</Configuration>

应用程序属性

kafka.bootstrap-servers=${spring.embedded.kafka.brokers}
kafka.security.protocol=PLAINTEXT

在上面我试图从application.properties获取bootstrapserver,但是它无法初始化${spring.embedded.kafka.brokers}这个属性。另一方面,我得到securityprotocol的值,因为它是字符串。先谢谢你。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题