org.cattleframework.cloud.discovery.reflect.DiscoveryClientInvocationHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cattle-cloud-discovery-common Show documentation
Show all versions of cattle-cloud-discovery-common Show documentation
Cattle framework cloud discovery common component pom
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed 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 org.cattleframework.cloud.discovery.reflect;
import java.lang.reflect.Method;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.cattleframework.cloud.discovery.event.DiscoveryClientEvent;
import org.cattleframework.utils.reflect.ReflectUtils;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import com.google.common.reflect.AbstractInvocationHandler;
/**
* 服务发现客户端调用处理类
*
* @author orange
*
*/
public class DiscoveryClientInvocationHandler extends AbstractInvocationHandler {
private static final Logger logger = LoggerFactory.getLogger(DiscoveryClientInvocationHandler.class);
private static final String METHOD_NAME_GET_INSTANCES = "getInstances";
private final DiscoveryClient target;
private final List discoveryClientEvents;
public DiscoveryClientInvocationHandler(DiscoveryClient target, List discoveryClientEvents) {
this.target = target;
this.discoveryClientEvents = discoveryClientEvents;
}
@SuppressWarnings("unchecked")
@Override
protected Object handleInvocation(Object proxy, Method method, @Nullable Object[] args) throws Throwable {
Object result = ReflectUtils.invokeObjectMethod(method, target, args);
String methodName = method.getName();
if (METHOD_NAME_GET_INSTANCES.equals(methodName) && CollectionUtils.isNotEmpty(discoveryClientEvents)) {
String serviceId = (String) args[0];
List serviceInstances = (List) result;
discoveryClientEvents.forEach(discoveryClientEvent -> {
logger.debug("执行服务发现客户端事件类:{}", discoveryClientEvent.getClass().getName());
discoveryClientEvent.onGetInstances(serviceId, serviceInstances);
});
return serviceInstances;
}
return result;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy