Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.0] dubbo-spring-boot-actuator compatible with Spring Boot Actuator 2.6.x #9426

Merged
merged 1 commit into from Dec 17, 2021
Merged

[3.0] dubbo-spring-boot-actuator compatible with Spring Boot Actuator 2.6.x #9426

merged 1 commit into from Dec 17, 2021

Conversation

gitchenjh
Copy link
Contributor

@gitchenjh gitchenjh commented Dec 16, 2021

What is the purpose of the change

fix #9394

Brief changelog

CompatibleOnEnabledEndpointCondition only handle org.springframework.boot.actuate.autoconfigure.endpoint.condition.OnEnabledEndpointCondition (Spring Boot 2.0.0 ~ 2.2.x)
Endpint beans add @ConditionalOnAvailableEndpoint derectly (Spring Boot 2.2x +)

Verifying this change

you can verify by this case: https://github.com/gitchenjh/dubbo-test

Checklist

  • Make sure there is a GitHub_issue field for the change (usually before you start working on it). Trivial changes like typos do not require a GitHub issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Check if is necessary to patch to Dubbo 3 if you are work on Dubbo 2.7
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add sample in dubbo samples project.
  • Add some description to dubbo-website project if you are requesting to add a feature.
  • GitHub Actions works fine on your own branch.
  • If this contribution is large, please follow the Software Donation Guide.

@@ -45,41 +46,47 @@

@Bean
@ConditionalOnMissingBean
@ConditionalOnAvailableEndpoint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ConditionalOnAvailableEndpoint is available sine Spring Boot 2.2.0, see https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/actuate/autoconfigure/endpoint/condition/ConditionalOnAvailableEndpoint.html
If we reference @ConditionalOnAvailableEndpoint directly, it may cause loading failed on Spring Boot version < 2.2.0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems missing annotation class will not cause ClassNotFoundException, please check it on Spring Boot version < 2.2.0.

.map(Condition.class::cast) // Cast the instance to be Condition one
.orElse(NegativeCondition.INSTANCE); // Or else get a negative condition

return condition.matches(context, metadata);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try fix error of condition.matches(context, metadata) of OnAvailableEndpointCondition by add required attributes or re-construct the metadata, to compatible with spring boot 2.2.0+ and 2.6.1+.

//OnAvailableEndpointCondition of Spring Boot 2.6.1

	@Override
	public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
		Environment environment = context.getEnvironment();
		MergedAnnotation<ConditionalOnAvailableEndpoint> conditionAnnotation = metadata.getAnnotations()
				.get(ConditionalOnAvailableEndpoint.class);
		Class<?> target = getTarget(context, metadata, conditionAnnotation);
		MergedAnnotation<Endpoint> endpointAnnotation = getEndpointAnnotation(target);
		return getMatchOutcome(environment, conditionAnnotation, endpointAnnotation);
	}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gitchenjh If it can work in Spring Boot <2.2.0, it is more compatible to use ConditionalOnAvailableEndpoint directly. The only problem is that the code is a bit difficult to understand.

Comment on lines 51 to +62
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
ClassLoader classLoader = context.getClassLoader();

Condition condition = Stream.of(CONDITION_CLASS_NAMES) // Iterate class names
.filter(className -> ClassUtils.isPresent(className, classLoader)) // Search class existing or not by name
.findFirst() // Find the first candidate
.map(className -> ClassUtils.resolveClassName(className, classLoader)) // Resolve class name to Class
.filter(Condition.class::isAssignableFrom) // Accept the Condition implementation
.map(BeanUtils::instantiateClass) // Instantiate Class to be instance
.map(Condition.class::cast) // Cast the instance to be Condition one
.orElse(NegativeCondition.INSTANCE); // Or else get a negative condition

return condition.matches(context, metadata);
}

private static class NegativeCondition implements Condition {

static final NegativeCondition INSTANCE = new NegativeCondition();

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return false;
if (ClassUtils.isPresent(CONDITION_CLASS_NAME_OLD, classLoader)) {
Class<?> cls = ClassUtils.resolveClassName(CONDITION_CLASS_NAME_OLD, classLoader);
if (Condition.class.isAssignableFrom(cls)) {
Condition condition = Condition.class.cast(BeanUtils.instantiateClass(cls));
return condition.matches(context, metadata);
}
}
// Check by org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint
if (ClassUtils.isPresent(CONDITION_CLASS_NAME_NEW, classLoader)) {
return true;
Copy link
Member

@kylixs kylixs Dec 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good. The CompatibleOnEnabledEndpointCondition only delegate OnEnabledEndpointCondition, and do nothing with OnAvailableEndpointCondition. In this case, it looks clearer.

Copy link
Contributor Author

@gitchenjh gitchenjh Dec 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is CompatibleOnEnabledEndpointCondition#matches(context, metadata) return true if OnEnabledEndpointCondition class were not found.
But I'm not sure is there a possibility that both OnEnabledEndpointCondition and OnAvailableEndpointCondition class were not found(eg. Spring Boot 1) . So I let this method return false by default, if OnAvailableEndpointCondition class were found, then return true, and @ConditionalOnAvailableEndpoint should work

@codecov-commenter
Copy link

codecov-commenter commented Dec 16, 2021

Codecov Report

Merging #9426 (e6d4813) into 3.0 (dc8ba01) will increase coverage by 0.04%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff              @@
##                3.0    #9426      +/-   ##
============================================
+ Coverage     65.21%   65.26%   +0.04%     
- Complexity      341      342       +1     
============================================
  Files          1202     1202              
  Lines         51950    51950              
  Branches       7739     7739              
============================================
+ Hits          33880    33903      +23     
+ Misses        14434    14415      -19     
+ Partials       3636     3632       -4     
Impacted Files Coverage Δ
...he/dubbo/remoting/transport/netty/NettyServer.java 70.17% <0.00%> (-3.51%) ⬇️
...rg/apache/dubbo/common/timer/HashedWheelTimer.java 85.22% <0.00%> (-0.69%) ⬇️
...n/java/org/apache/dubbo/common/utils/NetUtils.java 69.35% <0.00%> (+0.32%) ⬆️
...ubbo/config/deploy/DefaultApplicationDeployer.java 73.64% <0.00%> (+0.37%) ⬆️
...ting/zookeeper/curator/CuratorZookeeperClient.java 68.75% <0.00%> (+0.96%) ⬆️
...bo/registry/support/CacheableFailbackRegistry.java 78.31% <0.00%> (+1.20%) ⬆️
...bbo/common/resource/GlobalResourcesRepository.java 72.72% <0.00%> (+1.81%) ⬆️
...a/org/apache/dubbo/monitor/dubbo/DubboMonitor.java 90.36% <0.00%> (+2.40%) ⬆️
...pache/dubbo/registry/support/AbstractRegistry.java 76.59% <0.00%> (+2.48%) ⬆️
...org/apache/dubbo/rpc/protocol/AbstractInvoker.java 71.96% <0.00%> (+2.80%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc8ba01...e6d4813. Read the comment docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dubbo 2.7.14 不兼容spring boot 2.6.1
3 participants