美文网首页Java
springCloud项目实战统一管理fegin调用服务

springCloud项目实战统一管理fegin调用服务

作者: 黄黄丶 | 来源:发表于2019-10-14 09:31 被阅读0次

    养成良好的记录习惯
    作者:黄黄

    前言:


    springCloud在管理fegin调用服务时,有一些调用服务重合,所以建议统一管理fegin调用服务

    1.创建对应的公用部分(如图所示)

    package模块 1571014690(1).png

    • ProductFeign代码
    @FeignClient(name = ServiceNameConstants.PRODUCT_SERVICE,fallbackFactory= ProductFeignFallbackFactory.class)
    public interface ProductFeign {
        /**
         * @return  String
         */
        @GetMapping("/msg")
        String msg();
    
        /**
         * 根据IdList查找商品详细信息
         * @param idList
         * @return
         */
        @GetMapping(value = "/productInfo/listByIdList")
        List<ProductInfo> listByIdList(@RequestParam("idList") List<String> idList);
    
        @PutMapping(value = "/productInfo/decreaseStock")
        Integer decreaseStock(@RequestBody List<CartDTO> cartDTOList);
    }
    
    
    • ProductFeignFallback代码
    package com.zzw.core.api.feign.fallback;
    
    import com.zzw.core.api.dto.product.CartDTO;
    import com.zzw.core.api.feign.ProductFeign;
    import com.zzw.core.api.po.product.ProductInfo;
    import lombok.Data;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    
    /**
     * @author zhangzhiwen
     */
    @Slf4j
    @Data
    @Component
    public class ProductFeignFallback implements ProductFeign {
        private Throwable cause;
    
        @Override
        public String msg() {
            log.error("feign 查询失败", cause);
            return null;
        }
    
        @Override
        public List<ProductInfo> listByIdList(List<String> idList) {
            log.error("feign 根据idList查询详细信息失败 param={}",idList, cause);
            return null;
        }
    
        @Override
        public Integer decreaseStock(List<CartDTO> cartDTOList) {
            log.error("feign 扣减库存失败 param={}",cartDTOList, cause);
            return null;
        }
    }
    
    
    • ProductFeignFallbackFactory代码
    package com.zzw.core.api.feign.factory;
    
    import com.zzw.core.api.feign.ProductFeign;
    import com.zzw.core.api.feign.fallback.ProductFeignFallback;
    import feign.hystrix.FallbackFactory;
    import org.springframework.stereotype.Component;
    
    /**
     * @author zhang
     */
    @Component
    public class ProductFeignFallbackFactory implements FallbackFactory<ProductFeign> {
    
        @Override
        public ProductFeign create(Throwable throwable) {
            ProductFeignFallback productFeignFallback = new ProductFeignFallback();
            productFeignFallback.setCause(throwable);
            return productFeignFallback;
        }
    }
    
    

    对应的配置文件 1571014947(1).png

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

    配置结束

    相关文章

      网友评论

        本文标题:springCloud项目实战统一管理fegin调用服务

        本文链接:https://www.haomeiwen.com/subject/kiylmctx.html