Skip to content

Commit

Permalink
feat: support HostAddressSupplier (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Jul 28, 2023
1 parent f16a559 commit 469b271
Show file tree
Hide file tree
Showing 17 changed files with 255 additions and 13 deletions.
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();
}

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;
}
}
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());
}
}
6 changes: 5 additions & 1 deletion cosid-spring-boot-starter/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ java {
usingSourceSet(sourceSets[SourceSet.MAIN_SOURCE_SET_NAME])
capability(group.toString(), "mybatis-support", version.toString())
}
registerFeature("cloudSupport") {
usingSourceSet(sourceSets[SourceSet.MAIN_SOURCE_SET_NAME])
capability(group.toString(), "cloud-support", version.toString())
}
}

dependencies {
Expand All @@ -54,7 +58,7 @@ dependencies {

"mybatisSupportImplementation"(project(":cosid-mybatis"))
api("org.springframework.boot:spring-boot-starter")
api("org.springframework.cloud:spring-cloud-commons")
"cloudSupportImplementation"("org.springframework.cloud:spring-cloud-commons")
compileOnly("org.mongodb:mongodb-driver-sync")
compileOnly("org.mongodb:mongodb-driver-reactivestreams")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
Expand Down
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import me.ahoo.cosid.machine.ClockBackwardsSynchronizer;
import me.ahoo.cosid.machine.DefaultClockBackwardsSynchronizer;
import me.ahoo.cosid.machine.DefaultMachineIdGuarder;
import me.ahoo.cosid.machine.HostAddressSupplier;
import me.ahoo.cosid.machine.InstanceId;
import me.ahoo.cosid.machine.LocalMachineStateStorage;
import me.ahoo.cosid.machine.MachineId;
Expand All @@ -34,7 +35,6 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.annotation.Bean;

import java.util.Objects;
Expand All @@ -54,22 +54,20 @@ public CosIdMachineAutoConfiguration(CosIdProperties cosIdProperties, MachinePro

@Bean
@ConditionalOnMissingBean
public InstanceId instanceId(InetUtils inetUtils) {
public InstanceId instanceId(HostAddressSupplier hostAddressSupplier) {

boolean stable = Boolean.TRUE.equals(machineProperties.getStable());

if (!Strings.isNullOrEmpty(machineProperties.getInstanceId())) {
return InstanceId.of(machineProperties.getInstanceId(), stable);
}

InetUtils.HostInfo hostInfo = inetUtils.findFirstNonLoopbackHostInfo();

int port = ProcessId.CURRENT.getProcessId();
if (Objects.nonNull(machineProperties.getPort()) && machineProperties.getPort() > 0) {
port = machineProperties.getPort();
}

return InstanceId.of(hostInfo.getIpAddress(), port, stable);
return InstanceId.of(hostAddressSupplier.getHostAddress(), port, stable);
}

@Bean
Expand Down Expand Up @@ -136,4 +134,5 @@ public MachineIdGuarder machineIdGuarder(MachineIdDistributor machineIdDistribut
public CosIdLifecycleMachineIdGuarder cosIdLifecycleMachineIdGuarder(InstanceId instanceId, MachineIdGuarder machineIdGuarder) {
return new CosIdLifecycleMachineIdGuarder(cosIdProperties, instanceId, machineIdGuarder);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
me.ahoo.cosid.spring.boot.starter.CosIdAutoConfiguration
me.ahoo.cosid.spring.boot.starter.zookeeper.CosIdZookeeperAutoConfiguration
me.ahoo.cosid.spring.boot.starter.machine.CosIdHostNameAutoConfiguration
me.ahoo.cosid.spring.boot.starter.machine.CosIdMachineAutoConfiguration
me.ahoo.cosid.spring.boot.starter.cosid.CosIdGeneratorAutoConfiguration
me.ahoo.cosid.spring.boot.starter.machine.CosIdJdbcMachineIdDistributorAutoConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import me.ahoo.cosid.snowflake.SnowflakeId;
import me.ahoo.cosid.spring.boot.starter.CosIdAutoConfiguration;
import me.ahoo.cosid.spring.boot.starter.machine.ConditionalOnCosIdMachineEnabled;
import me.ahoo.cosid.spring.boot.starter.machine.CosIdHostNameAutoConfiguration;
import me.ahoo.cosid.spring.boot.starter.machine.CosIdLifecycleMachineIdDistributor;
import me.ahoo.cosid.spring.boot.starter.machine.CosIdMachineAutoConfiguration;
import me.ahoo.cosid.spring.boot.starter.machine.MachineProperties;
Expand All @@ -49,6 +50,7 @@ void contextLoads() {
.withPropertyValues(MachineProperties.PREFIX + ".distributor.manual.machineId=1")
.withUserConfiguration(UtilAutoConfiguration.class,
CosIdAutoConfiguration.class,
CosIdHostNameAutoConfiguration.class,
CosIdMachineAutoConfiguration.class,
CosIdGeneratorAutoConfiguration.class)
.run(context -> {
Expand All @@ -74,6 +76,7 @@ void contextLoadsWithType() {
.withPropertyValues(CosIdGeneratorProperties.PREFIX + ".type=RADIX36")
.withUserConfiguration(UtilAutoConfiguration.class,
CosIdAutoConfiguration.class,
CosIdHostNameAutoConfiguration.class,
CosIdMachineAutoConfiguration.class,
CosIdGeneratorAutoConfiguration.class)
.run(context -> {
Expand Down
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());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
class CosIdJdbcMachineIdDistributorAutoConfigurationTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();

@Test
void contextLoads() {
this.contextRunner
Expand All @@ -44,7 +44,11 @@ void contextLoads() {
.withPropertyValues("spring.datasource.url=jdbc:mysql://localhost:3306/cosid_db")
.withPropertyValues("spring.datasource.username=root")
.withPropertyValues("spring.datasource.password=root")
.withUserConfiguration(UtilAutoConfiguration.class, DataSourceAutoConfiguration.class, CosIdAutoConfiguration.class, CosIdMachineAutoConfiguration.class)
.withUserConfiguration(UtilAutoConfiguration.class,
DataSourceAutoConfiguration.class,
CosIdAutoConfiguration.class,
CosIdHostNameAutoConfiguration.class,
CosIdMachineAutoConfiguration.class)
.withUserConfiguration(CosIdJdbcMachineIdDistributorAutoConfiguration.class)
.run(context -> {
assertThat(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ void contextLoadsWhenMachineIdDistributorIsStatefulSet() {
this.contextRunner
.withPropertyValues(ConditionalOnCosIdMachineEnabled.ENABLED_KEY + "=true")
.withPropertyValues(MachineProperties.Distributor.TYPE + "=stateful_set")
.withUserConfiguration(UtilAutoConfiguration.class, CosIdAutoConfiguration.class, CosIdMachineAutoConfiguration.class)
.withUserConfiguration(UtilAutoConfiguration.class,
CosIdAutoConfiguration.class,
CosIdHostNameAutoConfiguration.class,
CosIdMachineAutoConfiguration.class)
.run(context -> {
assertThat(context)
.hasSingleBean(CosIdMachineAutoConfiguration.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void contextLoads() {
MongoAutoConfiguration.class,
MongoReactiveAutoConfiguration.class,
CosIdAutoConfiguration.class,
CosIdHostNameAutoConfiguration.class,
CosIdMachineAutoConfiguration.class,
CosIdMongoMachineIdDistributorAutoConfiguration.class)
.run(context -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ void contextLoads() {
this.contextRunner
.withPropertyValues(ConditionalOnCosIdMachineEnabled.ENABLED_KEY + "=true")
.withPropertyValues(MachineProperties.Distributor.TYPE + "=redis")
.withUserConfiguration(UtilAutoConfiguration.class, CosIdAutoConfiguration.class, CosIdMachineAutoConfiguration.class)
.withUserConfiguration(UtilAutoConfiguration.class,
CosIdAutoConfiguration.class,
CosIdHostNameAutoConfiguration.class,
CosIdMachineAutoConfiguration.class)
.withUserConfiguration(RedisAutoConfiguration.class, CosIdSpringRedisMachineIdDistributorAutoConfiguration.class)
.run(context -> {
assertThat(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ void contextLoads() {
.withPropertyValues(ConditionalOnCosIdMachineEnabled.ENABLED_KEY + "=true")
.withPropertyValues(MachineProperties.Distributor.TYPE + "=zookeeper")
.withPropertyValues(CosIdZookeeperProperties.PREFIX + ".connect-string=" + testingServer.getConnectString())
.withUserConfiguration(UtilAutoConfiguration.class, CosIdAutoConfiguration.class, CosIdZookeeperAutoConfiguration.class, CosIdMachineAutoConfiguration.class)
.withUserConfiguration(UtilAutoConfiguration.class,
CosIdAutoConfiguration.class,
CosIdZookeeperAutoConfiguration.class,
CosIdHostNameAutoConfiguration.class,
CosIdMachineAutoConfiguration.class)
.withUserConfiguration(CosIdZookeeperMachineIdDistributorAutoConfiguration.class)
.run(context -> {
assertThat(context)
Expand Down
Loading

0 comments on commit 469b271

Please sign in to comment.