From bf1cc4d2701256306878447358404ef6ce4f6923 Mon Sep 17 00:00:00 2001 From: "wenqi.huang" Date: Thu, 11 Apr 2019 17:19:50 +0800 Subject: [PATCH 1/5] Give users a choice to determine whether the EurekaClient instance can be refreshed or not. --- .../netflix/eureka/EurekaClientAutoConfiguration.java | 5 +++++ .../additional-spring-configuration-metadata.json | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java index 56e5c2681..484d1d0fc 100644 --- a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java +++ b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java @@ -259,6 +259,7 @@ public class EurekaClientAutoConfiguration { @Documented @ConditionalOnClass(RefreshScope.class) @ConditionalOnBean(RefreshAutoConfiguration.class) + @ConditionalOnProperty(value = "eureka.client.refresh.enable", havingValue = "true", matchIfMissing = true) @interface ConditionalOnRefreshScope { } @@ -376,6 +377,10 @@ public class EurekaClientAutoConfiguration { static class MissingScope { } + @ConditionalOnProperty(value = "eureka.client.refresh.enable", havingValue = "false") + static class OnPropertyDisabled { + } + } @Configuration diff --git a/spring-cloud-netflix-eureka-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-netflix-eureka-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json index aea1bb649..90a7b97b9 100644 --- a/spring-cloud-netflix-eureka-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-netflix-eureka-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -6,6 +6,12 @@ "description": "Enables the Eureka health check handler.", "type": "java.lang.Boolean" }, + { + "defaultValue": true, + "name": "eureka.client.refresh.enable", + "description": "Determines whether EurekaClient instance can be refreshed or not.", + "type": "java.lang.Boolean" + }, { "defaultValue": true, "name": "ribbon.eureka.enabled", @@ -13,4 +19,4 @@ "type": "java.lang.Boolean" } ] -} \ No newline at end of file +} From ffebfebe317c253ea23c63274e0c6315894209fe Mon Sep 17 00:00:00 2001 From: "wenqi.huang" Date: Thu, 11 Apr 2019 18:02:24 +0800 Subject: [PATCH 2/5] Add more tests for @ConditionalOnRefreshScope --- .../eureka/ConditionalOnRefreshScopeTests.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/ConditionalOnRefreshScopeTests.java b/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/ConditionalOnRefreshScopeTests.java index 2afeebc7a..93487293c 100644 --- a/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/ConditionalOnRefreshScopeTests.java +++ b/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/ConditionalOnRefreshScopeTests.java @@ -43,6 +43,18 @@ public class ConditionalOnRefreshScopeTests { }); } + @Test + public void refreshScopeIncludedAndPropertyDisabled() { + new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class)) + .withPropertyValues("eureka.client.refresh.enable=false") + .withUserConfiguration(Beans.class).run(c -> { + assertThat(c).hasSingleBean( + org.springframework.cloud.context.scope.refresh.RefreshScope.class); + assertThat(c).doesNotHaveBean("foo"); + }); + } + @Test public void refreshScopeNotIncluded() { new ApplicationContextRunner().withUserConfiguration(Beans.class).run(c -> { @@ -59,4 +71,4 @@ public class ConditionalOnRefreshScopeTests { } } -} \ No newline at end of file +} From 7fd55be4b3e5226a61f65dcedad62468802e16bb Mon Sep 17 00:00:00 2001 From: "wenqi.huang" Date: Thu, 11 Apr 2019 18:28:09 +0800 Subject: [PATCH 3/5] Add more tests for @ConditionalOnRefreshScope --- .../eureka/ConditionalOnRefreshScopeTests.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/ConditionalOnRefreshScopeTests.java b/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/ConditionalOnRefreshScopeTests.java index 93487293c..7118fa89d 100644 --- a/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/ConditionalOnRefreshScopeTests.java +++ b/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/ConditionalOnRefreshScopeTests.java @@ -20,6 +20,7 @@ import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration.ConditionalOnMissingRefreshScope; import org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration.ConditionalOnRefreshScope; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -40,6 +41,7 @@ public class ConditionalOnRefreshScopeTests { assertThat(c).hasSingleBean( org.springframework.cloud.context.scope.refresh.RefreshScope.class); assertThat(c.getBean("foo")).isEqualTo("foo"); + assertThat(c).doesNotHaveBean("bar"); }); } @@ -52,6 +54,7 @@ public class ConditionalOnRefreshScopeTests { assertThat(c).hasSingleBean( org.springframework.cloud.context.scope.refresh.RefreshScope.class); assertThat(c).doesNotHaveBean("foo"); + assertThat(c.getBean("bar")).isEqualTo("bar"); }); } @@ -59,6 +62,14 @@ public class ConditionalOnRefreshScopeTests { public void refreshScopeNotIncluded() { new ApplicationContextRunner().withUserConfiguration(Beans.class).run(c -> { assertThat(c).doesNotHaveBean("foo"); + assertThat(c.getBean("bar")).isEqualTo("bar"); + }); + + new ApplicationContextRunner().withUserConfiguration(Beans.class) + .withPropertyValues("eureka.client.refresh.enable=false") + .run(c -> { + assertThat(c).doesNotHaveBean("foo"); + assertThat(c.getBean("bar")).isEqualTo("bar"); }); } @@ -69,6 +80,12 @@ public class ConditionalOnRefreshScopeTests { public String foo() { return "foo"; } + + @Bean + @ConditionalOnMissingRefreshScope + public String bar() { + return "bar"; + } } } From 84ccefcd4aaddb4c80f09241118d89def885e9bc Mon Sep 17 00:00:00 2001 From: "wenqi.huang" Date: Fri, 3 May 2019 10:42:40 +0800 Subject: [PATCH 4/5] Add some documentation about the property `eureka.client.refresh.enable` --- docs/src/main/asciidoc/spring-cloud-netflix.adoc | 13 +++++++++++++ .../additional-spring-configuration-metadata.json | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/src/main/asciidoc/spring-cloud-netflix.adoc b/docs/src/main/asciidoc/spring-cloud-netflix.adoc index 77f6c2835..a292fde11 100755 --- a/docs/src/main/asciidoc/spring-cloud-netflix.adoc +++ b/docs/src/main/asciidoc/spring-cloud-netflix.adoc @@ -324,6 +324,19 @@ eureka.instance.metadataMap.zone = zone2 eureka.client.preferSameZoneEureka = true ``` +=== Refresh Scope of Eureka Client + +By default, the `EurekaClient` bean is refreshable, so the Eureka client properties can be refreshed, but this has a disadvantage: +with the default setting, the `EurekaClient` instance will be destroyed and reconstructed again when refreshed, so your program will unregister from eurekaServer and after some seconds, it will register to eurekaServer again, +consider that when we refresh all of the `Service A` instances at the same time, there will be some seconds no one instance alive on eurekaServer, if at the same time, `Service B` tries to fetch instances of `Service A`, +it will fetch an empty list. If you don't want to see this, you can set `eureka.client.refresh.enable=false` in the application properties to make the `EurekaClient` instance immutable, +but notice that if you set this property to false, none of the Eureka client properties will be refreshable. + +*application.properties. * +``` +eureka.client.refresh.enable=false +``` + [[spring-cloud-eureka-server]] == Service Discovery: Eureka Server diff --git a/spring-cloud-netflix-eureka-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-netflix-eureka-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 90a7b97b9..2d964f336 100644 --- a/spring-cloud-netflix-eureka-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-netflix-eureka-client/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -9,7 +9,7 @@ { "defaultValue": true, "name": "eureka.client.refresh.enable", - "description": "Determines whether EurekaClient instance can be refreshed or not.", + "description": "Determines whether the EurekaClient instance can be refreshed or not(If disabled none of the Eureka client properties will be refreshable).", "type": "java.lang.Boolean" }, { From 2bbb537b174a20025a780023dccf6036fc073ecc Mon Sep 17 00:00:00 2001 From: "wenqi.huang" Date: Fri, 3 May 2019 10:52:54 +0800 Subject: [PATCH 5/5] Add some documentation about the property `eureka.client.refresh.enable` --- docs/src/main/asciidoc/spring-cloud-netflix.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/main/asciidoc/spring-cloud-netflix.adoc b/docs/src/main/asciidoc/spring-cloud-netflix.adoc index a292fde11..94c6d99bc 100755 --- a/docs/src/main/asciidoc/spring-cloud-netflix.adoc +++ b/docs/src/main/asciidoc/spring-cloud-netflix.adoc @@ -330,7 +330,7 @@ By default, the `EurekaClient` bean is refreshable, so the Eureka client propert with the default setting, the `EurekaClient` instance will be destroyed and reconstructed again when refreshed, so your program will unregister from eurekaServer and after some seconds, it will register to eurekaServer again, consider that when we refresh all of the `Service A` instances at the same time, there will be some seconds no one instance alive on eurekaServer, if at the same time, `Service B` tries to fetch instances of `Service A`, it will fetch an empty list. If you don't want to see this, you can set `eureka.client.refresh.enable=false` in the application properties to make the `EurekaClient` instance immutable, -but notice that if you set this property to false, none of the Eureka client properties will be refreshable. +but notice that if you set this property to false, none of the Eureka client properties will be refreshable. (This property is imported since `Finchley.SR4`) *application.properties. * ```