Mandatory to specify the ip+port registered by the client (#799)

pull/806/head
chen.ma 2 years ago
parent 2283b4a50d
commit 7a996b7041

@ -0,0 +1,34 @@
/*
* 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.core.env.ConfigurableEnvironment;
/**
* Client network service.
*/
public interface ClientNetworkService {
/**
* Get network ip port. return as an array 127.0.0.1,8080
*
* @param environment environment
* @return network ip port
*/
String[] getNetworkIpPort(ConfigurableEnvironment environment);
}

@ -173,7 +173,7 @@ public class IoUtil {
public static long copy(Reader input, Writer output) throws IOException {
char[] buffer = new char[1 << 12];
long count = 0;
for (int n = 0; (n = input.read(buffer)) >= 0; ) {
for (int n = 0; (n = input.read(buffer)) >= 0;) {
output.write(buffer, 0, n);
count += n;
}

@ -17,7 +17,6 @@
package cn.hippo4j.common.executor;
import cn.hippo4j.common.toolkit.ReflectUtil;
import org.junit.Assert;
import org.junit.Test;
@ -30,16 +29,16 @@ import java.util.concurrent.ScheduledExecutorService;
public class ThreadPoolManagerTest {
// tenantId = schedulegroup = schedule
// tenantId = schedulegroup = schedule
ScheduledExecutorService executorService1 = Executors.newScheduledThreadPool(1);
// tenantId = schedulegroup = schedule
// tenantId = schedulegroup = schedule
ScheduledExecutorService executorService2 = Executors.newScheduledThreadPool(10);
// tenantId = executorgroup = executor
// tenantId = executorgroup = executor
ExecutorService executorService3 = Executors.newFixedThreadPool(8);
// tenantId = executorgroup = executor
// tenantId = executorgroup = executor
ExecutorService executorService4 = Executors.newFixedThreadPool(16);
static final String schedule = "schedule";

@ -17,13 +17,18 @@
package cn.hippo4j.core.toolkit;
import cn.hippo4j.common.api.ClientNetworkService;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.toolkit.*;
import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader;
import cn.hippo4j.common.toolkit.CollectionUtil;
import cn.hippo4j.common.toolkit.IdUtil;
import cn.hippo4j.common.toolkit.Joiner;
import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.common.toolkit.ThreadUtil;
import cn.hippo4j.core.toolkit.inet.InetUtils;
import org.springframework.core.env.ConfigurableEnvironment;
import java.util.ArrayList;
import java.util.UUID;
import static cn.hippo4j.common.constant.Constants.GROUP_KEY_DELIMITER;
import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL;
@ -37,34 +42,47 @@ public class IdentifyUtil {
public static final String CLIENT_IDENTIFICATION_VALUE = IdUtil.simpleUUID();
static {
DynamicThreadPoolServiceLoader.register(ClientNetworkService.class);
}
/**
* Generate identify.
*
* @param environment
* @param hippo4JInetUtils
* @return
* @param environment environment
* @param inetUtil inet util
* @return identify
*/
public static synchronized String generate(ConfigurableEnvironment environment, InetUtils hippo4JInetUtils) {
public static synchronized String generate(ConfigurableEnvironment environment, InetUtils inetUtil) {
if (StringUtil.isNotBlank(IDENTIFY)) {
return IDENTIFY;
}
String ip = hippo4JInetUtils.findFirstNonLoopBackHostInfo().getIpAddress();
String port = environment.getProperty("server.port", "8080");
String identification = new StringBuilder()
String[] customerNetwork = DynamicThreadPoolServiceLoader.getSingletonServiceInstances(ClientNetworkService.class)
.stream().findFirst().map(each -> each.getNetworkIpPort(environment)).orElse(null);
String ip;
String port;
if (customerNetwork != null && customerNetwork.length > 0) {
ip = customerNetwork[0];
port = customerNetwork[1];
} else {
ip = inetUtil.findFirstNonLoopBackHostInfo().getIpAddress();
port = environment.getProperty("server.port", "8080");
}
String identify = new StringBuilder()
.append(ip)
.append(":")
.append(port)
.append(IDENTIFY_SLICER_SYMBOL)
.append(CLIENT_IDENTIFICATION_VALUE)
.toString();
IDENTIFY = identification;
return identification;
IDENTIFY = identify;
return identify;
}
/**
* Get identify.
*
* @return
* @return identify
*/
public static String getIdentify() {
while (StringUtil.isBlank(IDENTIFY)) {
@ -82,10 +100,10 @@ public class IdentifyUtil {
/**
* Get thread pool identify.
*
* @param threadPoolId
* @param itemId
* @param namespace
* @return
* @param threadPoolId thread pool id
* @param itemId item id
* @param namespace namespace
* @return identify
*/
public static String getThreadPoolIdentify(String threadPoolId, String itemId, String namespace) {
ArrayList<String> params = CollectionUtil.newArrayList(threadPoolId, itemId, namespace, getIdentify());

@ -0,0 +1,35 @@
/*
* 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.example.core.handler;
import cn.hippo4j.common.api.ClientNetworkService;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* Customer client network service.
*/
public class CustomerClientNetworkService implements ClientNetworkService {
@Override
public String[] getNetworkIpPort(ConfigurableEnvironment environment) {
String[] network = new String[2];
network[0] = "127.0.0.1";
network[1] = environment.getProperty("server.port", "1994");
return network;
}
}

@ -17,19 +17,22 @@
package cn.hippo4j.springboot.starter.config;
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.core.toolkit.IdentifyUtil;
import cn.hippo4j.core.toolkit.inet.InetUtils;
import cn.hippo4j.springboot.starter.toolkit.CloudCommonIdUtil;
import cn.hippo4j.springboot.starter.core.DiscoveryClient;
import cn.hippo4j.springboot.starter.remote.HttpAgent;
import cn.hippo4j.springboot.starter.toolkit.CloudCommonIdUtil;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import java.net.InetAddress;
import java.util.Optional;
import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL;
import static cn.hippo4j.core.toolkit.IdentifyUtil.CLIENT_IDENTIFICATION_VALUE;
@ -46,6 +49,10 @@ public class DiscoveryConfiguration {
private final InetUtils hippo4JInetUtils;
static {
DynamicThreadPoolServiceLoader.register(ClientNetworkService.class);
}
@Bean
@SneakyThrows
public InstanceInfo instanceConfig() {
@ -69,8 +76,10 @@ public class DiscoveryConfiguration {
.setPort(port)
.setClientBasePath(contextPath)
.setGroupKey(ContentUtil.getGroupKey(itemId, namespace));
String callBackUrl = new StringBuilder().append(instanceInfo.getHostName()).append(":")
.append(port).append(instanceInfo.getClientBasePath())
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();
instanceInfo.setCallBackUrl(callBackUrl);
String identify = IdentifyUtil.generate(environment, hippo4JInetUtils);

@ -20,6 +20,9 @@ package cn.hippo4j.springboot.starter.controller;
import cn.hippo4j.adapter.base.ThreadPoolAdapter;
import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter;
import cn.hippo4j.adapter.base.ThreadPoolAdapterState;
import cn.hippo4j.common.api.ClientNetworkService;
import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader;
import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.common.web.base.Result;
import cn.hippo4j.common.web.base.Results;
import cn.hippo4j.core.toolkit.IdentifyUtil;
@ -56,7 +59,14 @@ public class ThreadPoolAdapterController {
ThreadPoolAdapterState threadPoolState = each.getThreadPoolState(requestParameter.getThreadPoolKey());
String active = environment.getProperty("spring.profiles.active", "UNKNOWN");
threadPoolState.setActive(active.toUpperCase());
String clientAddress = CloudCommonIdUtil.getClientIpPort(environment, hippo4JInetUtils);
String[] customerNetwork = DynamicThreadPoolServiceLoader.getSingletonServiceInstances(ClientNetworkService.class)
.stream().findFirst().map(network -> network.getNetworkIpPort(environment)).orElse(null);
String clientAddress;
if (customerNetwork != null) {
clientAddress = StringUtil.newBuilder(customerNetwork[0], ":", customerNetwork[1]);
} else {
clientAddress = CloudCommonIdUtil.getClientIpPort(environment, hippo4JInetUtils);
}
threadPoolState.setClientAddress(clientAddress);
threadPoolState.setIdentify(IdentifyUtil.getIdentify());
return threadPoolState;

Loading…
Cancel
Save