From 9b8d93e211142bf12140e14a9908a009471e66cf Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Wed, 8 Feb 2023 15:24:33 +0100 Subject: [PATCH 1/2] Add POC. --- .../openfeign/FeignClientFactoryBean.java | 5 ++- .../cloud/openfeign/PropertyBasedTarget.java | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java index 470676c06..b17408a8f 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java @@ -505,7 +505,10 @@ private HardCodedTarget resolveTarget(FeignClientFactory context, String "Provide Feign client URL either in @FeignClient() or in config properties."); } - return new HardCodedTarget(type, name, FeignClientsRegistrar.getUrl(config.getUrl())); + // 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) { diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java new file mode 100644 index 000000000..18ad8778a --- /dev/null +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java @@ -0,0 +1,38 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * https://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 org.springframework.cloud.openfeign; + +import feign.Target; + +/** + * @author Olga Maciaszek-Sharma + */ +public class PropertyBasedTarget extends Target.HardCodedTarget { + + private final FeignClientProperties.FeignClientConfiguration config; + + public PropertyBasedTarget(Class type, String name, FeignClientProperties.FeignClientConfiguration config) { + super(type, name, config.getUrl()); + this.config = config; + } + + @Override + public String url() { + return config.getUrl(); + } + +} From ad566f143c3306d941f81a8c9f3fe3111b42a63a Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Mon, 13 Feb 2023 16:11:26 +0100 Subject: [PATCH 2/2] Fix resolving url. Add tests. Add docs. --- docs/src/main/asciidoc/spring-cloud-openfeign.adoc | 1 + .../cloud/openfeign/FeignClientFactoryBean.java | 5 +---- .../cloud/openfeign/PropertyBasedTarget.java | 13 ++++++++++++- .../NonRefreshableFeignClientUrlTests.java | 5 +++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-openfeign.adoc b/docs/src/main/asciidoc/spring-cloud-openfeign.adoc index 12a895716..8e69bb766 100644 --- a/docs/src/main/asciidoc/spring-cloud-openfeign.adoc +++ b/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 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 diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java index b17408a8f..5ae1c6e0c 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientFactoryBean.java @@ -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 @@ private HardCodedTarget resolveTarget(FeignClientFactory context, String "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) { diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java index 18ad8778a..dd7b35ce2 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/PropertyBasedTarget.java @@ -19,10 +19,18 @@ 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 extends Target.HardCodedTarget { + private String url; + private final FeignClientProperties.FeignClientConfiguration config; public PropertyBasedTarget(Class type, String name, FeignClientProperties.FeignClientConfiguration config) { @@ -32,7 +40,10 @@ public PropertyBasedTarget(Class type, String name, FeignClientProperties.Fei @Override public String url() { - return config.getUrl(); + if (url == null) { + url = config.getUrl(); + } + return url; } } diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/NonRefreshableFeignClientUrlTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/NonRefreshableFeignClientUrlTests.java index 171567f47..1670e6355 100644 --- a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/NonRefreshableFeignClientUrlTests.java +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/NonRefreshableFeignClientUrlTests.java @@ -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 @@ /** * @author Jasbir Singh + * @author Olga Maciaszek-Sharma */ @SpringBootTest @TestPropertySource("classpath:feign-properties.properties") @@ -65,7 +66,7 @@ void shouldInstantiateFeignClientWhenUrlFromFeignClientUrlGivenPreferenceOverPro 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