Springboot with Docker: environment variable to override RabbitMQ host IP property in spring boot's application.properties is not working

huangapple go评论113阅读模式
英文:

Springboot with Docker: environment variable to override RabbitMQ host IP property in spring boot's application.properties is not working

问题

代码部分不要翻译,只返回翻译好的内容:

我正在开发一个Spring Boot应用程序,在其中我需要使用RabbitMQ作为消息代理。与其在本地使用RabbitMQ,我使用以下命令从Docker Hub使用Docker镜像:

docker run -d --name rabbit-name-management -p 15672:15672 -p 5672:5672 -p 15671:15671 -p 5671:5671 -p 4369:4369 rabbitmq:3-management

这将拉取镜像并成功运行为Docker容器。

现在在我的Spring Boot应用程序中,我使用一个application.properties文件来连接到RabbitMQ,其内容如下所示:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=*****
spring.rabbitmq.password=********

我的Spring Boot Docker文件

FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY encryptionKey.jks encryptionKey.jks
COPY UnlimitedJCEPolicyJDK8/* /usr/lib/jvm/java-1.8-openjdk/jre/lib/security/
COPY target/ConfigServer-0.0.1-SNAPSHOT.jar ConfigServer.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","ConfigServer.jar"]

我使用命令docker inspect <RABBITMQ_CONTAINER_ID>来查找RabbitMQ运行的机器的IP地址。为了使其与RabbitMQ通信,我使用环境变量将本地主机替换为容器中运行RabbitMQ的实际IP地址。此操作的Docker命令如下所示:

docker run -p 8012:8012 -e "spring.rabbitmq.host=http://172.17.0.3" config-server

但是,此命令执行失败,并记录以下错误:

2020-08-20 06:10:31.077 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed
...
java.lang.IllegalArgumentException: Address http://172.17.0.3:5672 seems to contain an unquoted IPv6 address. Make sure you quote IPv6 addresses like so: [2001:db8:85a3:8d3:1319:8a2e:370:7348]
...

我不知道这个“未引用的IPv6地址”是什么,为什么它在这里报错,以及我应该在这里做什么来消除此错误。

英文:

I am working on spring boot application where I have to use RabbitMQ as a message broker. Instead of using RabbitMQ on my localhost, I am using docker image from docker hub using below command

docker run -d --name rabbit-name-management -p 15672:15672 -p 5672:5672 -p 15671:15671 -p 5671:5671 -p 4369:4369 rabbitmq:3-management

So it pulls the image and running successfully as a docker container.

Now in my spring boot application, I use an application.properties file in order to connect to RabbitMQ and which looks like as shown below:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=*****
spring.rabbitmq.password=********

My spring boot docker file

FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY encryptionKey.jks encryptionKey.jks
COPY UnlimitedJCEPolicyJDK8/* /usr/lib/jvm/java-1.8-openjdk/jre/lib/security/
COPY target/ConfigServer-0.0.1-SNAPSHOT.jar ConfigServer.jar
ENTRYPOINT [&quot;java&quot;,&quot;-Djava.security.egd=file:/dev/./urandom&quot;,&quot;-jar&quot;,&quot;ConfigServer.jar&quot;]

I used command docker inspect &lt;RABBITMQ_CONTAINER_ID&gt; to find out the IP address of the machine where RabbitMQ is running. And in order to make it communicate with RabbitMQ I am using environment variable to override localhost with the actual IP address of the container where RabbitMQ is running. And the docker command for this is as below

docker run -p 8012:8012 -e &quot;spring.rabbitmq.host=http://172.17.0.3&quot; config-server

But this command is getting failed and it logs the error as below

2020-08-20 06:10:31.077 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed
 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;rabbitConnectionFactory&#39; defined in class path resource [org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration$RabbitConnectionFactoryCreator.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.amqp.rabbit.connection.CachingConnectionFactory]: Factory method &#39;rabbitConnectionFactory&#39; threw exception; nested exception is java.lang.IllegalArgumentException: Address http://172.17.0.3:5672 seems to contain an unquoted IPv6 address. Make sure you quote IPv6 addresses like so: [2001:db8:85a3:8d3:1319:8a2e:370:7348]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.3.RELEASE.jar!/:2.3.3.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.3.RELEASE.jar!/:2.3.3.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.3.RELEASE.jar!/:2.3.3.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.3.RELEASE.jar!/:2.3.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.3.RELEASE.jar!/:2.3.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.3.RELEASE.jar!/:2.3.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.3.RELEASE.jar!/:2.3.3.RELEASE]
        at com.appsdeveloperblog.photoapp.api.PhotoAppApiConfigServerApplication.main(PhotoAppApiConfigServerApplication.java:12) [classes!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) [ConfigServer.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:109) [ConfigServer.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) [ConfigServer.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) [ConfigServer.jar:0.0.1-SNAPSHOT]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.amqp.rabbit.connection.CachingConnectionFactory]: Factory method &#39;rabbitConnectionFactory&#39; threw exception; nested exception is java.lang.IllegalArgumentException: Address http://172.17.0.3:5672 seems to contain an unquoted IPv6 address. Make sure you quote IPv6 addresses like so: [2001:db8:85a3:8d3:1319:8a2e:370:7348]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        ... 28 common frames omitted
Caused by: java.lang.IllegalArgumentException: Address http://172.17.0.3:5672 seems to contain an unquoted IPv6 address. Make sure you quote IPv6 addresses like so: [2001:db8:85a3:8d3:1319:8a2e:370:7348]
        at com.rabbitmq.client.Address.parseHost(Address.java:96) ~[amqp-client-5.9.0.jar!/:5.9.0]
        at com.rabbitmq.client.Address.parseAddress(Address.java:158) ~[amqp-client-5.9.0.jar!/:5.9.0]
        at com.rabbitmq.client.Address.parseAddresses(Address.java:173) ~[amqp-client-5.9.0.jar!/:5.9.0]
        at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.setAddresses(AbstractConnectionFactory.java:299) ~[spring-rabbit-2.2.10.RELEASE.jar!/:2.2.10.RELEASE]
        at org.springframework.boot.context.properties.PropertyMapper$Source.to(PropertyMapper.java:316) ~[spring-boot-2.3.3.RELEASE.jar!/:2.3.3.RELEASE]
        at org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration$RabbitConnectionFactoryCreator.rabbitConnectionFactory(RabbitAutoConfiguration.java:102) ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar!/:2.3.3.RELEASE]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        ... 29 common frames omitted

I don't have any idea what is this unquoted IPv6 address and why it is complaining about it here and what am I supposed to do here to remove this error.

答案1

得分: 9

amqpclie-Address源代码

/**
 * 从包含主机名、IP地址、主机名:端口对或IP地址:端口对的字符串中提取主机名或IP地址。
 * 请注意,IPv6地址必须用方括号括起来,例如:[2001:db8:85a3:8d3:1319:8a2e:370:7348]。
 *
 * @param addressString 要从中提取主机名的字符串
 * @return 主机名或IP地址
 */
