我正在使用Log4j2的Socket Appender将日志写入远程服务器。当我从Spring Boot应用程序编写时,它运行良好。在使用docker容器化此应用程序并作为docker容器运行后,它出现以下错误。从Docker容器写入远程服务器是正确的做法吗?
错误
NGINX
在客户端应用中
log4j2.xml(log4j2配置)
kabilesh@kabilesh-Latitude-E6540:~/IdeaProjects/SpringResearch$ docker run -p 8080:8080 springappnew
2019-06-21 10:39:50,796 main ERROR TcpSocketManager (TCP:127.0.0.1:8000) java.net.ConnectException: Connection refused (Connection refused) java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.logging.log4j.core.net.TcpSocketManager$TcpSocketManagerFactory.createSocket(TcpSocketManager.java:423)
at org.apache.logging.log4j.core.net.TcpSocketManager$TcpSocketManagerFactory.createManager(TcpSocketManager.java:404)
at org.apache.logging.log4j.core.net.TcpSocketManager$TcpSocketManagerFactory.createManager(TcpSocketManager.java:387)
at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:112)
at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:114)
at org.apache.logging.log4j.core.net.TcpSocketManager.getSocketManager(TcpSocketManager.java:201)
at org.apache.logging.log4j.core.appender.SocketAppender.createSocketManager(SocketAppender.java:430)
at org.apache.logging.log4j.core.appender.SocketAppender$Builder.build(SocketAppender.java:215)
at org.apache.logging.log4j.core.appender.SocketAppender$Builder.build(SocketAppender.java:190)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:952)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:892)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:884)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:508)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:232)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:244)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:545)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
at org.apache.commons.logging.LogFactory$Log4jLog.<clinit>(LogFactory.java:199)
at org.apache.commons.logging.LogFactory$Log4jDelegate.createLog(LogFactory.java:166)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:109)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:99)
at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:189)
at com.springResearch.SpringRoot.main(SpringRoot.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
2019-06-21 10:39:51,396 Log4j2-TF-1-AsyncLoggerConfig-1 ERROR Unable to write to stream TCP:127.0.0.1:8000 for appender socket: org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:127.0.0.1:8000 socket not available
2019-06-21 10:39:51,398 Log4j2-TF-1-AsyncLoggerConfig-1 ERROR An exception occurred processing Appender socket org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:127.0.0.1:8000 socket not available
at org.apache.logging.log4j.core.net.TcpSocketManager.write(TcpSocketManager.java:213)
at org.apache.logging.log4j.core.appender.OutputStreamManager.write(OutputStreamManager.java:201)
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.writeByteArrayToManager(AbstractOutputStreamAppender.java:186)
at org.apache.logging.log4j.core.appender.SocketAppender.directEncodeEvent(SocketAppender.java:446)
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:170)
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:161)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:448)
at org.apache.logging.log4j.core.async.AsyncLoggerConfig.asyncCallAppenders(AsyncLoggerConfig.java:115)
at org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler.onEvent(AsyncLoggerConfigDisruptor.java:112)
at org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler.onEvent(AsyncLoggerConfigDisruptor.java:98)
at com.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:129)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.6.RELEASE)
2019-06-21 10:39:51,616 Log4j2-TF-1-AsyncLoggerConfig-1 ERROR Unable to write to stream TCP:127.0.0.1:8000 for appender socket: org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:127.0.0.1:8000 socket not available
2019-06-21 10:39:51,617 Log4j2-TF-1-AsyncLoggerConfig-1 ERROR An exception occurred processing Appender socket org.apache.logging.log4j.core.appender.AppenderLoggingException: Error writing to TCP:127.0.0.1:8000 socket not available
at org.apache.logging.log4j.core.net.TcpSocketManager.write(TcpSocketManager.java:213)
at org.apache.logging.log4j.core.appender.OutputStreamManager.write(OutputStreamManager.java:201)
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.writeByteArrayToManager(AbstractOutputStreamAppender.java:186)
at org.apache.logging.log4j.core.appender.SocketAppender.directEncodeEvent(SocketAppender.java:446)
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:170)
at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:161)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:448)
at org.apache.logging.log4j.core.async.AsyncLoggerConfig.asyncCallAppenders(AsyncLoggerConfig.java:115)
at org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler.onEvent(AsyncLoggerConfigDisruptor.java:112)
at org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler.onEvent(AsyncLoggerConfigDisruptor.java:98)
at com.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:129)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
2019-06-21 10:39:53.884 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-06-21 10:39:53.886 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34
2019-06-21 10:39:53.905 INFO 1 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib]
2019-06-21 10:39:54.013 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
Dockerfile
<Configuration status="warn" name="SpringResearch" packages="com.springResearch">
<Appenders>
<Socket name="socket" host="127.0.0.1" port="8000">
<SerializedLayout />
</Socket>
</Appenders>
<Loggers>
<AsyncRoot level="info">
<AppenderRef ref="socket"/>
</AsyncRoot>
</Loggers>
</Configuration>
我将docker容器运行为
FROM java:8
EXPOSE 8080
ADD /target/SpringResearch-1.0-SNAPSHOT.jar SpringResearch-1.0-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "SpringResearch-1.0-SNAPSHOT.jar"]
我使用这个-https://github.com/piruin/log4j2-socket-server作为监听套接字追加程序的应用程序。这正在localhost上监听:端口8000
在日志服务器应用中
log4j2.xml
docker run -p 8080:8080 springappnew
答案 0 :(得分:2)
通常,您不会在与应用程序相同的Docker容器中运行服务器。因此我认为127.0.0.1是不正确的,因为这是本地docker容器。您必须输入正确的名称或ip,可能是docker主机的名称。
答案 1 :(得分:2)
如@jokster所述,应将localhost(127.0.0.1)替换为容器内已知主机的IP。您可以在主机上使用以下命令来获取它:
ip r s 0/0 | awk '{print $3}
然后将IP作为环境变量传递到容器,并在log4j.xml配置文件中使用它。请参阅log4j docs
中的环境查找从Docker v18.03 +开始,似乎可以使用host.docker.internal
主机名
更多信息:
Get Your Docker Host's IP Address from in a Container
How to get the IP address of the docker host from inside a docker container
Connect to a Database Running on Your Docker Host
希望有帮助。