Merge branch '3.1.x'
This commit is contained in:
@@ -376,6 +376,47 @@ public class FooConfiguration {
|
||||
|
||||
To enable Spring Cloud CircuitBreaker group set the `spring.cloud.openfeign.circuitbreaker.group.enabled` property to `true` (by default `false`).
|
||||
|
||||
[[spring-clou-feign-circuitbreaker-configurationproperties]]
|
||||
=== Configuring CircuitBreakers With Configuration Properties
|
||||
|
||||
You can configure CircuitBreakers via configuration properties. To do set
|
||||
`feign.circuitbreaker.alphanumeric-ids.enabled` to `true`. Since
|
||||
you cannot use characters like `#`, `(`, `)` `,` in configuration property names we need to
|
||||
change the naming convention for the ids of the circuit breakers generated by OpenFeign. The above
|
||||
property will do this for you.
|
||||
|
||||
For example, if you had this Feign client
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@FeignClienturl = "http://localhost:8080")
|
||||
public interface DemoClient {
|
||||
|
||||
@GetMapping("demo")
|
||||
String getDemo();
|
||||
}
|
||||
----
|
||||
|
||||
You could configure it using configuration properties by doing the following
|
||||
|
||||
[source,yaml,indent=0]
|
||||
----
|
||||
feign:
|
||||
circuitbreaker:
|
||||
enabled: true
|
||||
alphanumeric-ids:
|
||||
enabled: true
|
||||
resilience4j:
|
||||
circuitbreaker:
|
||||
instances:
|
||||
DemoClientgetDemo:
|
||||
minimumNumberOfCalls: 69
|
||||
timelimiter:
|
||||
instances:
|
||||
DemoClientgetDemo:
|
||||
timeoutDuration: 10s
|
||||
----
|
||||
|
||||
|
||||
[[spring-cloud-feign-circuitbreaker-fallback]]
|
||||
=== Feign Spring Cloud CircuitBreaker Fallbacks
|
||||
|
||||
@@ -165,10 +165,20 @@ public class FeignAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(CircuitBreakerNameResolver.class)
|
||||
@ConditionalOnProperty(value = "feign.circuitbreaker.alphanumeric-ids.enabled",
|
||||
havingValue = "false", matchIfMissing = true)
|
||||
public CircuitBreakerNameResolver circuitBreakerNameResolver() {
|
||||
return new DefaultCircuitBreakerNameResolver();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(CircuitBreakerNameResolver.class)
|
||||
@ConditionalOnProperty(value = "feign.circuitbreaker.alphanumeric-ids.enabled",
|
||||
havingValue = "true")
|
||||
public CircuitBreakerNameResolver alphanumericCircuitBreakerNameResolver() {
|
||||
return new AlphanumericCircuitBreakerNameResolver();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnBean(CircuitBreakerFactory.class)
|
||||
@@ -188,6 +198,15 @@ public class FeignAutoConfiguration {
|
||||
|
||||
}
|
||||
|
||||
static class AlphanumericCircuitBreakerNameResolver extends DefaultCircuitBreakerNameResolver {
|
||||
|
||||
@Override
|
||||
public String resolveCircuitBreakerName(String feignClientName, Target<?> target, Method method) {
|
||||
return super.resolveCircuitBreakerName(feignClientName, target, method).replaceAll("[^a-zA-Z0-9]", "");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// the following configuration is for alternate feign clients if
|
||||
|
||||
@@ -20,6 +20,12 @@
|
||||
"description": "If true, an OpenFeign client will be wrapped with a Spring Cloud CircuitBreaker circuit breaker with with group.",
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.openfeign.circuitbreaker.alphanumeric-ids.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "If true, Circuit Breaker ids will only contain alphanumeric characters to allow for configuration via configuration properties.",
|
||||
"defaultValue": "false"
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.openfeign.httpclient.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2013-2022 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.circuitbreaker;
|
||||
|
||||
import feign.Target;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.openfeign.CircuitBreakerNameResolver;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
public class CircuitBreakerAutoConfigurationTests {
|
||||
|
||||
@SpringBootTest(classes = CircuitBreakerTests.Application.class,
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
value = { "spring.application.name=springcircuittest", "spring.jmx.enabled=false",
|
||||
"feign.circuitbreaker.enabled=true" })
|
||||
@Nested
|
||||
class DefaultNamingStrategy {
|
||||
|
||||
@Autowired
|
||||
CircuitBreakerNameResolver nameResolver;
|
||||
|
||||
@Test
|
||||
public void assertDefaultNamingStrategy() throws Exception {
|
||||
Target target = mock(Target.class);
|
||||
when(target.type()).thenReturn(CircuitBreakerTests.TestClientWithFactory.class);
|
||||
assertThat(nameResolver.resolveCircuitBreakerName("foo", target,
|
||||
CircuitBreakerTests.TestClientWithFactory.class.getMethod("getHello")))
|
||||
.isEqualTo("TestClientWithFactory#getHello()");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SpringBootTest(classes = CircuitBreakerTests.Application.class,
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
value = { "spring.application.name=springcircuittest", "spring.jmx.enabled=false",
|
||||
"feign.circuitbreaker.enabled=true",
|
||||
"feign.circuitbreaker.alphanumeric-ids.enabled=true" })
|
||||
@Nested
|
||||
class AlphanumericNamingStrategy {
|
||||
|
||||
@Autowired
|
||||
CircuitBreakerNameResolver nameResolver;
|
||||
|
||||
@Test
|
||||
public void assertAlphanumericNamingStrategy() throws Exception {
|
||||
Target target = mock(Target.class);
|
||||
when(target.type()).thenReturn(CircuitBreakerTests.TestClientWithFactory.class);
|
||||
assertThat(nameResolver.resolveCircuitBreakerName("foo", target,
|
||||
CircuitBreakerTests.TestClientWithFactory.class.getMethod("getHello")))
|
||||
.isEqualTo("TestClientWithFactorygetHello");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user