dragon-zhang commented on code in PR #4001:
URL: https://github.com/apache/shenyu/pull/4001#discussion_r980657073
##########
shenyu-client/shenyu-client-http/shenyu-client-springcloud/src/main/java/org/apache/shenyu/client/springcloud/init/SpringCloudClientEventListener.java:
##########
@@ -56,94 +55,94 @@
/**
* The type Spring cloud client event listener.
*/
-public class SpringCloudClientEventListener implements
ApplicationListener<ContextRefreshedEvent> {
-
- /**
- * api path separator.
- */
- private static final String PATH_SEPARATOR = "/";
-
- private static final Logger LOG =
LoggerFactory.getLogger(SpringCloudClientEventListener.class);
-
- private final ShenyuClientRegisterEventPublisher publisher =
ShenyuClientRegisterEventPublisher.getInstance();
-
- private final String contextPath;
-
+public class SpringCloudClientEventListener extends
AbstractContextRefreshedEventListener<Object, ShenyuSpringCloudClient> {
+
private final Boolean isFull;
private final Environment env;
private final String servletContextPath;
- private final List<Class<? extends Annotation>> mappingAnnotation = new
ArrayList<>(7);
-
- private final String[] pathAttributeNames = new String[]{"path", "value"};
-
+ private final List<Class<? extends Annotation>> mappingAnnotation = new
ArrayList<>(3);
+
/**
* Instantiates a new Spring cloud client bean post processor.
*
- * @param clientConfig the client config
- * @param shenyuClientRegisterRepository the shenyu client register
repository
+ * @param clientConfig the shenyu client config
+ * @param shenyuClientRegisterRepository the shenyuClientRegisterRepository
* @param env the env
*/
public SpringCloudClientEventListener(final PropertiesConfig clientConfig,
final ShenyuClientRegisterRepository
shenyuClientRegisterRepository,
final Environment env) {
- String appName = env.getProperty("spring.application.name");
+ super(clientConfig, shenyuClientRegisterRepository);
Properties props = clientConfig.getProps();
- this.contextPath =
Optional.ofNullable(props.getProperty(ShenyuClientConstants.CONTEXT_PATH)).map(UriUtils::repairData).orElse(null);
- if (StringUtils.isBlank(appName)) {
+ this.env = env;
+ if (StringUtils.isBlank(getAppName())) {
String errorMsg = "spring cloud param must config the appName";
LOG.error(errorMsg);
throw new ShenyuClientIllegalArgumentException(errorMsg);
}
- this.env = env;
this.isFull =
Boolean.parseBoolean(props.getProperty(ShenyuClientConstants.IS_FULL,
Boolean.FALSE.toString()));
this.servletContextPath =
env.getProperty("server.servlet.context-path", "");
mappingAnnotation.add(ShenyuSpringCloudClient.class);
mappingAnnotation.add(RequestMapping.class);
- publisher.start(shenyuClientRegisterRepository);
}
@Override
- public void onApplicationEvent(final ContextRefreshedEvent
contextRefreshedEvent) {
+ protected Map<String, Object> getBeans(final ApplicationContext context) {
// Filter out is not controller out
if (Boolean.TRUE.equals(isFull)) {
- return;
- }
- Map<String, Object> beans =
contextRefreshedEvent.getApplicationContext().getBeansWithAnnotation(Controller.class);
- for (Map.Entry<String, Object> entry : beans.entrySet()) {
- handler(entry.getValue());
+ getPublisher().publishEvent(MetaDataRegisterDTO.builder()
+ .contextPath(getContextPath())
+ .appName(getAppName())
+ .path(PathUtils.decoratorPathWithSlash(getContextPath()))
+ .rpcType(RpcTypeEnum.SPRING_CLOUD.getName())
+ .enabled(true)
+ .ruleName(getContextPath())
+ .build());
+ return null;
}
+ return context.getBeansWithAnnotation(Controller.class);
}
- private void handler(final Object bean) {
- Class<?> clazz = bean.getClass();
- if (AopUtils.isAopProxy(bean)) {
- clazz = AopUtils.getTargetClass(bean);
- }
- final ShenyuSpringCloudClient beanShenyuClient =
AnnotatedElementUtils.findMergedAnnotation(clazz,
ShenyuSpringCloudClient.class);
- final String superPath = buildApiSuperPath(clazz, beanShenyuClient);
- // Compatible with previous versions
- if (Objects.nonNull(beanShenyuClient) && superPath.contains("*")) {
- publisher.publishEvent(buildMetaDataDTO(beanShenyuClient,
pathJoin(contextPath, superPath), clazz, null));
- return;
+ @Override
+ protected URIRegisterDTO buildURIRegisterDTO(final ApplicationContext
context, final Map<String, Object> beans) {
+ try {
+ final String host = getHost();
+ final int port =
Integer.parseInt(Optional.ofNullable(getPort()).orElseGet(() -> "-1"));
+ final int mergedPort = port <= 0 ?
PortUtils.findPort(context.getAutowireCapableBeanFactory()) : port;
+ return URIRegisterDTO.builder()
+ .contextPath(getContextPath())
+ .appName(getAppName())
+ .host(IpUtils.isCompleteHost(host) ? host :
IpUtils.getHost(host))
+ .port(mergedPort)
+ .rpcType(RpcTypeEnum.SPRING_CLOUD.getName())
+ .build();
+ } catch (ShenyuException e) {
+ throw new ShenyuException(e.getMessage() + "please config
${shenyu.client.http.props.port} in xml/yml !");
}
- final Method[] methods =
ReflectionUtils.getUniqueDeclaredMethods(clazz);
- for (Method method : methods) {
- final RequestMapping requestMapping =
AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
- ShenyuSpringCloudClient methodShenyuClient =
AnnotatedElementUtils.findMergedAnnotation(method,
ShenyuSpringCloudClient.class);
- methodShenyuClient = Objects.isNull(methodShenyuClient) ?
beanShenyuClient : methodShenyuClient;
- // the result of ReflectionUtils#getUniqueDeclaredMethods contains
methods such as hashCode, wait, toSting
- // add Objects.nonNull(requestMapping) to make sure not register
wrong method
- if (Objects.nonNull(methodShenyuClient) &&
Objects.nonNull(requestMapping)) {
- publisher.publishEvent(buildMetaDataDTO(methodShenyuClient,
buildApiPath(method, superPath, methodShenyuClient), clazz, method));
- }
+ }
+
+ @Override
+ protected void handleMethod(final Object bean, final Class<?> clazz,
+ @Nullable final ShenyuSpringCloudClient
beanShenyuClient, final Method method,
+ final String superPath) {
+ final RequestMapping requestMapping =
AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
+ ShenyuSpringCloudClient methodShenyuClient =
AnnotatedElementUtils.findMergedAnnotation(method,
ShenyuSpringCloudClient.class);
+ methodShenyuClient = Objects.isNull(methodShenyuClient) ?
beanShenyuClient : methodShenyuClient;
+ // the result of ReflectionUtils#getUniqueDeclaredMethods contains
methods such as hashCode, wait, toSting
+ // add Objects.nonNull(requestMapping) to make sure not register wrong
method
+ if (Objects.nonNull(methodShenyuClient) &&
Objects.nonNull(requestMapping)) {
+ getPublisher().publishEvent(buildMetaDataDTO(bean,
methodShenyuClient,
+ buildApiPath(method, superPath, methodShenyuClient),
clazz, method));
}
}
-
- private String buildApiPath(@NonNull final Method method, @NonNull final
String superPath, final ShenyuSpringCloudClient methodShenyuClient) {
- if (Objects.nonNull(methodShenyuClient) &&
StringUtils.isNotBlank(methodShenyuClient.path())) {
+
+ protected String buildApiPath(final Method method, final String superPath,
Review Comment:
missing `@Override`
##########
shenyu-client/shenyu-client-http/shenyu-client-springcloud/src/main/java/org/apache/shenyu/client/springcloud/init/SpringCloudClientEventListener.java:
##########
@@ -156,77 +155,71 @@ private String buildApiPath(@NonNull final Method method,
@NonNull final String
private String getPathByMethod(@NonNull final Method method) {
for (Class<? extends Annotation> mapping : mappingAnnotation) {
final Annotation mergedAnnotation =
AnnotatedElementUtils.findMergedAnnotation(method, mapping);
- final String pathByAnnotation =
getPathByAnnotation(mergedAnnotation, pathAttributeNames);
+ final String pathByAnnotation =
getPathByAnnotation(mergedAnnotation);
if (StringUtils.isNotBlank(pathByAnnotation)) {
return pathByAnnotation;
}
}
return null;
}
- private String getPathByAnnotation(@Nullable final Annotation annotation,
@NonNull final String... pathAttributeName) {
+ private String getPathByAnnotation(@Nullable final Annotation annotation) {
if (Objects.isNull(annotation)) {
return null;
}
- for (String s : pathAttributeName) {
- final Object value = AnnotationUtils.getValue(annotation, s);
- if (value instanceof String && StringUtils.isNotBlank((String)
value)) {
- return (String) value;
- }
- // Only the first path is supported temporarily
- if (value instanceof String[] && ArrayUtils.isNotEmpty((String[])
value) && StringUtils.isNotBlank(((String[]) value)[0])) {
- return ((String[]) value)[0];
- }
+ final Object value = AnnotationUtils.getValue(annotation, "value");
+ if (value instanceof String && StringUtils.isNotBlank((String) value))
{
+ return (String) value;
+ }
+ if (value instanceof String[] && ArrayUtils.isNotEmpty((String[])
value)
+ && StringUtils.isNotBlank(((String[]) value)[0])) {
+ return ((String[]) value)[0];
}
return null;
}
- private String buildApiSuperPath(@NonNull final Class<?> clazz, final
ShenyuSpringCloudClient beanShenyuClient) {
+ protected String buildApiSuperPath(final Class<?> clazz, @Nullable final
ShenyuSpringCloudClient beanShenyuClient) {
Review Comment:
missing `@Override`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]