public static String parseHost(String addressString) {          
   //...
  int lastClosingSquareBracket = addressString.lastIndexOf("]");
     if (lastClosingSquareBracket == -1) {
                String[] parts = addressString.split(":");
                if (parts.length > 2) { // 这里出现了问题
                    String msg = "地址" +
                                        addressString +
                                        "似乎包含了未引用的IPv6地址。请确保像这样引用IPv6地址:[2001:db8:85a3:8d3:1319:8a2e:370:7348]";
                    LOGGER.error(msg);
                    throw new IllegalArgumentException(msg);
                }                   
            return parts[0];
        }
   //...
}

您应该删除http前缀(这将使您的地址被视为IPv4格式):

docker run -p 8012:8012 -e "spring.rabbitmq.host=172.17.0.3" config-server
英文:

From amqpclie-Address source code :

   /**
     * Extracts hostname or IP address from a string containing a hostname, IP address,
     * hostname:port pair or IP address:port pair.
     * Note that IPv6 addresses must be quoted with square brackets, e.g. [2001:db8:85a3:8d3:1319:8a2e:370:7348].
     *
     * @param addressString the string to extract hostname from
     * @return the hostname or IP address
     */
    public static String parseHost(String addressString) {          
       //...
      int lastClosingSquareBracket = addressString.lastIndexOf(&quot;]&quot;);
         if (lastClosingSquareBracket == -1) {
                    String[] parts = addressString.split(&quot;:&quot;);
                    if (parts.length &gt; 2) { // HERE OCCURS THE ISSUE
                        String msg = &quot;Address &quot; +
                                            addressString +
                                            &quot; seems to contain an unquoted IPv6 address. Make sure you quote IPv6 addresses like so: [2001:db8:85a3:8d3:1319:8a2e:370:7348]&quot;;
                        LOGGER.error(msg);
                        throw new IllegalArgumentException(msg);
                    }                   
                return parts[0];
            }
       //...
    }

you should remove the http prefix (it will make your address to be considered as ip v4 format):

docker run -p 8012:8012 -e &quot;spring.rabbitmq.host=172.17.0.3&quot; config-server

huangapple
  • 本文由 发表于 2020年8月22日 23:10:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63537708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定