@FeignClientScan -> @EnableFeignClients
By analogy with @EnableJpaRepositories, so it's obvious that the same thing will happen (interfaces are turned into concrete @Beans).
This commit is contained in:
@@ -37,8 +37,8 @@ import org.springframework.context.annotation.Import;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
@Import(FeignClientScanRegistrar.class)
|
||||
public @interface FeignClientScan {
|
||||
@Import({ FeignClientsConfiguration.class, FeignClientsRegistrar.class })
|
||||
public @interface EnableFeignClients {
|
||||
|
||||
/**
|
||||
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.netflix.feign;
|
||||
|
||||
import feign.slf4j.Slf4jLogger;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
|
||||
@@ -28,9 +27,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
|
||||
import feign.Client;
|
||||
import feign.Contract;
|
||||
import feign.Feign;
|
||||
import feign.Logger;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
@@ -39,28 +36,9 @@ import feign.Logger;
|
||||
@Configuration
|
||||
@ConditionalOnClass(Feign.class)
|
||||
@AutoConfigureAfter(ArchaiusAutoConfiguration.class)
|
||||
@EnableFeignClients
|
||||
public class FeignAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public SpringDecoder feignDecoder() {
|
||||
return new SpringDecoder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SpringEncoder feignEncoder() {
|
||||
return new SpringEncoder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Logger feignLogger() {
|
||||
return new Slf4jLogger();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Contract feignContract() {
|
||||
return new SpringMvcContract();
|
||||
}
|
||||
|
||||
@ConditionalOnClass(ILoadBalancer.class)
|
||||
@Configuration
|
||||
protected static class RibbonClientConfiguration {
|
||||
|
||||
@@ -24,7 +24,10 @@ import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Annotation for interfaces declaring that a REST client with that interface should be
|
||||
* created (e.g. for autowiring into another component).
|
||||
* created (e.g. for autowiring into another component). If ribbon is available it will be
|
||||
* used to load balance the backend requests, and the load balancer can be configured
|
||||
* using a <code>@RibbonClient</code> with the same name (i.e. value) as the feign client.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@@ -33,14 +36,15 @@ import java.lang.annotation.Target;
|
||||
public @interface FeignClient {
|
||||
|
||||
/**
|
||||
* @return serviceId if loadbalance is true, url otherwise There is no need to prefix
|
||||
* serviceId with http://.
|
||||
* The serviceId if loadbalance is true, or an absolute URL otherwise There is no need
|
||||
* to prefix serviceId with http://.
|
||||
*/
|
||||
String value();
|
||||
|
||||
/**
|
||||
* @return true if calls should be load balanced (assuming a load balancer is
|
||||
* available).
|
||||
* Set to true if calls should be load balanced (assuming a load balancer is
|
||||
* available). If no load balancer is available this flag is ignored (and hence the
|
||||
* {@link #value() value} should be an absolute URL).
|
||||
*/
|
||||
boolean loadbalance() default true;
|
||||
|
||||
|
||||
@@ -106,10 +106,6 @@ class FeignClientFactoryBean implements FactoryBean<Object> {
|
||||
return builder;
|
||||
}
|
||||
|
||||
protected <T> T loadBalance(Class<T> type, String schemeName) {
|
||||
return loadBalance(feign(), type, schemeName);
|
||||
}
|
||||
|
||||
protected <T> T loadBalance(Feign.Builder builder, Class<T> type, String schemeName) {
|
||||
builder.logger(new Slf4jLogger(type)); // TODO: how to have choice here?
|
||||
if (this.ribbonClient != null) {
|
||||
@@ -126,7 +122,7 @@ class FeignClientFactoryBean implements FactoryBean<Object> {
|
||||
this.schemeName = "http://" + this.schemeName;
|
||||
}
|
||||
if (this.loadbalance) {
|
||||
return loadBalance(this.type, this.schemeName);
|
||||
return loadBalance(feign(), this.type, this.schemeName);
|
||||
}
|
||||
return feign().target(this.type, this.schemeName);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2013-2015 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
|
||||
*
|
||||
* http://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.netflix.feign;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import feign.Contract;
|
||||
import feign.Logger;
|
||||
import feign.slf4j.Slf4jLogger;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@Configuration
|
||||
public class FeignClientsConfiguration {
|
||||
|
||||
@Bean
|
||||
public SpringDecoder feignDecoder() {
|
||||
return new SpringDecoder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SpringEncoder feignEncoder() {
|
||||
return new SpringEncoder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Logger feignLogger() {
|
||||
return new Slf4jLogger();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Contract feignContract() {
|
||||
return new SpringMvcContract();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,7 +41,7 @@ import org.springframework.util.StringUtils;
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar,
|
||||
public class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
|
||||
ResourceLoaderAware, BeanClassLoaderAware {
|
||||
|
||||
// patterned after Spring Integration IntegrationComponentScanRegistrar
|
||||
@@ -50,7 +50,7 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar,
|
||||
|
||||
private ClassLoader classLoader;
|
||||
|
||||
public FeignClientScanRegistrar() {
|
||||
public FeignClientsRegistrar() {
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -91,7 +91,8 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar,
|
||||
}
|
||||
}
|
||||
|
||||
public BeanDefinitionHolder createBeanDefinition(AnnotationMetadata annotationMetadata) {
|
||||
private BeanDefinitionHolder createBeanDefinition(
|
||||
AnnotationMetadata annotationMetadata) {
|
||||
Map<String, Object> attributes = annotationMetadata
|
||||
.getAnnotationAttributes(FeignClient.class.getCanonicalName());
|
||||
|
||||
@@ -121,7 +122,7 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar,
|
||||
try {
|
||||
Class<?> target = ClassUtils.forName(beanDefinition
|
||||
.getMetadata().getClassName(),
|
||||
FeignClientScanRegistrar.this.classLoader);
|
||||
FeignClientsRegistrar.this.classLoader);
|
||||
return !target.isAnnotation();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
@@ -140,7 +141,7 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar,
|
||||
|
||||
protected Set<String> getBasePackages(AnnotationMetadata importingClassMetadata) {
|
||||
Map<String, Object> attributes = importingClassMetadata
|
||||
.getAnnotationAttributes(FeignClientScan.class.getCanonicalName());
|
||||
.getAnnotationAttributes(EnableFeignClients.class.getCanonicalName());
|
||||
|
||||
Set<String> basePackages = new HashSet<>();
|
||||
for (String pkg : (String[]) attributes.get("value")) {
|
||||
@@ -91,7 +91,7 @@ public class FeignClientTests {
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@RestController
|
||||
@FeignClientScan
|
||||
@EnableFeignClients
|
||||
@RibbonClient(name = "localapp", configuration = LocalRibbonClientConfiguration.class)
|
||||
protected static class Application {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user