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

feat: Connect spring cloud properties refresh #4303

Closed

Conversation

gy09535
Copy link
Contributor

@gy09535 gy09535 commented Apr 7, 2022

What's the purpose of this PR

通过整合 Spring Cloud 来完成对如下内容更新:

  • 完成对于 @RefreshScope 的自动更新。
  • 完成对于 @ConfigurationProperties 配置类的自动更新。
  • 完成对于 日志的重新绑定。

参考:

Which issue(s) this PR fixes:

Fixes #4293

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Read the Contributing Guide before making this pull request.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit tests to verify the code.
  • Run mvn clean test to make sure this pull request doesn't break anything.
  • Update the CHANGES log.

@codecov-commenter
Copy link

codecov-commenter commented Apr 7, 2022

Codecov Report

Merging #4303 (0d5a357) into master (9b1cd8f) will not change coverage.
The diff coverage is n/a.

@@            Coverage Diff            @@
##             master    #4303   +/-   ##
=========================================
  Coverage     53.37%   53.37%           
+ Complexity     2684     2681    -3     
=========================================
  Files           490      490           
  Lines         15252    15252           
  Branches       1586     1586           
=========================================
  Hits           8141     8141           
- Misses         6552     6555    +3     
+ Partials        559      556    -3     
Impacted Files Coverage Δ
...framework/apollo/openapi/entity/ConsumerAudit.java 42.42% <0.00%> (-6.07%) ⬇️
...o/openapi/filter/ConsumerAuthenticationFilter.java 94.11% <0.00%> (-5.89%) ⬇️
...mework/apollo/openapi/service/ConsumerService.java 53.38% <0.00%> (-1.70%) ⬇️
...ework/apollo/internals/RemoteConfigRepository.java 88.34% <0.00%> (+3.06%) ⬆️

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 9b1cd8f...0d5a357. Read the comment docs.

