Browse Source

Fix resolving url. Add tests. Add docs.

property-based-url-native
Olga Maciaszek-Sharma 2 years ago
parent
commit
ad566f143c
  1. 1
      docs/src/main/asciidoc/spring-cloud-openfeign.adoc
  2. 5
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java
  3. 13
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java
  4. 5
      spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/NonRefreshableFeignClientUrlTests.java

1
docs/src/main/asciidoc/spring-cloud-openfeign.adoc

@ -925,6 +925,7 @@ TIP: If you want to run Spring Cloud OpenFeign clients in AOT or native image mo @@ -925,6 +925,7 @@ TIP: If you want to run Spring Cloud OpenFeign clients in AOT or native image mo
TIP: If you want to run Spring Cloud OpenFeign clients in AOT or native image modes, ensure `spring.cloud.openfeign.lazy-attributes-resolution` has not been set to `true`.
TIP: However, if you set the `url` value via properties, it is possible to override the `@FeignClient` `url` value by running the image with `-Dspring.cloud.openfeign.client.config.[clientId].url=[url]` flag. In order to enable overriding, a `url` value also has to be set via properties and not `@FeignClient` attribute during buildtime.
== Configuration properties

5
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -505,10 +505,7 @@ public class FeignClientFactoryBean @@ -505,10 +505,7 @@ public class FeignClientFactoryBean
"Provide Feign client URL either in @FeignClient() or in config properties.");
}
// TODO: create the appropriate target
return new PropertyBasedTarget(type, name, config);
// return new HardCodedTarget(type, name,
// FeignClientsRegistrar.getUrl(config.getUrl()));
}
private boolean isUrlAvailableInConfig(String contextId) {

13
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java

@ -19,10 +19,18 @@ package org.springframework.cloud.openfeign; @@ -19,10 +19,18 @@ package org.springframework.cloud.openfeign;
import feign.Target;
/**
* A {@link HardCodedTarget} implementation that resolves url from properties when the
* initial call is made. Using it allows specifying the url at runtime in an AOT-packaged
* application or a native image by setting the value of the
* `spring.cloud.openfeign.client.config.[clientId].url`.
*
* @author Olga Maciaszek-Sharma
* @see FeignClientProperties.FeignClientConfiguration#getUrl()
*/
public class PropertyBasedTarget<T> extends Target.HardCodedTarget<T> {
private String url;
private final FeignClientProperties.FeignClientConfiguration config;
public PropertyBasedTarget(Class<T> type, String name, FeignClientProperties.FeignClientConfiguration config) {
@ -32,7 +40,10 @@ public class PropertyBasedTarget<T> extends Target.HardCodedTarget<T> { @@ -32,7 +40,10 @@ public class PropertyBasedTarget<T> extends Target.HardCodedTarget<T> {
@Override
public String url() {
return config.getUrl();
if (url == null) {
url = config.getUrl();
}
return url;
}
}

5
spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/NonRefreshableFeignClientUrlTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -33,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -33,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Jasbir Singh
* @author Olga Maciaszek-Sharma
*/
@SpringBootTest
@TestPropertySource("classpath:feign-properties.properties")
@ -65,7 +66,7 @@ class NonRefreshableFeignClientUrlTests { @@ -65,7 +66,7 @@ class NonRefreshableFeignClientUrlTests {
public void shouldInstantiateFeignClientWhenUrlFromProperties() {
UrlTestClient.UrlResponseForTests response = configBasedClient.test();
assertThat(response.getUrl()).isEqualTo("http://localhost:9999/test");
assertThat(response.getTargetType()).isEqualTo(Target.HardCodedTarget.class);
assertThat(response.getTargetType()).isEqualTo(PropertyBasedTarget.class);
}
@Test

Loading…
Cancel
Save