mirror of https://github.com/longtai-cn/hippo4j
Adapter 初始化覆盖核心参数 (#716)
* feat : Unify get the IP and port of the current instance(#609) * feat : add enable prop for this(#609) * feat : add ThreadPoolInitRefresh for init(#609) * feat : adapter thread pool init, including nacos, zookeeper, etcd and apollo(#609)pull/718/head
parent
7dc30efc53
commit
a7f2bb7521
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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.adapter.web;
|
||||
|
||||
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 org.springframework.boot.web.server.WebServer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* Ip and port Holder
|
||||
*/
|
||||
public class WebIpAndPortHolder {
|
||||
|
||||
/**
|
||||
* Application ip and application post
|
||||
*/
|
||||
protected static AtomicReference<WebIpAndPortInfo> webIpAndPort = new AtomicReference<>();
|
||||
|
||||
public static final String ALL = "*";
|
||||
|
||||
protected static final String SEPARATOR = ",";
|
||||
|
||||
private WebIpAndPortHolder() {
|
||||
|
||||
}
|
||||
|
||||
protected static void initIpAndPort() {
|
||||
webIpAndPort.compareAndSet(null, getWebIpAndPortInfo());
|
||||
}
|
||||
|
||||
private static WebIpAndPortInfo getWebIpAndPortInfo() {
|
||||
InetUtils inetUtils = ApplicationContextHolder.getBean(InetUtils.class);
|
||||
InetUtils.HostInfo loopBackHostInfo = inetUtils.findFirstNonLoopBackHostInfo();
|
||||
Assert.notNull(loopBackHostInfo, "Unable to get the application IP address");
|
||||
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
|
||||
WebServer webServer = webThreadPoolService.getWebServer();
|
||||
String port = String.valueOf(webServer.getPort());
|
||||
return new WebIpAndPortInfo(ip, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* get WebIpAndPortInfo, If it is null, initialize it
|
||||
*
|
||||
* @return WebIpAndPortInfo
|
||||
*/
|
||||
public static WebIpAndPortInfo getWebIpAndPort() {
|
||||
if (webIpAndPort.get() == null) {
|
||||
initIpAndPort();
|
||||
}
|
||||
return WebIpAndPortHolder.webIpAndPort.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the new properties and instance IP and port
|
||||
*
|
||||
* @param nodes nodes in properties
|
||||
* @return Whether it meets the conditions
|
||||
*/
|
||||
public static boolean check(String nodes) {
|
||||
WebIpAndPortInfo webIpAndPort = WebIpAndPortHolder.getWebIpAndPort();
|
||||
if (StringUtil.isEmpty(nodes) || ALL.equals(nodes)) {
|
||||
return true;
|
||||
}
|
||||
String[] splitNodes = nodes.split(SEPARATOR);
|
||||
return Arrays.stream(splitNodes)
|
||||
.distinct()
|
||||
.map(WebIpAndPortInfo::build)
|
||||
.filter(Objects::nonNull)
|
||||
.anyMatch(each -> each.check(webIpAndPort.getIpSegment(), webIpAndPort.getPort()));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.api;
|
||||
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
|
||||
/**
|
||||
* when init thread-pool dynamic refresh.
|
||||
*/
|
||||
public interface ThreadPoolInitRefresh extends ApplicationRunner {
|
||||
|
||||
/**
|
||||
* Initializes the thread pool after system startup
|
||||
*
|
||||
* @param context new properties
|
||||
*/
|
||||
void initRefresh(String context);
|
||||
|
||||
/**
|
||||
* get from the Configuration center
|
||||
*
|
||||
* @return new properties
|
||||
* @throws Exception exception
|
||||
*/
|
||||
String getProperties() throws Exception;
|
||||
|
||||
@Override
|
||||
default void run(ApplicationArguments args) throws Exception {
|
||||
String properties = getProperties();
|
||||
if (properties == null) {
|
||||
return;
|
||||
}
|
||||
initRefresh(properties);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue