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

Dynamically set the url of FeignClient through config #1126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.StandardServletEnvironment;

/**
* @author Spencer Gibb
Expand All @@ -64,6 +67,10 @@ class FeignClientsRegistrar

private Environment environment;

private static final String PREFIX = "appoint.";

private static final String SUFFIX = ".url";

FeignClientsRegistrar() {
}

Expand Down Expand Up @@ -209,9 +216,9 @@ private void registerFeignClient(BeanDefinitionRegistry registry,
BeanDefinitionBuilder definition = BeanDefinitionBuilder
.genericBeanDefinition(FeignClientFactoryBean.class);
validate(attributes);
definition.addPropertyValue("url", getUrl(attributes));
definition.addPropertyValue("path", getPath(attributes));
String name = getName(attributes);
definition.addPropertyValue("url", getUrl(attributes, name));
definition.addPropertyValue("path", getPath(attributes));
definition.addPropertyValue("name", name);
String contextId = getContextId(attributes);
definition.addPropertyValue("contextId", contextId);
Expand Down Expand Up @@ -282,6 +289,28 @@ private String getUrl(Map<String, Object> attributes) {
return getUrl(url);
}

private String getUrl(Map<String, Object> attributes, String name) {
String realName = PREFIX + name + SUFFIX;
String url = resolve((String) attributes.get("url"));
Map<String, Object> propertyMap;
if (!StringUtils.hasText(url)) {
String[] activeProfiles = this.environment.getActiveProfiles();
String config = "applicationConfig: [classpath:/bootstrap-"
+ activeProfiles[0] + ".yml]";
MutablePropertySources propertySources = ((StandardServletEnvironment) this.environment)
.getPropertySources();
PropertySource<?> propertySource = propertySources.stream()
.filter(p -> p.getName().equals(config)).findFirst().orElse(null);
if (propertySource != null) {
propertyMap = (Map<String, Object>) propertySource.getSource();
if (propertyMap.containsKey(realName)) {
url = propertyMap.get(realName).toString();
}
}
}
return getUrl(url);
}

private String getPath(Map<String, Object> attributes) {
String path = resolve((String) attributes.get("path"));
return getPath(path);
Expand Down
12 changes: 12 additions & 0 deletions spring-cloud-openfeign-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>spring</id>
Expand Down