fix: The IP and port of the runtime Web (#614)

pull/649/head
pizihao 3 years ago
parent 9a62eb5fe1
commit 20d12371a9

@ -46,7 +46,7 @@ public class AdapterExecutorProperties {
private Integer maximumPoolSize; private Integer maximumPoolSize;
/** /**
* nodes * nodes,application startup is not affect,change properties is effect
*/ */
private String nodes; private String nodes;
} }

@ -104,7 +104,7 @@ public class ExecutorProperties {
private DynamicThreadPoolNotifyProperties notify; private DynamicThreadPoolNotifyProperties notify;
/** /**
* nodes * nodes,application startup is not affect,change properties is effect
*/ */
private String nodes; private String nodes;
} }

@ -41,7 +41,7 @@ public class WebThreadPoolProperties {
private Integer keepAliveTime; private Integer keepAliveTime;
/** /**
* nodes * nodes,application startup is not affect,change properties is effect
*/ */
private String nodes; private String nodes;
} }

@ -23,7 +23,8 @@ import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.core.toolkit.inet.InetUtils; import cn.hippo4j.core.toolkit.inet.InetUtils;
import lombok.Data; import lombok.Data;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.core.env.Environment; import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.event.EventListener;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
@ -34,39 +35,40 @@ import java.util.Objects;
@Log4j2 @Log4j2
public abstract class AbstractRefreshListener<M> implements RefreshListener<Hippo4jConfigDynamicRefreshEvent, M> { public abstract class AbstractRefreshListener<M> implements RefreshListener<Hippo4jConfigDynamicRefreshEvent, M> {
protected static final String PORT_PROPERTY = "local.server.port";
protected static final String ALL = "*"; protected static final String ALL = "*";
/** protected static final String SPOT = "\\.";
* separator
*/
protected static final String SEPARATOR = ","; protected static final String SEPARATOR = ",";
protected static final String COLON = ":";
/** /**
* application ip * application ip
*/ */
protected final String ipAddress; protected final String[] ipSegment;
/** /**
* application post * application post
*/ */
protected final String port; protected String port;
AbstractRefreshListener() { AbstractRefreshListener() {
InetUtils inetUtils = ApplicationContextHolder.getBean(InetUtils.class); InetUtils inetUtils = ApplicationContextHolder.getBean(InetUtils.class);
InetUtils.HostInfo loopbackHostInfo = inetUtils.findFirstNonLoopbackHostInfo(); InetUtils.HostInfo loopbackHostInfo = inetUtils.findFirstNonLoopbackHostInfo();
Assert.isNull(loopbackHostInfo, "Unable to get the application IP address"); Assert.notNull(loopbackHostInfo, "Unable to get the application IP address");
ipAddress = loopbackHostInfo.getIpAddress(); ipSegment = loopbackHostInfo.getIpAddress().split(SPOT);
Environment environment = ApplicationContextHolder.getInstance().getEnvironment(); }
Assert.isTrue(environment.containsProperty(PORT_PROPERTY), "Unable to get the application port");
port = environment.getProperty(PORT_PROPERTY); @EventListener(WebServerInitializedEvent.class)
public void webServerInitializedListener(WebServerInitializedEvent event) {
port = String.valueOf(event.getWebServer().getPort());
} }
/** /**
* Matching nodes<br> * Matching nodes<br>
* nodes is ip + port.Get 'nodes' in the new Properties,Compare this with the ip + port of Application.<br> * nodes is ip + port.Get 'nodes' in the new Properties,Compare this with the ip + port of Application.<br>
* Support prefix pattern matching.e.g: <br> * support prefix pattern matching. e.g: <br>
* <ul> * <ul>
* <li>192.168.1.5:* -- Matches all ports of 192.168.1.5</li> * <li>192.168.1.5:* -- Matches all ports of 192.168.1.5</li>
* <li>192.168.1.*:2009 -- Matches 2009 port of 192.168.1.*</li> * <li>192.168.1.*:2009 -- Matches 2009 port of 192.168.1.*</li>
@ -94,9 +96,9 @@ public abstract class AbstractRefreshListener<M> implements RefreshListener<Hipp
String[] splitNodes = nodes.split(SEPARATOR); String[] splitNodes = nodes.split(SEPARATOR);
return Arrays.stream(splitNodes) return Arrays.stream(splitNodes)
.distinct() .distinct()
.map(IpAndPort::new) .map(IpAndPort::build)
.map(i -> i.check(ipAddress, port)) .filter(Objects::nonNull)
.anyMatch(Boolean.FALSE::equals); .anyMatch(i -> i.check(ipSegment, port));
} }
/** /**
@ -105,27 +107,73 @@ public abstract class AbstractRefreshListener<M> implements RefreshListener<Hipp
@Data @Data
protected static class IpAndPort { protected static class IpAndPort {
protected static final String COLON = ";";
private String ip; private String ip;
private String port; private String port;
private String[] propIpSegment;
private IpAndPort(String ip, String port) {
this.ip = ip;
this.port = port;
this.propIpSegment = ip.split(SPOT);
}
public IpAndPort(String node) { public static IpAndPort build(String node) {
if (ALL.equals(node)) {
return new IpAndPort(ALL, ALL);
}
String[] ipPort = node.split(COLON); String[] ipPort = node.split(COLON);
Assert.isTrue(ipPort.length != 2, "The IP address format is error:" + node); if (ipPort.length != 2) {
ip = ipPort[0]; log.error("The IP address format is error:{}", node);
port = ipPort[1]; return null;
}
return new IpAndPort(ipPort[0], ipPort[1]);
} }
/** /**
* check * check
* *
* @param ip application ip * @param appIpSegment application ip segment
* @param port application port
*/
public boolean check(String[] appIpSegment, String port) {
return checkPort(port) && checkIp(appIpSegment);
}
/**
* check ip
*
* @param appIpSegment application ip segment
*/
protected boolean checkIp(String[] appIpSegment) {
if (ALL.equals(this.ip)) {
return true;
}
boolean flag = true;
for (int i = 0; i < propIpSegment.length && flag; i++) {
String propIp = propIpSegment[i];
String appIp = appIpSegment[i];
flag = contrastSegment(appIp, propIp);
}
return flag;
}
/**
* check port
*
* @param port application port * @param port application port
*/ */
public boolean check(String ip, String port) { protected boolean checkPort(String port) {
return Objects.equals(ip, this.ip) && Objects.equals(port, this.port); return contrastSegment(port, this.port);
} }
/**
* Check whether the strings are the same
*
* @param appIp appIp
* @param propIp propIp
*/
protected boolean contrastSegment(String appIp, String propIp) {
return ALL.equals(propIp) || appIp.equals(propIp);
}
} }
} }

