improve the way to get port and ip (#663)

* improve the way to get port and ip

* fix : Code format
pull/667/head
pizihao 2 years ago committed by GitHub
parent 9adeb96a91
commit 0a6ee0a68e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,101 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 cn.hippo4j.common.model;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
/**
* web ip and port info
*/
@Data
@Slf4j
public class WebIpAndPortInfo {
protected static final String ALL = "*";
protected static final String SPOT = "\\.";
protected static final String COLON = ":";
private String ip;
private String port;
private String[] ipSegment;
public WebIpAndPortInfo(String ip, String port) {
this.ip = ip;
this.port = port;
this.ipSegment = ip.split(SPOT);
}
public static WebIpAndPortInfo build(String node) {
if (ALL.equals(node)) {
return new WebIpAndPortInfo(ALL, ALL);
}
String[] ipPort = node.split(COLON);
if (ipPort.length != 2) {
log.error("The IP address format is error : {}", node);
return null;
}
return new WebIpAndPortInfo(ipPort[0], ipPort[1]);
}
/**
* check
*
* @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 < ipSegment.length && flag; i++) {
String propIp = ipSegment[i];
String appIp = appIpSegment[i];
flag = contrastSegment(appIp, propIp);
}
return flag;
}
/**
* check port
*
* @param port application port
*/
protected boolean checkPort(String 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);
}
}

@ -17,14 +17,14 @@
package cn.hippo4j.config.springboot.starter.refresher.event;
import cn.hippo4j.adapter.web.WebThreadPoolHandlerChoose;
import cn.hippo4j.adapter.web.WebThreadPoolService;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.model.WebIpAndPortInfo;
import cn.hippo4j.common.toolkit.Assert;
import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.core.toolkit.inet.InetUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.event.EventListener;
import java.util.Arrays;
import java.util.Objects;
@ -36,33 +36,33 @@ import java.util.Objects;
public abstract class AbstractRefreshListener<M> implements RefreshListener<Hippo4jConfigDynamicRefreshEvent, M> {
protected static final String ALL = "*";
protected static final String SPOT = "\\.";
protected static final String SEPARATOR = ",";
protected static final String COLON = ":";
/**
* application ip
*/
protected final String[] ipSegment;
/**
* application post
* application ip and application post
*/
protected String port;
protected static volatile WebIpAndPortInfo webIpAndPort;
protected void initIpAndPort() {
if (webIpAndPort == null) {
synchronized (AbstractRefreshListener.class) {
if (webIpAndPort == null) {
webIpAndPort = getWebIpAndPortInfo();
}
}
}
}
AbstractRefreshListener() {
private WebIpAndPortInfo getWebIpAndPortInfo() {
InetUtils inetUtils = ApplicationContextHolder.getBean(InetUtils.class);
InetUtils.HostInfo loopBackHostInfo = inetUtils.findFirstNonLoopBackHostInfo();
Assert.notNull(loopBackHostInfo, "Unable to get the application IP address");
ipSegment = loopBackHostInfo.getIpAddress().split(SPOT);
}
@EventListener(WebServerInitializedEvent.class)
public void webServerInitializedListener(WebServerInitializedEvent event) {
port = String.valueOf(event.getWebServer().getPort());
String ip = loopBackHostInfo.getIpAddress();
WebThreadPoolHandlerChoose webThreadPoolHandlerChoose = ApplicationContextHolder.getBean(WebThreadPoolHandlerChoose.class);
WebThreadPoolService webThreadPoolService = webThreadPoolHandlerChoose.choose();
// when get the port at startup, can get the message: "port xxx was already in use" or use two ports
String port = String.valueOf(webThreadPoolService.getWebServer().getPort());
return new WebIpAndPortInfo(ip, port);
}
/**
@ -81,6 +81,9 @@ public abstract class AbstractRefreshListener<M> implements RefreshListener<Hipp
*/
@Override
public boolean match(M properties) {
if (webIpAndPort == null) {
initIpAndPort();
}
String nodes = getNodes(properties);
if (StringUtil.isEmpty(nodes) || ALL.equals(nodes)) {
return true;
@ -88,9 +91,9 @@ public abstract class AbstractRefreshListener<M> implements RefreshListener<Hipp
String[] splitNodes = nodes.split(SEPARATOR);
return Arrays.stream(splitNodes)
.distinct()
.map(IpAndPort::build)
.map(WebIpAndPortInfo::build)
.filter(Objects::nonNull)
.anyMatch(i -> i.check(ipSegment, port));
.anyMatch(i -> i.check(webIpAndPort.getIpSegment(), webIpAndPort.getPort()));
}
/**
@ -103,79 +106,4 @@ public abstract class AbstractRefreshListener<M> implements RefreshListener<Hipp
return ALL;
}
/**
* ip + port
*/
@Data
protected static class IpAndPort {
private String ip;
private String port;
private String[] propIpSegment;
private IpAndPort(String ip, String port) {
this.ip = ip;
this.port = port;
this.propIpSegment = ip.split(SPOT);
}
public static IpAndPort build(String node) {
if (ALL.equals(node)) {
return new IpAndPort(ALL, ALL);
}
String[] ipPort = node.split(COLON);
if (ipPort.length != 2) {
log.error("The IP address format is error : {}", node);
return null;
}
return new IpAndPort(ipPort[0], ipPort[1]);
}
/**
* check
*
* @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
*/
protected boolean checkPort(String 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);
}
}
}

@ -60,7 +60,7 @@ public class AdapterExecutorsRefreshListener extends AbstractRefreshListener<Ada
continue;
}
if (!Objects.equals(adapterExecutorProperties.getCorePoolSize(), each.getCorePoolSize())
|| !Objects.equals(adapterExecutorProperties.getMaximumPoolSize(), each.getMaximumPoolSize())) {
|| !Objects.equals(adapterExecutorProperties.getMaximumPoolSize(), each.getMaximumPoolSize())) {
threadPoolAdapterMap.forEach((key, val) -> {
if (Objects.equals(val.mark(), each.getMark())) {
val.updateThreadPool(BeanUtil.toBean(each, ThreadPoolAdapterParameter.class));

Loading…
Cancel
Save