ConfigurableListableBeanFactory beanFactory,
ApplicationContext applicationContext) {
super(environment, beanFactory);
this.applicationContext = applicationContext;
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to retrieve the RefreshScope bean here and cache it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it possible to retrieve the RefreshScope bean here and cache it?

fixed

private void adapterSpringCloud(ConfigChangeEvent changeEvent) {
RefreshScope refreshScope = applicationContext.getBean(RefreshScope.class);
if (refreshScope != null) {
refreshScope.refreshAll();
Copy link
Member

Choose a reason for hiding this comment

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

refreshAll is kind of a dangerous operation, it could cause unexpected behaviors, e.g. eureka service up and down. So I think we need to allow users to customize the change listener behaviors.

There was a config apollo.autoUpdateInjectedSpringProperties to control whether AutoUpdateConfigChangeListener is enabled or not. I think we need to also have two configs to control whether refreshScope.refreshAll and applicationContext.publishEvent are enabled respectively.

With that said, I would suggest we refactor the change listener logic instead of adding a new SpringCloudConfigPropertySourceProcessor

  1. Define a Service Provider Interface, e.g. SpringAutoUpdateConfigChangeListener
  2. Implement different implementations: AutoUpdateConfigChangeListener(the current one), SpringCloudRefreshScopeConfigChangeListener, SpringCloudEnviromentChangeEventConfigChangeListener
  3. Use ServiceBootstrap#loadAllOrdered method to load all the implementations in PropertySourcesProcessor#initializeAutoUpdatePropertiesFeature
  4. Write a CompositeConfigChagneListener to trigger each of the change listener
  5. In the onChange method, each listener will first check whether itself is enabled, then will do the actual work if enabled.

image

The benefit of this architecture is it's easy to add more customized config change listeners in the future.

If you like this idea, I think I could contribute to the refactoring part.

Copy link
Contributor Author

@gy09535 gy09535 Apr 8, 2022

Choose a reason for hiding this comment

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

Yeah, Sounds good, l prefer it. Maybe we can base this code and create another PR to do this.

Copy link
Member

Choose a reason for hiding this comment

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

I submitted #4305 to enable the spi mechanism, please help to take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I submitted #4305 to enable the spi mechanism, please help to take a look.

Got it.

Comment on lines 97 to 101
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<scope>provided</scope>
</dependency>
Copy link
Member

@nobodyiam nobodyiam Apr 8, 2022

Choose a reason for hiding this comment

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

I think optional is more appropriate and it's better to move it up with the spring-boot dependencies.

Suggested change
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<optional>true</optional>
</dependency>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think optional is more appropriate and it's better to move it up with the spring-boot dependencies.

fixed

@gy09535 gy09535 requested a review from nobodyiam April 8, 2022 02:14
@Anilople
Copy link
Contributor

Anilople commented Apr 8, 2022

Do we need to create a new maven module like spring-cloud-starter-apollo-client to implement this feature?

i.e let user choose client base on their runtime.

And the code will be more clear? i.e we know this feature only enable in spring cloud

@nobodyiam
Copy link
Member

Do we need to create a new maven module like spring-cloud-starter-apollo-client to implement this feature?

i.e let user choose client base on their runtime.

And the code will be more clear? i.e we know this feature only enable in spring cloud

@Anilople This is a good idea. I will do the refactoring mentioned in this thread in apollo-client module and I think the spring-cloud-starter-apollo-client module could then enable more spring cloud features based on the spi mechanism?

List<ConfigPropertySource> configPropertySources = configPropertySourceFactory.getAllConfigPropertySources();
for (ConfigPropertySource configPropertySource : configPropertySources) {
configPropertySource.addChangeListener(autoUpdateConfigChangeListener);
configPropertySource.addChangeListener(configChangeListener(this.environment, beanFactory));
Copy link
Member

Choose a reason for hiding this comment

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

why do we create a new config change listener for every configPropertySource?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why do we create a new config change listener for every configPropertySource?

fixed

private void adapterSpringCloud(ConfigChangeEvent changeEvent) {
RefreshScope refreshScope = applicationContext.getBean(RefreshScope.class);
if (refreshScope != null) {
refreshScope.refreshAll();
Copy link
Member

Choose a reason for hiding this comment

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

I submitted #4305 to enable the spi mechanism, please help to take a look.

@Anilople
Copy link
Contributor

Anilople commented Apr 9, 2022

Do we need to create a new maven module like spring-cloud-starter-apollo-client to implement this feature?
i.e let user choose client base on their runtime.
And the code will be more clear? i.e we know this feature only enable in spring cloud

@Anilople This is a good idea. I will do the refactoring mentioned in this thread in apollo-client module and I think the spring-cloud-starter-apollo-client module could then enable more spring cloud features based on the spi mechanism?

Spi sounds a good idea.

@stale
Copy link

stale bot commented May 15, 2022

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 14 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@stale stale bot added the stale label May 15, 2022
@nobodyiam
Copy link
Member

@gy09535 as apollo 2.0.0 is released, we may continue this pr to create a separate spring cloud client repository?

@stale stale bot removed the stale label May 15, 2022
@gy09535
Copy link
Contributor Author

gy09535 commented Jun 14, 2022

@gy09535 as apollo 2.0.0 is released, we may continue this pr to create a separate spring cloud client repository?
yeah, I will do it.

@gy09535 gy09535 changed the title feat: Connect spring cloud properties refresh WIP:feat: Connect spring cloud properties refresh Jun 14, 2022
@gy09535 gy09535 changed the title WIP:feat: Connect spring cloud properties refresh WIP(feat): Connect spring cloud properties refresh Jun 14, 2022
@nobodyiam
Copy link
Member

@gy09535 @Anilople

I'm thinking we could create a new repo under apolloconfig to host spring-cloud-apollo, how do you think?

@gy09535 gy09535 changed the title WIP(feat): Connect spring cloud properties refresh feat: Connect spring cloud properties refresh Jun 16, 2022
@gy09535 gy09535 closed this Jun 16, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

apollo 是否需要支持自动刷新配置类
4 participants