@ -58,7 +58,7 @@ public class AdapterExecutorsRefreshListener extends AbstractRefreshListener<Ada
for (AdapterExecutorProperties each : adapterExecutors) { for (AdapterExecutorProperties each : adapterExecutors) {
String buildKey = each.getMark() + IDENTIFY_SLICER_SYMBOL + each.getThreadPoolKey(); String buildKey = each.getMark() + IDENTIFY_SLICER_SYMBOL + each.getThreadPoolKey();
AdapterExecutorProperties adapterExecutorProperties = DynamicThreadPoolAdapterRegister.ADAPTER_EXECUTORS_MAP.get(buildKey); AdapterExecutorProperties adapterExecutorProperties = DynamicThreadPoolAdapterRegister.ADAPTER_EXECUTORS_MAP.get(buildKey);
if (adapterExecutorProperties == null || match(adapterExecutorProperties)) { if (adapterExecutorProperties == null || !match(adapterExecutorProperties)) {
continue; continue;
} }
if (!Objects.equals(adapterExecutorProperties.getCorePoolSize(), each.getCorePoolSize()) if (!Objects.equals(adapterExecutorProperties.getCorePoolSize(), each.getCorePoolSize())

@ -79,7 +79,7 @@ public class DynamicThreadPoolRefreshListener extends AbstractRefreshListener<Ex
List<ExecutorProperties> executors = bindableConfigProperties.getExecutors(); List<ExecutorProperties> executors = bindableConfigProperties.getExecutors();
for (ExecutorProperties properties : executors) { for (ExecutorProperties properties : executors) {
String threadPoolId = properties.getThreadPoolId(); String threadPoolId = properties.getThreadPoolId();
if (!match(properties) && !checkConsistency(threadPoolId, properties)) { if (!match(properties) || !checkConsistency(threadPoolId, properties)) {
continue; continue;
} }
/* /*

Loading…
Cancel
Save