
com.taotao.cloud.openfeign.endpoint.FeignClientEndpoint Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taotao-cloud-starter-openfeign Show documentation
Show all versions of taotao-cloud-starter-openfeign Show documentation
taotao-cloud-starter-openfeign
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* 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
*
* https://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 com.taotao.cloud.openfeign.endpoint;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/** Feign client 端点 */
@Endpoint(id = "ttc-feign-clients")
public class FeignClientEndpoint implements SmartInitializingSingleton {
private final ApplicationContext context;
private final List clientList;
public FeignClientEndpoint(ApplicationContext context) {
this.context = context;
this.clientList = new ArrayList<>();
}
@ReadOperation
public List invoke() {
return clientList;
}
@Override
public void afterSingletonsInstantiated() {
clientList.addAll(getClientList(context));
}
private static List getClientList(ApplicationContext context) {
Map feignClientMap = context.getBeansWithAnnotation(FeignClient.class);
// 1. 解析注解
List feignClientInfoList = new ArrayList<>();
Set> feignClientEntrySet = feignClientMap.entrySet();
for (Map.Entry feignClientEntry : feignClientEntrySet) {
String beanName = feignClientEntry.getKey();
Object feignClientBean = feignClientEntry.getValue();
if (feignClientBean == null) {
continue;
}
// 解析注解
Class> feignClientClass = feignClientBean.getClass();
FeignClient feignClientAnn = AnnotationUtils.findAnnotation(feignClientClass, FeignClient.class);
if (feignClientAnn == null) {
continue;
}
FeignClientInfo feignClientInfo = new FeignClientInfo();
feignClientInfo.setBeanName(beanName);
String serviceId = feignClientAnn.value();
String contextId = feignClientAnn.contextId();
String url = feignClientAnn.url();
String path = feignClientAnn.path();
feignClientInfo.setServiceId(serviceId);
feignClientInfo.setContextId(contextId);
feignClientInfo.setUrl(url);
feignClientInfo.setPath(path);
// 组装客户端信息
List clientInfoList = new ArrayList<>();
Class>[] interfaces = feignClientClass.getInterfaces();
for (Class> clientInterface : interfaces) {
Method[] methods = clientInterface.getDeclaredMethods();
for (Method method : methods) {
if (method.isDefault()) {
continue;
}
RequestMapping requestMapping =
AnnotatedElementUtils.getMergedAnnotation(method, RequestMapping.class);
if (requestMapping == null) {
continue;
}
clientInfoList.add(new ClientInfo(requestMapping.method(), requestMapping.value()));
}
}
feignClientInfo.setClientList(clientInfoList);
feignClientInfoList.add(feignClientInfo);
}
return feignClientInfoList;
}
public static class FeignClientInfo {
private String beanName;
private String serviceId;
private String contextId;
private String url;
private String path;
private List clientList;
public String getBeanName() {
return beanName;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public String getServiceId() {
return serviceId;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
public String getContextId() {
return contextId;
}
public void setContextId(String contextId) {
this.contextId = contextId;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public List getClientList() {
return clientList;
}
public void setClientList(List clientList) {
this.clientList = clientList;
}
}
public static class ClientInfo {
private final RequestMethod[] methods;
private final String[] mappings;
public ClientInfo(RequestMethod[] methods, String[] mappings) {
this.methods = methods;
this.mappings = mappings;
}
public RequestMethod[] getMethods() {
return methods;
}
public String[] getMappings() {
return mappings;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy