代码之家  ›  专栏  ›  技术社区  ›  alexanoid

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

  •  1
  • alexanoid  · 技术社区  · 8 年前

    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插件 要自动获取此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>
    

    但是为了使用它,我需要执行 build-helper:local-ip 在其余Maven命令之前完成目标:

    mvn build-helper:local-ip docker:start
    

    如何将这个插件与某个阶段/目标绑定,以便在某个早期初始化阶段自动调用它,而无需调用 生成帮助程序:本地ip 每次手动?

    2 回复  |  直到 8 年前
        1
  •  2
  •   skaffman    8 年前

    From the docs for 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>
    
        2
  •  1
  •   Sven Döring    5 年前

    这个 local-ip 从build helper maven插件仅返回 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。

    (不要投票给我,而是投票给帮助我的答案: Getting the IP address of the current machine using Java )

    推荐文章