养成良好的记录习惯
时间:2019年10月12日
作者:黄黄
邮箱:15797683468@163.com(可指出问题相互交流)
前言:
springCloud在管理fegin调用服务时,有一些调用服务重合,所以建议统一管理fegin调用服务
1.创建对应的公用部分(如图所示)
package模块
对应的配置项
spring.factories配置其他包自动注入代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zzw.core.api.feign.fallback.ProductFeignFallback,\
com.zzw.core.api.feign.factory.ProductFeignFallbackFactory
2.指定fegin客户端扫描地址
可以直接在@EnableFeignClients(basePackages = “com.zzw.core.api”)指定
也可以自定义注解(相对比较优雅)
####自定义注解类
package com.zzw.core.security.annotation;
import org.springframework.cloud.openfeign.EnableFeignClients;
import java.lang.annotation.*;
/**
* @author zhang
* @date 2019/3/18
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@EnableFeignClients
public @interface EnableZzwFeignClients {
/**
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation
* declarations e.g.: {@code @ComponentScan("org.my.pkg")} instead of
* {@code @ComponentScan(basePackages="org.my.pkg")}.
*
* @return the array of 'basePackages'.
*/
String[] value() default {};
/**
* Base packages to scan for annotated components.
* <p>
* {@link #value()} is an alias for (and mutually exclusive with) this attribute.
* <p>
* Use {@link #basePackageClasses()} for a type-safe alternative to String-based
* package names.
*
* @return the array of 'basePackages'.
*/
String[] basePackages() default {"com.zzw.core.api"};
/**
* Type-safe alternative to {@link #basePackages()} for specifying the packages to
* scan for annotated components. The package of each class specified will be scanned.
* <p>
* Consider creating a special no-op marker class or interface in each package that
* serves no purpose other than being referenced by this attribute.
*
* @return the array of 'basePackageClasses'.
*/
Class<?>[] basePackageClasses() default {};
/**
* A custom <code>@Configuration</code> for all feign clients. Can contain override
* <code>@Bean</code> definition for the pieces that make up the client, for instance
* {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.
*
* @see for the defaults
*/
Class<?>[] defaultConfiguration() default {};
/**
* List of classes annotated with @FeignClient. If not empty, disables classpath scanning.
*
* @return
*/
Class<?>[] clients() default {};
}
3.启动类添加注解
package com.zzw.order;
import com.zzw.core.security.annotation.EnableZzwFeignClients;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author zhangzhiwen
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableZzwFeignClients
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
配置结束