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
54 changes: 54 additions & 0 deletions apollo-client-spring-cloud-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2022 Apollo 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.
~
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>Apollo Client Spring Cloud Starter</name>
<artifactId>spring-cloud-starter-apollo-client</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<scope>provided</scope>
</dependency>
<!-- spring cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<optional>true</optional>
</dependency>
<!-- end of spring cloud -->
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2022 Apollo 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 com.ctrip.framework.apollo.spring.cloud;

import com.ctrip.framework.apollo.spring.cloud.listener.SpringCloudEnvironmentChangeEventConfigChangeListener;
import com.ctrip.framework.apollo.spring.cloud.listener.SpringCloudRefreshScopeConfigChangeListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class ApolloClientSpringCloudAutoConfiguration {

@Bean
@ConditionalOnClass(name = {"org.springframework.cloud.context.scope.refresh.RefreshScope"})
public SpringCloudRefreshScopeConfigChangeListener springCloudConfigPropertySourceProcessor() {
return new SpringCloudRefreshScopeConfigChangeListener();
}

@Bean
@ConditionalOnClass(name = {"org.springframework.cloud.context.environment.EnvironmentChangeEvent"})
public SpringCloudEnvironmentChangeEventConfigChangeListener springCloudEnvironmentChangeEventConfigChangeListener(){
return new SpringCloudEnvironmentChangeEventConfigChangeListener();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 Apollo 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 com.ctrip.framework.apollo.spring.cloud.listener;

import com.ctrip.framework.apollo.spring.events.ApolloConfigChangeEvent;
import org.springframework.beans.BeansException;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;

public class SpringCloudEnvironmentChangeEventConfigChangeListener implements ApplicationListener<ApolloConfigChangeEvent>, ApplicationContextAware {

private ApplicationContext applicationContext;

@Override
public void onApplicationEvent(ApolloConfigChangeEvent event) {
if (event.getConfigChangeEvent() != null && !event.getConfigChangeEvent().changedKeys().isEmpty()) {
applicationContext.publishEvent(new EnvironmentChangeEvent(event.getConfigChangeEvent().changedKeys()));
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 Apollo 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 com.ctrip.framework.apollo.spring.cloud.listener;

import com.ctrip.framework.apollo.spring.events.ApolloConfigChangeEvent;
import org.springframework.beans.BeansException;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;

public class SpringCloudRefreshScopeConfigChangeListener implements ApplicationListener<ApolloConfigChangeEvent>, ApplicationContextAware {

private RefreshScope refreshScope;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
refreshScope = applicationContext.getBean(RefreshScope.class);
}

@Override
public void onApplicationEvent(ApolloConfigChangeEvent event) {
if (refreshScope != null) {
refreshScope.refreshAll();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ctrip.framework.apollo.spring.cloud.ApolloClientSpringCloudAutoConfiguration
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<module>apollo-demo</module>
<module>apollo-mockserver</module>
<module>apollo-openapi</module>
<module>apollo-client-spring-cloud-starter</module>
</modules>

<dependencyManagement>
Expand Down