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);
}

@ -17,7 +17,6 @@
package cn.hippo4j.common.executor; package cn.hippo4j.common.executor;
import cn.hippo4j.common.toolkit.ReflectUtil; import cn.hippo4j.common.toolkit.ReflectUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;

@ -17,13 +17,18 @@
package cn.hippo4j.core.toolkit; package cn.hippo4j.core.toolkit;
import cn.hippo4j.common.api.ClientNetworkService;
import cn.hippo4j.common.config.ApplicationContextHolder; 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 cn.hippo4j.core.toolkit.inet.InetUtils;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import java.util.ArrayList; 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.GROUP_KEY_DELIMITER;
import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL; 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(); public static final String CLIENT_IDENTIFICATION_VALUE = IdUtil.simpleUUID();
static {
DynamicThreadPoolServiceLoader.register(ClientNetworkService.class);
}
/** /**
* Generate identify. * Generate identify.
* *
* @param environment * @param environment environment
* @param hippo4JInetUtils * @param inetUtil inet util
* @return * @return identify
*/ */
public static synchronized String generate(ConfigurableEnvironment environment, InetUtils hippo4JInetUtils) { public static synchronized String generate(ConfigurableEnvironment environment, InetUtils inetUtil) {
if (StringUtil.isNotBlank(IDENTIFY)) { if (StringUtil.isNotBlank(IDENTIFY)) {
return IDENTIFY; return IDENTIFY;
} }
String ip = hippo4JInetUtils.findFirstNonLoopBackHostInfo().getIpAddress(); String[] customerNetwork = DynamicThreadPoolServiceLoader.getSingletonServiceInstances(ClientNetworkService.class)
String port = environment.getProperty("server.port", "8080"); .stream().findFirst().map(each -> each.getNetworkIpPort(environment)).orElse(null);
String identification = new StringBuilder() 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(ip)
.append(":") .append(":")
.append(port) .append(port)
.append(IDENTIFY_SLICER_SYMBOL) .append(IDENTIFY_SLICER_SYMBOL)
.append(CLIENT_IDENTIFICATION_VALUE) .append(CLIENT_IDENTIFICATION_VALUE)
.toString(); .toString();
IDENTIFY = identification; IDENTIFY = identify;
return identification; return identify;
} }
/** /**
* Get identify. * Get identify.
* *
* @return * @return identify
*/ */
public static String getIdentify() { public static String getIdentify() {
while (StringUtil.isBlank(IDENTIFY)) { while (StringUtil.isBlank(IDENTIFY)) {
@ -82,10 +100,10 @@ public class IdentifyUtil {
/** /**
* Get thread pool identify. * Get thread pool identify.
* *
* @param threadPoolId * @param threadPoolId thread pool id
* @param itemId * @param itemId item id
* @param namespace * @param namespace namespace
* @return * @return identify
*/ */
public static String getThreadPoolIdentify(String threadPoolId, String itemId, String namespace) { public static String getThreadPoolIdentify(String threadPoolId, String itemId, String namespace) {
ArrayList<String> params = CollectionUtil.newArrayList(threadPoolId, itemId, namespace, getIdentify()); 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; package cn.hippo4j.springboot.starter.config;
import cn.hippo4j.common.api.ClientNetworkService;
import cn.hippo4j.common.model.InstanceInfo; import cn.hippo4j.common.model.InstanceInfo;
import cn.hippo4j.common.spi.DynamicThreadPoolServiceLoader;
import cn.hippo4j.common.toolkit.ContentUtil; import cn.hippo4j.common.toolkit.ContentUtil;
import cn.hippo4j.core.toolkit.IdentifyUtil; import cn.hippo4j.core.toolkit.IdentifyUtil;
import cn.hippo4j.core.toolkit.inet.InetUtils; 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.core.DiscoveryClient;
import cn.hippo4j.springboot.starter.remote.HttpAgent; import cn.hippo4j.springboot.starter.remote.HttpAgent;
import cn.hippo4j.springboot.starter.toolkit.CloudCommonIdUtil;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.Optional;
import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL; import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL;
import static cn.hippo4j.core.toolkit.IdentifyUtil.CLIENT_IDENTIFICATION_VALUE; import static cn.hippo4j.core.toolkit.IdentifyUtil.CLIENT_IDENTIFICATION_VALUE;
@ -46,6 +49,10 @@ public class DiscoveryConfiguration {
private final InetUtils hippo4JInetUtils; private final InetUtils hippo4JInetUtils;
static {
DynamicThreadPoolServiceLoader.register(ClientNetworkService.class);
}
@Bean @Bean
@SneakyThrows @SneakyThrows
public InstanceInfo instanceConfig() { public InstanceInfo instanceConfig() {
@ -69,8 +76,10 @@ public class DiscoveryConfiguration {
.setPort(port) .setPort(port)
.setClientBasePath(contextPath) .setClientBasePath(contextPath)
.setGroupKey(ContentUtil.getGroupKey(itemId, namespace)); .setGroupKey(ContentUtil.getGroupKey(itemId, namespace));
String callBackUrl = new StringBuilder().append(instanceInfo.getHostName()).append(":") String[] customerNetwork = DynamicThreadPoolServiceLoader.getSingletonServiceInstances(ClientNetworkService.class)
.append(port).append(instanceInfo.getClientBasePath()) .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(); .toString();
instanceInfo.setCallBackUrl(callBackUrl); instanceInfo.setCallBackUrl(callBackUrl);
String identify = IdentifyUtil.generate(environment, hippo4JInetUtils); 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.ThreadPoolAdapter;
import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter; import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter;
import cn.hippo4j.adapter.base.ThreadPoolAdapterState; 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.Result;
import cn.hippo4j.common.web.base.Results; import cn.hippo4j.common.web.base.Results;
import cn.hippo4j.core.toolkit.IdentifyUtil; import cn.hippo4j.core.toolkit.IdentifyUtil;
@ -56,7 +59,14 @@ public class ThreadPoolAdapterController {
ThreadPoolAdapterState threadPoolState = each.getThreadPoolState(requestParameter.getThreadPoolKey()); ThreadPoolAdapterState threadPoolState = each.getThreadPoolState(requestParameter.getThreadPoolKey());
String active = environment.getProperty("spring.profiles.active", "UNKNOWN"); String active = environment.getProperty("spring.profiles.active", "UNKNOWN");
threadPoolState.setActive(active.toUpperCase()); 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.setClientAddress(clientAddress);
threadPoolState.setIdentify(IdentifyUtil.getIdentify()); threadPoolState.setIdentify(IdentifyUtil.getIdentify());
return threadPoolState; return threadPoolState;

Loading…
Cancel
Save