Provide an alternate naming convention for CB ids to allow for configuration via configuration properties (#687)

Configuration properties cannot contain characters like hash, parens, or commas
This commit is contained in:
Ryan Baxter
2022-03-10 19:36:51 -05:00
committed by GitHub
parent 765431dce1
commit f2386e365d
4 changed files with 145 additions and 0 deletions

View File

@@ -166,10 +166,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)
@@ -189,6 +199,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

View File

@@ -14,6 +14,12 @@
"description": "If true, an OpenFeign client will be wrapped with a Spring Cloud CircuitBreaker circuit breaker.",
"defaultValue": "false"
},
{
"name": "feign.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": "feign.circuitbreaker.group.enabled",
"type": "java.lang.Boolean",

View File

@@ -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");
}
}
}