Maven,Docker获得主机系统的实际IP地址



使用maven和docker-maven-plugin我配置了apache kafka zookeeper容器:

<image>
    <name>wurstmeister/zookeeper:${zookeeper.version}</name>
    <alias>zookeeper</alias>
    <run>
        <ports>
            <port>${zookeeper.port}:2181</port>
        </ports>
    </run>
</image>
<image>
    <name>wurstmeister/kafka:${kafka.version}</name>
    <alias>kafka</alias>
    <run>
        <ports>
            <port>9093:9092</port>
        </ports>
        <links>
            <link>zookeeper:zookeeper</link>
        </links>
        <env>
            <KAFKA_ADVERTISED_HOST_NAME>192.168.1.202</KAFKA_ADVERTISED_HOST_NAME>
            <KAFKA_ADVERTISED_PORT>9093</KAFKA_ADVERTISED_PORT>
            <KAFKA_ZOOKEEPER_CONNECT>zookeeper:${zookeeper.port}</KAFKA_ZOOKEEPER_CONNECT>
        </env>
    </run>
</image>

您可以看到,为了使其正常工作,我必须提供主机系统的实际IP地址:

<KAFKA_ADVERTISED_HOST_NAME>192.168.1.202</KAFKA_ADVERTISED_HOST_NAME>

在Maven或Docker-Maven-Plugin中有任何方法可以自动获取此IP地址,而无需进行硬码?

更新

我找到了允许我检索主机IP地址的插件:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>get-local-ip</id>
                    <goals>
                        <goal>local-ip</goal>
                    </goals>
                    <configuration>
                        <localIpProperty>local.ip</localIpProperty>
                    </configuration>
                </execution>
            </executions>
        </plugin>

,为了使用它,我需要在其余的maven命令之前执行build-helper:local-ip目标:

mvn build-helper:local-ip docker:start

如何用某个阶段/目标绑定此插件以在某些早期初始化阶段自动调用它,而无需每次手动调用build-helper:local-ip

来自build-helper-maven-plugin的文档:

默认绑定到生命周期阶段:过程测试类别

您可以更改它以绑定到您想要的任何阶段,例如

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>local-ip</goal>
                    </goals>
                    <phase>initialize</phase>
                </execution>
            </executions>
        </plugin>

build-helper-maven-plugin的local-ip仅返回127.0.0.1

在我的用例中,网络地址是必要的 - 诸如192.168.17.112

之类的东西
<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <source>
                    java.net.DatagramSocket socket = new java.net.DatagramSocket()
                    socket.connect(java.net.InetAddress.getByName('google.com'), 80)
                    project.properties['host.address'] = socket.getLocalAddress().getHostAddress()
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

然后,属性host.address包含所需的网络IP。

(不要向我投票支持我的答案:使用Java获取当前计算机的IP地址)

相关内容

  • 没有找到相关文章

最新更新