From 5aebf3bb690ea23b3bb3faefd1489d876767b9cd Mon Sep 17 00:00:00 2001 From: pizihao <2335715300@qq.com> Date: Mon, 14 Nov 2022 18:07:48 +0800 Subject: [PATCH] fix : ThreadPoolAdapterController WebThreadPoolController, WebThreadPoolRunStateController method in does not affect the use of the three interfaces of modify the calls for the use of RPC modules --- .../adapter/base/ThreadPoolAdapterApi.java | 41 ++++++++++ .../base/ThreadPoolAdapterCacheConfig.java | 5 ++ .../base/ThreadPoolAdapterParameter.java | 4 +- .../adapter/base/ThreadPoolAdapterState.java | 9 ++- .../hippo4j/common/api/WebThreadPoolApi.java | 52 +++++++++++++ .../common/api/WebThreadPoolRunStateApi.java | 46 ++++++++++++ .../cn/hippo4j/common/model/InstanceInfo.java | 2 + .../common/model/ThreadDetailStateInfo.java | 3 +- .../common/model/ThreadPoolBaseInfo.java | 4 +- hippo4j-server/hippo4j-config/pom.xml | 5 ++ .../service/ThreadPoolAdapterService.java | 75 ++++++++++++++----- ...stractConfigModificationVerifyService.java | 21 +++++- ...olConfigModificationVerifyServiceImpl.java | 10 --- ...olConfigModificationVerifyServiceImpl.java | 10 --- .../ThreadPoolAdapterController.java | 24 +----- .../controller/ThreadPoolController.java | 53 ++++++------- .../hippo4j-spring-boot-starter/pom.xml | 5 ++ .../starter/config/BootstrapProperties.java | 5 ++ .../DynamicThreadPoolAutoConfiguration.java | 3 +- .../config/NettyServerConfiguration.java | 62 +++++++++++++++ .../ThreadPoolAdapterController.java | 14 ++-- .../controller/WebThreadPoolController.java | 14 ++-- .../WebThreadPoolRunStateController.java | 15 ++-- .../core/ThreadPoolAdapterRegister.java | 2 + .../provider/InstanceInfoProviderFactory.java | 20 +++-- .../starter/toolkit/CloudCommonIdUtil.java | 13 ++++ ...itional-spring-configuration-metadata.json | 6 ++ 27 files changed, 400 insertions(+), 123 deletions(-) create mode 100644 hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterApi.java create mode 100644 hippo4j-common/src/main/java/cn/hippo4j/common/api/WebThreadPoolApi.java create mode 100644 hippo4j-common/src/main/java/cn/hippo4j/common/api/WebThreadPoolRunStateApi.java create mode 100644 hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/NettyServerConfiguration.java diff --git a/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterApi.java b/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterApi.java new file mode 100644 index 00000000..861ea8b4 --- /dev/null +++ b/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterApi.java @@ -0,0 +1,41 @@ +/* + * 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.base; + +import cn.hippo4j.common.web.base.Result; + +/** + * Thread-pool adapter api. + */ +public interface ThreadPoolAdapterApi { + + /** + * Get thread pool information for the third-party framework + * + * @param requestParameter Third party frame identification and other info + * @return thread pool info + */ + Result getAdapterThreadPool(ThreadPoolAdapterParameter requestParameter); + + /** + * Example Modify the thread pool information + * + * @param requestParameter update info + */ + Result updateAdapterThreadPool(ThreadPoolAdapterParameter requestParameter); +} diff --git a/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterCacheConfig.java b/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterCacheConfig.java index 07b2c73c..103fbb23 100644 --- a/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterCacheConfig.java +++ b/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterCacheConfig.java @@ -52,6 +52,11 @@ public class ThreadPoolAdapterCacheConfig { */ private String clientAddress; + /** + * Open server address + */ + private String localServerAddress; + /** * Thread-pool adapter states */ diff --git a/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterParameter.java b/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterParameter.java index c68129eb..54954248 100644 --- a/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterParameter.java +++ b/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterParameter.java @@ -19,11 +19,13 @@ package cn.hippo4j.adapter.base; import lombok.Data; +import java.io.Serializable; + /** * Thread pool adapter parameter info. */ @Data -public class ThreadPoolAdapterParameter { +public class ThreadPoolAdapterParameter implements Serializable { /** * Mark diff --git a/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterState.java b/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterState.java index b46c6c26..75db8a8c 100644 --- a/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterState.java +++ b/hippo4j-adapter/hippo4j-adapter-base/src/main/java/cn/hippo4j/adapter/base/ThreadPoolAdapterState.java @@ -19,11 +19,13 @@ package cn.hippo4j.adapter.base; import lombok.Data; +import java.io.Serializable; + /** * Thread pool adapter state info. */ @Data -public class ThreadPoolAdapterState { +public class ThreadPoolAdapterState implements Serializable { /** * Thread-pool keu @@ -45,6 +47,11 @@ public class ThreadPoolAdapterState { */ private String clientAddress; + /** + * Open server address + */ + private String localServerAddress; + /** * Core size */ diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/api/WebThreadPoolApi.java b/hippo4j-common/src/main/java/cn/hippo4j/common/api/WebThreadPoolApi.java new file mode 100644 index 00000000..36d8294b --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/api/WebThreadPoolApi.java @@ -0,0 +1,52 @@ +/* + * 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 cn.hippo4j.common.model.ThreadPoolBaseInfo; +import cn.hippo4j.common.model.ThreadPoolParameterInfo; +import cn.hippo4j.common.model.ThreadPoolRunStateInfo; +import cn.hippo4j.common.web.base.Result; + +/** + * Web thread pool api. + */ +public interface WebThreadPoolApi { + + /** + * Get thread pool information by identifying the mark + * + * @param mark Third party frame identification + * @return thread pool info + */ + Result getPoolBaseState(String mark); + + /** + * Get all thread pool information + * + * @return thread pool info + */ + Result getPoolRunState(); + + /** + * Example Modify the thread pool information + * + * @param threadPoolParameterInfo update info + */ + Result updateWebThreadPool(ThreadPoolParameterInfo threadPoolParameterInfo); + +} diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/api/WebThreadPoolRunStateApi.java b/hippo4j-common/src/main/java/cn/hippo4j/common/api/WebThreadPoolRunStateApi.java new file mode 100644 index 00000000..149705ce --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/api/WebThreadPoolRunStateApi.java @@ -0,0 +1,46 @@ +/* + * 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 cn.hippo4j.common.model.ThreadDetailStateInfo; +import cn.hippo4j.common.model.ThreadPoolRunStateInfo; +import cn.hippo4j.common.web.base.Result; + +import java.util.List; + +/** + * Web thread-pool run state api. + */ +public interface WebThreadPoolRunStateApi { + + /** + * Get the run state info of the web thread pool + * + * @param threadPoolId the thread pool id + * @return the info + */ + Result getPoolRunState(String threadPoolId); + + /** + * Get the run state detail of the web thread pool + * + * @param threadPoolId the thread pool id + * @return the detail + */ + Result> getThreadStateDetail(String threadPoolId); +} diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/model/InstanceInfo.java b/hippo4j-common/src/main/java/cn/hippo4j/common/model/InstanceInfo.java index 204e856a..46aa7801 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/model/InstanceInfo.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/model/InstanceInfo.java @@ -50,6 +50,8 @@ public class InstanceInfo { private String callBackUrl; + private String serverUrl; + private String identify; private String active; diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadDetailStateInfo.java b/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadDetailStateInfo.java index b8777540..c0033c0b 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadDetailStateInfo.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadDetailStateInfo.java @@ -23,6 +23,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; +import java.io.Serializable; import java.util.List; /** @@ -33,7 +34,7 @@ import java.util.List; @NoArgsConstructor @AllArgsConstructor @Accessors(chain = true) -public class ThreadDetailStateInfo { +public class ThreadDetailStateInfo implements Serializable { /** * threadId diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolBaseInfo.java b/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolBaseInfo.java index 8fba9dd0..000c5050 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolBaseInfo.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolBaseInfo.java @@ -20,12 +20,14 @@ package cn.hippo4j.common.model; import lombok.Data; import lombok.experimental.Accessors; +import java.io.Serializable; + /** * Thread-pool base info. */ @Data @Accessors(chain = true) -public class ThreadPoolBaseInfo { +public class ThreadPoolBaseInfo implements Serializable { /** * coreSize diff --git a/hippo4j-server/hippo4j-config/pom.xml b/hippo4j-server/hippo4j-config/pom.xml index f548e2cc..78853c06 100644 --- a/hippo4j-server/hippo4j-config/pom.xml +++ b/hippo4j-server/hippo4j-config/pom.xml @@ -19,6 +19,11 @@ hippo4j-common ${revision} + + cn.hippo4j + hippo4j-rpc + ${revision} + org.springframework.boot spring-boot-starter diff --git a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/ThreadPoolAdapterService.java b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/ThreadPoolAdapterService.java index 03b23bb5..6453337e 100644 --- a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/ThreadPoolAdapterService.java +++ b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/ThreadPoolAdapterService.java @@ -17,19 +17,24 @@ package cn.hippo4j.config.service; +import cn.hippo4j.adapter.base.ThreadPoolAdapterApi; import cn.hippo4j.adapter.base.ThreadPoolAdapterCacheConfig; +import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter; import cn.hippo4j.adapter.base.ThreadPoolAdapterState; +import cn.hippo4j.common.constant.ConfigModifyTypeConstants; import cn.hippo4j.common.design.observer.AbstractSubjectCenter; import cn.hippo4j.common.design.observer.Observer; import cn.hippo4j.common.design.observer.ObserverMessage; +import cn.hippo4j.common.toolkit.BeanUtil; import cn.hippo4j.common.toolkit.CollectionUtil; -import cn.hippo4j.common.toolkit.JSONUtil; -import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.common.toolkit.http.HttpUtil; +import cn.hippo4j.common.toolkit.UserContext; import cn.hippo4j.common.web.base.Result; import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterReqDTO; import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterRespDTO; -import com.fasterxml.jackson.core.type.TypeReference; +import cn.hippo4j.config.model.biz.threadpool.ConfigModifySaveReqDTO; +import cn.hippo4j.config.verify.ConfigModificationVerifyServiceChoose; +import cn.hippo4j.rpc.support.NettyProxyCenter; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -44,6 +49,7 @@ import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL; */ @Slf4j @Service +@RequiredArgsConstructor public class ThreadPoolAdapterService { /** @@ -51,6 +57,10 @@ public class ThreadPoolAdapterService { */ private static final Map>>> THREAD_POOL_ADAPTER_MAP = new ConcurrentHashMap<>(); + private static final Class CLASS = ThreadPoolAdapterApi.class; + + private final ConfigModificationVerifyServiceChoose configModificationVerifyServiceChoose; + static { AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.CLEAR_CONFIG_CACHE, new ClearThreadPoolAdapterCache()); } @@ -76,12 +86,16 @@ public class ThreadPoolAdapterService { adapterStateList = new ArrayList<>(); tenantItemMap.put(adapterState.getThreadPoolKey(), adapterStateList); } - Optional first = adapterStateList.stream().filter(state -> Objects.equals(state.getClientAddress(), each.getClientAddress())).findFirst(); + String clientAddress = each.getClientAddress(); + String localServerAddress = each.getLocalServerAddress(); + Optional first = adapterStateList.stream().filter(state -> Objects.equals(state.getClientAddress(), clientAddress)).findFirst(); if (!first.isPresent()) { ThreadPoolAdapterState state = new ThreadPoolAdapterState(); - state.setClientAddress(each.getClientAddress()); + state.setClientAddress(clientAddress); state.setIdentify(each.getClientIdentify()); + state.setLocalServerAddress(localServerAddress); adapterStateList.add(state); + NettyProxyCenter.getProxy(CLASS, localServerAddress); } } } @@ -93,20 +107,16 @@ public class ThreadPoolAdapterService { .map(each -> each.get(requestParameter.getTenant() + IDENTIFY_SLICER_SYMBOL + requestParameter.getItem())) .map(each -> each.get(requestParameter.getThreadPoolKey())) .orElse(new ArrayList<>()); - List addressList = actual.stream().map(ThreadPoolAdapterState::getClientAddress).collect(Collectors.toList()); + List addressList = actual.stream().map(ThreadPoolAdapterState::getLocalServerAddress).collect(Collectors.toList()); List result = new ArrayList<>(addressList.size()); addressList.forEach(each -> { - String url = StringUtil.newBuilder("http://", each, "/adapter/thread-pool/info"); - Map param = new HashMap<>(); - param.put("mark", requestParameter.getMark()); - param.put("threadPoolKey", requestParameter.getThreadPoolKey()); try { - String resultStr = HttpUtil.get(url, param); - if (StringUtil.isNotBlank(resultStr)) { - Result restResult = JSONUtil.parseObject(resultStr, new TypeReference>() { - }); - result.add(restResult.getData()); - } + ThreadPoolAdapterApi adapterApi = NettyProxyCenter.getProxy(CLASS, each); + ThreadPoolAdapterParameter parameter = new ThreadPoolAdapterParameter(); + parameter.setThreadPoolKey(requestParameter.getThreadPoolKey()); + parameter.setMark(requestParameter.getMark()); + Result adapterThreadPool = adapterApi.getAdapterThreadPool(parameter); + result.add(BeanUtil.convert(adapterThreadPool.getData(), ThreadPoolAdapterRespDTO.class)); } catch (Throwable ex) { log.error("Failed to get third-party thread pool data.", ex); } @@ -126,10 +136,37 @@ public class ThreadPoolAdapterService { return new HashSet<>(); } + public void updateThreadPool(ThreadPoolAdapterReqDTO requestParameter) { + if (UserContext.getUserRole().equals("ROLE_ADMIN")) { + List actual = Optional.ofNullable(THREAD_POOL_ADAPTER_MAP.get(requestParameter.getMark())) + .map(each -> each.get(requestParameter.getTenant() + IDENTIFY_SLICER_SYMBOL + requestParameter.getItem())) + .map(each -> each.get(requestParameter.getThreadPoolKey())) + .orElse(new ArrayList<>()); + actual.forEach(t -> { + String localServerAddress = t.getLocalServerAddress(); + ThreadPoolAdapterApi adapterApi = NettyProxyCenter.getProxy(CLASS, localServerAddress); + ThreadPoolAdapterParameter parameter = BeanUtil.convert(requestParameter, ThreadPoolAdapterParameter.class); + adapterApi.updateAdapterThreadPool(parameter); + }); + } else { + ConfigModifySaveReqDTO modifySaveReqDTO = BeanUtil.convert(requestParameter, ConfigModifySaveReqDTO.class); + modifySaveReqDTO.setModifyUser(UserContext.getUserName()); + modifySaveReqDTO.setTenantId(requestParameter.getTenant()); + modifySaveReqDTO.setItemId(requestParameter.getItem()); + modifySaveReqDTO.setTpId(requestParameter.getThreadPoolKey()); + modifySaveReqDTO.setType(ConfigModifyTypeConstants.ADAPTER_THREAD_POOL); + configModificationVerifyServiceChoose.choose(modifySaveReqDTO.getType()).saveConfigModifyApplication(modifySaveReqDTO); + } + } + public static void remove(String identify) { synchronized (ThreadPoolAdapterService.class) { - THREAD_POOL_ADAPTER_MAP.values() - .forEach(each -> each.forEach((key, val) -> val.forEach((threadPoolKey, states) -> states.removeIf(adapterState -> Objects.equals(adapterState.getIdentify(), identify))))); + THREAD_POOL_ADAPTER_MAP.values().forEach(each -> each.forEach((key, val) -> val.forEach((threadPoolKey, states) -> { + states.stream() + .filter(s -> Objects.equals(s.getIdentify(), identify)) + .forEach(t -> NettyProxyCenter.removeProxy(CLASS, t.getLocalServerAddress())); + states.removeIf(s -> Objects.equals(s.getIdentify(), identify)); + }))); } } diff --git a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/AbstractConfigModificationVerifyService.java b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/AbstractConfigModificationVerifyService.java index 6648b322..1889e1b5 100644 --- a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/AbstractConfigModificationVerifyService.java +++ b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/AbstractConfigModificationVerifyService.java @@ -17,6 +17,8 @@ package cn.hippo4j.config.service.biz.impl; +import cn.hippo4j.adapter.base.ThreadPoolAdapterApi; +import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter; import cn.hippo4j.common.enums.VerifyEnum; import cn.hippo4j.common.model.InstanceInfo; import cn.hippo4j.common.toolkit.BeanUtil; @@ -30,6 +32,7 @@ import cn.hippo4j.config.model.biz.threadpool.ConfigModifyVerifyReqDTO; import cn.hippo4j.config.service.biz.ConfigModificationVerifyService; import cn.hippo4j.discovery.core.BaseInstanceRegistry; import cn.hippo4j.discovery.core.Lease; +import cn.hippo4j.rpc.support.NettyProxyCenter; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import javax.annotation.Resource; @@ -45,6 +48,8 @@ public abstract class AbstractConfigModificationVerifyService implements ConfigM @Resource protected HisConfigVerifyMapper hisConfigVerifyMapper; + private static final Class CLASS = ThreadPoolAdapterApi.class; + @Resource private BaseInstanceRegistry baseInstanceRegistry; @@ -100,11 +105,11 @@ public abstract class AbstractConfigModificationVerifyService implements ConfigM List> leases = baseInstanceRegistry.listInstance(reqDTO.getItemId()); ConditionUtil .condition(reqDTO.getModifyAll(), - () -> leases.forEach(lease -> clientAddressList.add(lease.getHolder().getCallBackUrl())), + () -> leases.forEach(lease -> clientAddressList.add(lease.getHolder().getServerUrl())), () -> clientAddressList.add( leases.stream() .filter(lease -> lease.getHolder().getIdentify().equals(reqDTO.getIdentify())).findAny().orElseThrow(() -> new RuntimeException("该线程池实例不存在")).getHolder() - .getCallBackUrl())); + .getServerUrl())); return clientAddressList; } @@ -113,5 +118,15 @@ public abstract class AbstractConfigModificationVerifyService implements ConfigM * * @param reqDTO */ - protected abstract void updateThreadPoolParameter(ConfigModifyVerifyReqDTO reqDTO); + protected void updateThreadPoolParameter(ConfigModifyVerifyReqDTO reqDTO) { + for (String each : getClientAddress(reqDTO)) { + ThreadPoolAdapterApi poolAdapterApi = NettyProxyCenter.getProxy(CLASS, each); + ThreadPoolAdapterParameter parameter = new ThreadPoolAdapterParameter(); + parameter.setMark(reqDTO.getMark()); + parameter.setMaximumPoolSize(reqDTO.getMaximumPoolSize()); + parameter.setCorePoolSize(reqDTO.getCorePoolSize()); + parameter.setThreadPoolKey(reqDTO.getThreadPoolKey()); + poolAdapterApi.updateAdapterThreadPool(parameter); + } + } } diff --git a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/AdapterThreadPoolConfigModificationVerifyServiceImpl.java b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/AdapterThreadPoolConfigModificationVerifyServiceImpl.java index fb6cf00f..70a497bd 100644 --- a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/AdapterThreadPoolConfigModificationVerifyServiceImpl.java +++ b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/AdapterThreadPoolConfigModificationVerifyServiceImpl.java @@ -18,9 +18,6 @@ package cn.hippo4j.config.service.biz.impl; import cn.hippo4j.common.constant.ConfigModifyTypeConstants; -import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.common.toolkit.http.HttpUtil; -import cn.hippo4j.config.model.biz.threadpool.ConfigModifyVerifyReqDTO; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -36,11 +33,4 @@ public class AdapterThreadPoolConfigModificationVerifyServiceImpl extends Abstra return ConfigModifyTypeConstants.ADAPTER_THREAD_POOL; } - @Override - protected void updateThreadPoolParameter(ConfigModifyVerifyReqDTO reqDTO) { - for (String each : getClientAddress(reqDTO)) { - String urlString = StringUtil.newBuilder("http://", each, "/adapter/thread-pool/update"); - HttpUtil.post(urlString, reqDTO); - } - } } diff --git a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/WebThreadPoolConfigModificationVerifyServiceImpl.java b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/WebThreadPoolConfigModificationVerifyServiceImpl.java index e4fb6b17..9e527b77 100644 --- a/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/WebThreadPoolConfigModificationVerifyServiceImpl.java +++ b/hippo4j-server/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/WebThreadPoolConfigModificationVerifyServiceImpl.java @@ -18,9 +18,6 @@ package cn.hippo4j.config.service.biz.impl; import cn.hippo4j.common.constant.ConfigModifyTypeConstants; -import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.common.toolkit.http.HttpUtil; -import cn.hippo4j.config.model.biz.threadpool.ConfigModifyVerifyReqDTO; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -36,11 +33,4 @@ public class WebThreadPoolConfigModificationVerifyServiceImpl extends AbstractCo return ConfigModifyTypeConstants.WEB_THREAD_POOL; } - @Override - protected void updateThreadPoolParameter(ConfigModifyVerifyReqDTO reqDTO) { - for (String each : getClientAddress(reqDTO)) { - String urlString = StringUtil.newBuilder("http://", each, "/web/update/pool"); - HttpUtil.post(urlString, reqDTO); - } - } } diff --git a/hippo4j-server/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolAdapterController.java b/hippo4j-server/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolAdapterController.java index bb0094b2..818e7fc6 100644 --- a/hippo4j-server/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolAdapterController.java +++ b/hippo4j-server/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolAdapterController.java @@ -17,18 +17,11 @@ package cn.hippo4j.console.controller; -import cn.hippo4j.common.constant.ConfigModifyTypeConstants; -import cn.hippo4j.common.toolkit.BeanUtil; -import cn.hippo4j.common.toolkit.StringUtil; -import cn.hippo4j.common.toolkit.UserContext; -import cn.hippo4j.common.toolkit.http.HttpUtil; import cn.hippo4j.common.web.base.Result; import cn.hippo4j.common.web.base.Results; import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterReqDTO; import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterRespDTO; -import cn.hippo4j.config.model.biz.threadpool.ConfigModifySaveReqDTO; import cn.hippo4j.config.service.ThreadPoolAdapterService; -import cn.hippo4j.config.verify.ConfigModificationVerifyServiceChoose; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -49,8 +42,6 @@ public class ThreadPoolAdapterController { private final ThreadPoolAdapterService threadPoolAdapterService; - private final ConfigModificationVerifyServiceChoose configModificationVerifyServiceChoose; - @GetMapping(REGISTER_ADAPTER_BASE_PATH + "/query") public Result> queryAdapterThreadPool(ThreadPoolAdapterReqDTO requestParameter) { List result = threadPoolAdapterService.query(requestParameter); @@ -65,20 +56,7 @@ public class ThreadPoolAdapterController { @PostMapping(REGISTER_ADAPTER_BASE_PATH + "/update") public Result updateAdapterThreadPool(@RequestBody ThreadPoolAdapterReqDTO requestParameter) { - if (UserContext.getUserRole().equals("ROLE_ADMIN")) { - for (String each : requestParameter.getClientAddressList()) { - String urlString = StringUtil.newBuilder("http://", each, "/adapter/thread-pool/update"); - HttpUtil.post(urlString, requestParameter); - } - } else { - ConfigModifySaveReqDTO modifySaveReqDTO = BeanUtil.convert(requestParameter, ConfigModifySaveReqDTO.class); - modifySaveReqDTO.setModifyUser(UserContext.getUserName()); - modifySaveReqDTO.setTenantId(requestParameter.getTenant()); - modifySaveReqDTO.setItemId(requestParameter.getItem()); - modifySaveReqDTO.setTpId(requestParameter.getThreadPoolKey()); - modifySaveReqDTO.setType(ConfigModifyTypeConstants.ADAPTER_THREAD_POOL); - configModificationVerifyServiceChoose.choose(modifySaveReqDTO.getType()).saveConfigModifyApplication(modifySaveReqDTO); - } + threadPoolAdapterService.updateThreadPool(requestParameter); return Results.success(); } diff --git a/hippo4j-server/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolController.java b/hippo4j-server/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolController.java index b3ffd783..6811888c 100644 --- a/hippo4j-server/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolController.java +++ b/hippo4j-server/hippo4j-console/src/main/java/cn/hippo4j/console/controller/ThreadPoolController.java @@ -17,23 +17,21 @@ package cn.hippo4j.console.controller; +import cn.hippo4j.common.api.WebThreadPoolApi; +import cn.hippo4j.common.api.WebThreadPoolRunStateApi; import cn.hippo4j.common.constant.ConfigModifyTypeConstants; import cn.hippo4j.common.constant.Constants; import cn.hippo4j.common.model.InstanceInfo; +import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.common.toolkit.BeanUtil; import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.common.toolkit.UserContext; -import cn.hippo4j.common.toolkit.http.HttpUtil; import cn.hippo4j.common.web.base.Result; import cn.hippo4j.common.web.base.Results; import cn.hippo4j.common.web.exception.ErrorCodeEnum; import cn.hippo4j.config.model.CacheItem; -import cn.hippo4j.config.model.biz.threadpool.ConfigModifySaveReqDTO; -import cn.hippo4j.config.model.biz.threadpool.ThreadPoolDelReqDTO; -import cn.hippo4j.config.model.biz.threadpool.ThreadPoolQueryReqDTO; -import cn.hippo4j.config.model.biz.threadpool.ThreadPoolRespDTO; -import cn.hippo4j.config.model.biz.threadpool.ThreadPoolSaveOrUpdateReqDTO; +import cn.hippo4j.config.model.biz.threadpool.*; import cn.hippo4j.config.service.ConfigCacheService; import cn.hippo4j.config.service.biz.ThreadPoolService; import cn.hippo4j.config.verify.ConfigModificationVerifyServiceChoose; @@ -42,17 +40,11 @@ import cn.hippo4j.console.model.WebThreadPoolReqDTO; import cn.hippo4j.console.model.WebThreadPoolRespDTO; import cn.hippo4j.discovery.core.BaseInstanceRegistry; import cn.hippo4j.discovery.core.Lease; +import cn.hippo4j.rpc.support.NettyProxyCenter; import com.baomidou.mybatisplus.core.metadata.IPage; import lombok.RequiredArgsConstructor; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; @@ -75,7 +67,9 @@ public class ThreadPoolController { private final ConfigModificationVerifyServiceChoose configModificationVerifyServiceChoose; - private static final String HTTP = "http://"; + private static final Class STATE_API_CLASS = WebThreadPoolRunStateApi.class; + + private static final Class POOL_API_CLASS = WebThreadPoolApi.class; @PostMapping("/query/page") public Result> queryNameSpacePage(@RequestBody ThreadPoolQueryReqDTO reqDTO) { @@ -122,15 +116,15 @@ public class ThreadPoolController { @GetMapping("/run/state/{tpId}") public Result runState(@PathVariable("tpId") String tpId, @RequestParam(value = "clientAddress") String clientAddress) { - String urlString = StringUtil.newBuilder(HTTP, clientAddress, "/run/state/", tpId); - return HttpUtil.get(urlString, Result.class); + WebThreadPoolRunStateApi threadPoolRunStateApi = NettyProxyCenter.getProxy(STATE_API_CLASS, clientAddress); + return threadPoolRunStateApi.getPoolRunState(tpId); } @GetMapping("/run/thread/state/{tpId}") public Result runThreadState(@PathVariable("tpId") String tpId, @RequestParam(value = "clientAddress") String clientAddress) { - String urlString = StringUtil.newBuilder(HTTP, clientAddress, "/run/thread/state/", tpId); - return HttpUtil.get(urlString, Result.class); + WebThreadPoolRunStateApi threadPoolRunStateApi = NettyProxyCenter.getProxy(STATE_API_CLASS, clientAddress); + return threadPoolRunStateApi.getThreadStateDetail(tpId); } @GetMapping("/list/client/instance/{itemId}") @@ -145,7 +139,7 @@ public class ThreadPoolController { for (Lease each : leases) { Result poolBaseState; try { - poolBaseState = getPoolBaseState(mark, each.getHolder().getCallBackUrl()); + poolBaseState = getPoolBaseState(mark, each.getHolder().getServerUrl()); } catch (Throwable ignored) { continue; } @@ -158,7 +152,7 @@ public class ThreadPoolController { result.setTenantId(each.getHolder().getGroupKey().split("[+]")[1]); result.setActive(each.getHolder().getActive()); result.setIdentify(each.getHolder().getIdentify()); - result.setClientAddress(each.getHolder().getCallBackUrl()); + result.setClientAddress(each.getHolder().getServerUrl()); returnThreadPool.add(result); } return Results.success(returnThreadPool); @@ -167,22 +161,23 @@ public class ThreadPoolController { @GetMapping("/web/base/info") public Result getPoolBaseState(@RequestParam(value = "mark") String mark, @RequestParam(value = "clientAddress") String clientAddress) { - String urlString = StringUtil.newBuilder(HTTP, clientAddress, "/web/base/info", "?mark=", mark); - return HttpUtil.get(urlString, Result.class); + WebThreadPoolApi threadPoolApi = NettyProxyCenter.getProxy(POOL_API_CLASS, clientAddress); + return threadPoolApi.getPoolBaseState(mark); } @GetMapping("/web/run/state") public Result getPoolRunState(@RequestParam(value = "clientAddress") String clientAddress) { - String urlString = StringUtil.newBuilder(HTTP, clientAddress, "/web/run/state"); - return HttpUtil.get(urlString, Result.class); + WebThreadPoolApi threadPoolApi = NettyProxyCenter.getProxy(POOL_API_CLASS, clientAddress); + return threadPoolApi.getPoolRunState(); } @PostMapping("/web/update/pool") public Result updateWebThreadPool(@RequestBody WebThreadPoolReqDTO requestParam) { if (UserContext.getUserRole().equals("ROLE_ADMIN")) { for (String each : requestParam.getClientAddressList()) { - String urlString = StringUtil.newBuilder(HTTP, each, "/web/update/pool"); - HttpUtil.post(urlString, requestParam); + WebThreadPoolApi threadPoolApi = NettyProxyCenter.getProxy(POOL_API_CLASS, each); + ThreadPoolParameterInfo parameterInfo = BeanUtil.convert(requestParam, ThreadPoolParameterInfo.class); + threadPoolApi.updateWebThreadPool(parameterInfo); } } else { ConfigModifySaveReqDTO modifySaveReqDTO = BeanUtil.convert(requestParam, ConfigModifySaveReqDTO.class); @@ -206,9 +201,9 @@ public class ThreadPoolController { String groupKey = getGroupKey(tpId, itemTenantKey); Map content = ConfigCacheService.getContent(groupKey); Map activeMap = - leases.stream().map(each -> each.getHolder()).filter(each -> StringUtil.isNotBlank(each.getActive())) + leases.stream().map(Lease::getHolder).filter(each -> StringUtil.isNotBlank(each.getActive())) .collect(Collectors.toMap(InstanceInfo::getIdentify, InstanceInfo::getActive)); - Map clientBasePathMap = leases.stream().map(each -> each.getHolder()) + Map clientBasePathMap = leases.stream().map(Lease::getHolder) .filter(each -> StringUtil.isNotBlank(each.getClientBasePath())) .collect(Collectors.toMap(InstanceInfo::getIdentify, InstanceInfo::getClientBasePath)); List returnThreadPool = new ArrayList<>(); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml b/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml index 5de5c2ee..c21ced00 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml @@ -33,6 +33,11 @@ hippo4j-message ${revision} + + cn.hippo4j + hippo4j-rpc + ${revision} + org.springframework.boot spring-boot-configuration-processor diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/BootstrapProperties.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/BootstrapProperties.java index 8faf4dcc..35428421 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/BootstrapProperties.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/BootstrapProperties.java @@ -54,6 +54,11 @@ public class BootstrapProperties implements BootstrapPropertiesInterface { */ private String nettyServerPort; + /** + * The service port that is open to the outside world, The default value is 16691 + */ + private Integer localServerPort; + /** * Report type */ diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java index 8614c4fc..fa290c2d 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -82,7 +82,8 @@ import org.springframework.core.env.ConfigurableEnvironment; @ConditionalOnBean(MarkerConfiguration.Marker.class) @EnableConfigurationProperties(BootstrapProperties.class) @ConditionalOnProperty(prefix = BootstrapProperties.PREFIX, value = "enable", matchIfMissing = true, havingValue = "true") -@ImportAutoConfiguration({WebAdapterConfiguration.class, NettyClientConfiguration.class, DiscoveryConfiguration.class, MessageConfiguration.class, UtilAutoConfiguration.class}) +@ImportAutoConfiguration({WebAdapterConfiguration.class, NettyClientConfiguration.class, DiscoveryConfiguration.class, MessageConfiguration.class, UtilAutoConfiguration.class, + NettyServerConfiguration.class}) public class DynamicThreadPoolAutoConfiguration { private final BootstrapProperties properties; diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/NettyServerConfiguration.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/NettyServerConfiguration.java new file mode 100644 index 00000000..d66ee807 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/config/NettyServerConfiguration.java @@ -0,0 +1,62 @@ +/* + * 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.springboot.starter.config; + +import cn.hippo4j.adapter.base.ThreadPoolAdapterApi; +import cn.hippo4j.common.api.WebThreadPoolApi; +import cn.hippo4j.common.api.WebThreadPoolRunStateApi; +import cn.hippo4j.rpc.discovery.ServerPort; +import cn.hippo4j.rpc.discovery.SpringContextInstance; +import cn.hippo4j.rpc.handler.NettyServerTakeHandler; +import cn.hippo4j.rpc.server.NettyServerConnection; +import cn.hippo4j.rpc.server.Server; +import cn.hippo4j.rpc.support.NettyServerSupport; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.context.EnvironmentAware; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +public class NettyServerConfiguration implements ApplicationRunner, EnvironmentAware { + + Environment environment; + + private static final Class[] classes = { + WebThreadPoolApi.class, + ThreadPoolAdapterApi.class, + WebThreadPoolRunStateApi.class + }; + + @Override + public void run(ApplicationArguments args) throws Exception { + Integer port = environment.getProperty("spring.dynamic.thread-pool.local-server-port", Integer.class); + ServerPort serverPort = () -> port == null ? 16691 : port; + NettyServerTakeHandler handler = new NettyServerTakeHandler(new SpringContextInstance()); + try ( + NettyServerConnection connection = new NettyServerConnection(handler); + Server server = new NettyServerSupport(serverPort, connection, classes)) { + server.bind(); + } + } + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } +} diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/ThreadPoolAdapterController.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/ThreadPoolAdapterController.java index 6bc1bad8..50dec09e 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/ThreadPoolAdapterController.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/ThreadPoolAdapterController.java @@ -18,6 +18,7 @@ package cn.hippo4j.springboot.starter.controller; import cn.hippo4j.adapter.base.ThreadPoolAdapter; +import cn.hippo4j.adapter.base.ThreadPoolAdapterApi; import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter; import cn.hippo4j.adapter.base.ThreadPoolAdapterState; import cn.hippo4j.common.api.ClientNetworkService; @@ -31,10 +32,7 @@ import cn.hippo4j.springboot.starter.toolkit.CloudCommonIdUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; import java.util.Optional; @@ -44,15 +42,16 @@ import static cn.hippo4j.adapter.base.ThreadPoolAdapterBeanContainer.THREAD_POOL * Thread-pool adapter controller. */ @Slf4j -@RestController +// @RestController @AllArgsConstructor -public class ThreadPoolAdapterController { +public class ThreadPoolAdapterController implements ThreadPoolAdapterApi { private final ConfigurableEnvironment environment; private final InetUtils hippo4JInetUtils; - @GetMapping("/adapter/thread-pool/info") + @Override + // @GetMapping("/adapter/thread-pool/info") public Result getAdapterThreadPool(ThreadPoolAdapterParameter requestParameter) { ThreadPoolAdapter threadPoolAdapter = THREAD_POOL_ADAPTER_BEAN_CONTAINER.get(requestParameter.getMark()); ThreadPoolAdapterState result = Optional.ofNullable(threadPoolAdapter).map(each -> { @@ -74,7 +73,8 @@ public class ThreadPoolAdapterController { return Results.success(result); } - @PostMapping("/adapter/thread-pool/update") + @Override + // @PostMapping("/adapter/thread-pool/update") public Result updateAdapterThreadPool(@RequestBody ThreadPoolAdapterParameter requestParameter) { log.info("[{}] Change third-party thread pool data. key: {}, coreSize: {}, maximumSize: {}", requestParameter.getMark(), requestParameter.getThreadPoolKey(), requestParameter.getCorePoolSize(), requestParameter.getMaximumPoolSize()); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/WebThreadPoolController.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/WebThreadPoolController.java index a6ba1e51..922dd11b 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/WebThreadPoolController.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/WebThreadPoolController.java @@ -19,6 +19,7 @@ package cn.hippo4j.springboot.starter.controller; import cn.hippo4j.adapter.web.WebThreadPoolHandlerChoose; import cn.hippo4j.adapter.web.WebThreadPoolService; +import cn.hippo4j.common.api.WebThreadPoolApi; import cn.hippo4j.common.model.ThreadPoolBaseInfo; import cn.hippo4j.common.model.ThreadPoolParameterInfo; import cn.hippo4j.common.model.ThreadPoolRunStateInfo; @@ -33,13 +34,14 @@ import org.springframework.web.bind.annotation.*; *

At present, only Tomcat is well supported, and other web containers need to be improved. */ @CrossOrigin -@RestController +// @RestController @AllArgsConstructor -public class WebThreadPoolController { +public class WebThreadPoolController implements WebThreadPoolApi { private final WebThreadPoolHandlerChoose webThreadPoolServiceChoose; - @GetMapping("/web/base/info") + @Override + // @GetMapping("/web/base/info") public Result getPoolBaseState(@RequestParam(value = "mark") String mark) { WebThreadPoolService webThreadPoolService = webThreadPoolServiceChoose.choose(); if (webThreadPoolService != null && webThreadPoolService.getClass().getSimpleName().contains(mark)) { @@ -48,13 +50,15 @@ public class WebThreadPoolController { return Results.success(null); } - @GetMapping("/web/run/state") + @Override + // @GetMapping("/web/run/state") public Result getPoolRunState() { ThreadPoolRunStateInfo result = webThreadPoolServiceChoose.choose().getWebRunStateInfo(); return Results.success(result); } - @PostMapping("/web/update/pool") + @Override + // @PostMapping("/web/update/pool") public Result updateWebThreadPool(@RequestBody ThreadPoolParameterInfo threadPoolParameterInfo) { webThreadPoolServiceChoose.choose().updateWebThreadPool(threadPoolParameterInfo); return Results.success(); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/WebThreadPoolRunStateController.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/WebThreadPoolRunStateController.java index 5b0424d2..63f3d210 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/WebThreadPoolRunStateController.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/controller/WebThreadPoolRunStateController.java @@ -18,16 +18,15 @@ package cn.hippo4j.springboot.starter.controller; import cn.hippo4j.common.api.ThreadDetailState; -import cn.hippo4j.common.model.ThreadPoolRunStateInfo; +import cn.hippo4j.common.api.WebThreadPoolRunStateApi; import cn.hippo4j.common.model.ThreadDetailStateInfo; +import cn.hippo4j.common.model.ThreadPoolRunStateInfo; import cn.hippo4j.common.web.base.Result; import cn.hippo4j.common.web.base.Results; import cn.hippo4j.core.executor.state.ThreadPoolRunStateHandler; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; import java.util.List; @@ -35,21 +34,23 @@ import java.util.List; * Web thread-pool run state controller. */ @CrossOrigin -@RestController +// @RestController @AllArgsConstructor -public class WebThreadPoolRunStateController { +public class WebThreadPoolRunStateController implements WebThreadPoolRunStateApi { private final ThreadPoolRunStateHandler threadPoolRunStateHandler; private final ThreadDetailState threadDetailState; - @GetMapping("/run/state/{threadPoolId}") + @Override + // @GetMapping("/run/state/{threadPoolId}") public Result getPoolRunState(@PathVariable("threadPoolId") String threadPoolId) { ThreadPoolRunStateInfo result = threadPoolRunStateHandler.getPoolRunState(threadPoolId); return Results.success(result); } - @GetMapping("/run/thread/state/{threadPoolId}") + @Override + // @GetMapping("/run/thread/state/{threadPoolId}") public Result> getThreadStateDetail(@PathVariable("threadPoolId") String threadPoolId) { List result = threadDetailState.getThreadDetailStateInfo(threadPoolId); return Results.success(result); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ThreadPoolAdapterRegister.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ThreadPoolAdapterRegister.java index a5def8e0..163460d1 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ThreadPoolAdapterRegister.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/core/ThreadPoolAdapterRegister.java @@ -79,6 +79,8 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner, ThreadPoolA String clientAddress = CloudCommonIdUtil.getClientIpPort(environment, hippo4JInetUtils); cacheConfig.setClientAddress(clientAddress); cacheConfig.setThreadPoolAdapterStates(threadPoolStates); + String localServerAddress = CloudCommonIdUtil.getLocalServerIpPort(environment, hippo4JInetUtils); + cacheConfig.setLocalServerAddress(localServerAddress); adapterCacheConfigList.add(cacheConfig); } return adapterCacheConfigList; diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/provider/InstanceInfoProviderFactory.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/provider/InstanceInfoProviderFactory.java index 41a20e9e..78cf1574 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/provider/InstanceInfoProviderFactory.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/provider/InstanceInfoProviderFactory.java @@ -21,6 +21,7 @@ import cn.hippo4j.common.api.ClientNetworkService; import cn.hippo4j.common.model.InstanceInfo; import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader; import cn.hippo4j.common.toolkit.ContentUtil; +import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.core.toolkit.IdentifyUtil; import cn.hippo4j.core.toolkit.inet.InetUtils; import cn.hippo4j.springboot.starter.config.BootstrapProperties; @@ -58,12 +59,12 @@ public final class InstanceInfoProviderFactory { String namespace = bootstrapProperties.getNamespace(); String itemId = bootstrapProperties.getItemId(); String port = environment.getProperty("server.port", "8080"); + String serverPort = environment.getProperty("spring.dynamic.thread-pool.local-server-port", "16691"); String applicationName = environment.getProperty("spring.dynamic.thread-pool.item-id"); String active = environment.getProperty("spring.profiles.active", "UNKNOWN"); InstanceInfo instanceInfo = new InstanceInfo(); String instanceId = CloudCommonIdUtil.getDefaultInstanceId(environment, inetUtils); - instanceId = new StringBuilder() - .append(instanceId).append(IDENTIFY_SLICER_SYMBOL).append(CLIENT_IDENTIFICATION_VALUE).toString(); + instanceId = instanceId + IDENTIFY_SLICER_SYMBOL + CLIENT_IDENTIFICATION_VALUE; String contextPath = environment.getProperty("server.servlet.context-path", ""); instanceInfo.setInstanceId(instanceId) .setIpApplicationName(CloudCommonIdUtil.getIpApplicationName(environment, inetUtils)) @@ -71,10 +72,19 @@ public final class InstanceInfoProviderFactory { .setPort(port).setClientBasePath(contextPath).setGroupKey(ContentUtil.getGroupKey(itemId, namespace)); String[] customerNetwork = DynamicThreadPoolServiceLoader.getSingletonServiceInstances(ClientNetworkService.class) .stream().findFirst().map(each -> each.getNetworkIpPort(environment)).orElse(null); - String callBackUrl = new StringBuilder().append(Optional.ofNullable(customerNetwork).map(each -> each[0]).orElse(instanceInfo.getHostName())).append(":") - .append(Optional.ofNullable(customerNetwork).map(each -> each[1]).orElse(port)).append(instanceInfo.getClientBasePath()) - .toString(); + + String callBackUrl = StringUtil.newBuilder( + Optional.ofNullable(customerNetwork).map(each -> each[0]).orElse(instanceInfo.getHostName()), + ":", + Optional.ofNullable(customerNetwork).map(each -> each[1]).orElse(port), + instanceInfo.getClientBasePath()); + String serverUrl = StringUtil.newBuilder( + Optional.ofNullable(customerNetwork).map(each -> each[0]).orElse(instanceInfo.getHostName()), + ":", + serverPort, + instanceInfo.getClientBasePath()); instanceInfo.setCallBackUrl(callBackUrl); + instanceInfo.setServerUrl(serverUrl); String identify = IdentifyUtil.generate(environment, inetUtils); instanceInfo.setIdentify(identify); instanceInfo.setActive(active.toUpperCase()); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/toolkit/CloudCommonIdUtil.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/toolkit/CloudCommonIdUtil.java index 53c79ff4..1524d28f 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/toolkit/CloudCommonIdUtil.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/springboot/starter/toolkit/CloudCommonIdUtil.java @@ -44,6 +44,19 @@ public class CloudCommonIdUtil { return combineParts(hostname, SEPARATOR, port); } + /** + * Get local server ip port. + * + * @param resolver resolver + * @param inetUtils inet utils + * @return ip and port + */ + public static String getLocalServerIpPort(PropertyResolver resolver, InetUtils inetUtils) { + String hostname = inetUtils.findFirstNonLoopBackHostInfo().getIpAddress(); + String port = resolver.getProperty("spring.dynamic.thread-pool.local-server-port", "16691"); + return combineParts(hostname, SEPARATOR, port); + } + /** * Get default instance id. * diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json index fe916f2f..b806eaec 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -12,6 +12,12 @@ "defaultValue": "8899", "description": "dynamic thread-pool server netty port." }, + { + "name": "spring.dynamic.thread-pool.local-server-port", + "type": "java.lang.String", + "defaultValue": "19961", + "description": "dynamic thread-pool local server port." + }, { "name": "spring.dynamic.thread-pool.report-type", "type": "java.lang.String",