-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
HostAddressSupplier
(#389)
- Loading branch information
Showing
17 changed files
with
255 additions
and
13 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
cosid-core/src/main/java/me/ahoo/cosid/machine/HostAddressSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.cosid.machine; | ||
|
||
@FunctionalInterface | ||
public interface HostAddressSupplier { | ||
String getHostAddress(); | ||
} | ||
|
55 changes: 55 additions & 0 deletions
55
cosid-core/src/main/java/me/ahoo/cosid/machine/LocalHostAddressSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.cosid.machine; | ||
|
||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.net.Inet4Address; | ||
import java.net.InetAddress; | ||
import java.net.NetworkInterface; | ||
import java.util.Enumeration; | ||
|
||
@Slf4j | ||
public final class LocalHostAddressSupplier implements HostAddressSupplier { | ||
public static final LocalHostAddressSupplier INSTANCE = new LocalHostAddressSupplier(); | ||
|
||
@SneakyThrows | ||
@Override | ||
public String getHostAddress() { | ||
String hostAddress = InetAddress.getLocalHost().getHostAddress(); | ||
try { | ||
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); | ||
while (networkInterfaces.hasMoreElements()) { | ||
NetworkInterface eachNetworkInterface = networkInterfaces.nextElement(); | ||
if (!eachNetworkInterface.isUp() || eachNetworkInterface.isLoopback()) { | ||
continue; | ||
} | ||
Enumeration<InetAddress> inetAddresses = eachNetworkInterface.getInetAddresses(); | ||
while (inetAddresses.hasMoreElements()) { | ||
InetAddress inetAddress = inetAddresses.nextElement(); | ||
if (inetAddress instanceof Inet4Address | ||
&& !inetAddress.isLoopbackAddress() | ||
&& !inetAddress.isAnyLocalAddress() | ||
) { | ||
hostAddress = inetAddress.getHostAddress(); | ||
} | ||
} | ||
} | ||
} catch (Exception ex) { | ||
log.error("Cannot get first non-loopback address", ex); | ||
} | ||
return hostAddress; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
cosid-core/src/test/java/me/ahoo/cosid/machine/LocalHostAddressSupplierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.cosid.machine; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class LocalHostAddressSupplierTest { | ||
|
||
@Test | ||
void getHostName() { | ||
assertThat(LocalHostAddressSupplier.INSTANCE.getHostAddress(), notNullValue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...c/main/java/me/ahoo/cosid/spring/boot/starter/machine/CosIdHostNameAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.cosid.spring.boot.starter.machine; | ||
|
||
import me.ahoo.cosid.machine.HostAddressSupplier; | ||
import me.ahoo.cosid.machine.LocalHostAddressSupplier; | ||
import me.ahoo.cosid.spring.boot.starter.ConditionalOnCosIdEnabled; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.cloud.commons.util.InetUtils; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@AutoConfiguration(afterName = "org.springframework.cloud.commons.util.UtilAutoConfiguration") | ||
@ConditionalOnCosIdEnabled | ||
public class CosIdHostNameAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public HostAddressSupplier hostNameSupplier() { | ||
return LocalHostAddressSupplier.INSTANCE; | ||
} | ||
|
||
@Configuration | ||
@ConditionalOnClass(InetUtils.class) | ||
static class CloudUtilInstanceIdConfiguration { | ||
@Bean | ||
@ConditionalOnBean(InetUtils.class) | ||
public HostAddressSupplier hostNameSupplier(InetUtils inetUtils) { | ||
return new CloudUtilHostAddressSupplier(inetUtils); | ||
} | ||
} | ||
|
||
static class CloudUtilHostAddressSupplier implements HostAddressSupplier { | ||
private final InetUtils inetUtils; | ||
|
||
CloudUtilHostAddressSupplier(InetUtils inetUtils) { | ||
this.inetUtils = inetUtils; | ||
} | ||
|
||
@Override | ||
public String getHostAddress() { | ||
return inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...st/java/me/ahoo/cosid/spring/boot/starter/machine/CosIdHostNameAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package me.ahoo.cosid.spring.boot.starter.machine; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
import me.ahoo.cosid.machine.HostAddressSupplier; | ||
import me.ahoo.cosid.machine.LocalHostAddressSupplier; | ||
|
||
import org.assertj.core.api.AssertionsForInterfaceTypes; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.cloud.commons.util.UtilAutoConfiguration; | ||
|
||
class CosIdHostNameAutoConfigurationTest { | ||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); | ||
|
||
@Test | ||
void contextLoads() { | ||
this.contextRunner | ||
.withPropertyValues(ConditionalOnCosIdMachineEnabled.ENABLED_KEY + "=true") | ||
.withUserConfiguration(CosIdHostNameAutoConfiguration.class) | ||
.run(context -> { | ||
AssertionsForInterfaceTypes.assertThat(context) | ||
.hasSingleBean(LocalHostAddressSupplier.class) | ||
; | ||
assertThat(context.getBean(LocalHostAddressSupplier.class).getHostAddress(), notNullValue()); | ||
}); | ||
} | ||
|
||
@Test | ||
void contextLoadsIfCloud() { | ||
this.contextRunner | ||
.withPropertyValues(ConditionalOnCosIdMachineEnabled.ENABLED_KEY + "=true") | ||
.withUserConfiguration(UtilAutoConfiguration.class, CosIdHostNameAutoConfiguration.class) | ||
.run(context -> { | ||
AssertionsForInterfaceTypes.assertThat(context) | ||
.hasSingleBean(CosIdHostNameAutoConfiguration.CloudUtilHostAddressSupplier.class) | ||
; | ||
assertThat(context.getBean(HostAddressSupplier.class).getHostAddress(), notNullValue()); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.