Merge branch '2.0.x.fix' of https://github.com/aftersss/spring-cloud-netflix into aftersss-2.0.x.fix

This commit is contained in:
Ryan Baxter
2019-05-21 11:10:07 -04:00
4 changed files with 55 additions and 2 deletions

View File

@@ -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. (This property is imported since `Finchley.SR4`)
*application.properties. *
```
eureka.client.refresh.enable=false
```
[[spring-cloud-eureka-server]]
== Service Discovery: Eureka Server

View File

@@ -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

View File

@@ -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 the EurekaClient instance can be refreshed or not(If disabled none of the Eureka client properties will be refreshable).",
"type": "java.lang.Boolean"
},
{
"defaultValue": true,
"name": "ribbon.eureka.enabled",
@@ -13,4 +19,4 @@
"type": "java.lang.Boolean"
}
]
}
}

View File

@@ -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,20 @@ public class ConditionalOnRefreshScopeTests {
assertThat(c).hasSingleBean(
org.springframework.cloud.context.scope.refresh.RefreshScope.class);
assertThat(c.getBean("foo")).isEqualTo("foo");
assertThat(c).doesNotHaveBean("bar");
});
}
@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");
assertThat(c.getBean("bar")).isEqualTo("bar");
});
}
@@ -47,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");
});
}
@@ -57,6 +80,12 @@ public class ConditionalOnRefreshScopeTests {
public String foo() {
return "foo";
}
@Bean
@ConditionalOnMissingRefreshScope
public String bar() {
return "bar";
}
}
}
}