Docker用户定义的网络 - 通过容器主机名进行的容器间通信

时间:2017-08-19 18:22:51

标签: docker spring-boot activemq

问题 - Spring-Boot消息侦听器Docker容器无法连接到同一个Docker用户定义网络中的ActiveMQ Docker容器

配置Spring-Boot消息侦听器以检查" activemq.hostname "

<bean id="jmsConnectionFactory" 
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="failover:
(tcp://${activemq.hostname:0.0.0.0}:61616)?randomize=false" />
</bean>

为Spring-Boot消息侦听器创建Docker文件 - 将消息代理的主机名设置为 event-broker

FROM openjdk:8-jdk-alpine
EXPOSE 9001
RUN mkdir /app/
COPY target/event-ingestion-router-1.0.2.jar /app/
ENTRYPOINT exec java $JAVA_OPTS -Dactivemq.hostname='event-broker' -jar 
/app/event-ingestion-router-1.0.2.jar

创建Docker网络

docker network create --driver=bridge messaging-network

创建在&#34; messaging-network&#34;上运行的ActiveMQ Broker主机名 event-broker

docker run -d -p 61616:61616 -p 8161:8161 --net=messaging-network \
  --hostname event-broker --name event-broker rmohr/activemq:5.14.3-alpine

在&#34; messaging-network&#34;上创建Spring-Boot消息侦听器 - 连接 event-broker

docker run -d -p 9001:9001 -e JAVA_OPTS='-Xmx256m -Xms128m' \
  --net=messaging-network --name event-ingestion event-ingestion:latest

查看网络消息 - 网络

"Containers": {
"28707ba65bb2d05e1eed38d0f76587129668b61275a6d076674290f9f85d0b36": {
"Name": "event-broker",
"EndpointID": 
"98bc2749fee0442c980347ecbde8002a8462239cb4e0e8d5e01ea2655ec4c722",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"48d049f054baac3183407b8ee320dd27be8645c026ec3502e83d4bc7502ccf01": {
"Name": "event-ingestion",
"EndpointID": 
"6c908d2cf1c922c7b2baa0bb399d1a456b99be542d45ae1559e6cc8f6b9ac68b",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
}

但是无法将消息侦听器连接到消息代理。不知何故,消息代理容器的主机名无法解析。

注意尝试在不使用已弃用的链接的情况下完成此任务。如果没有Compose(现在),也想看看是否可行。

1 个答案:

答案 0 :(得分:0)

根本原因是对Camel Routes使用旧的包扫描技术,扫描在本地工作但在部署到Docker时失败。所以我升级到使用spring-boot camel

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring-boot</artifactId>
    <version>${camel.version}</version>
</dependency> 

使用@Component注释我的Route Builder:

@Component
public class EventIngestionRouteBuilder extends RouteBuilder implements 
RoutesBuilder {
...

使用这种方法,本地和通过Docker部署时都会发现路由。

03:02:41.118 | [ActiveMQ Task-1] | INFO | FailoverTransport | 
Successfully connected to tcp://event-broker:61616

03:02:41.146 | [main] | INFO | SpringCamelContext | Route: route1 
started and consuming from: activemq://event.ingestion

03:02:41.146 | [main] | INFO | SpringCamelContext | Total 1 routes, of 
which 1 are started.