zoukankan      html  css  js  c++  java
  • 【Feign】自定义配置

    【Feign】自定义配置

    转载:

    自定义配置,如果在同一个工程,注意配置不要和@SpringBootApplication或@ComponentSacan放在用一个包下,就是不要被扫描上

    package cn.example.config;
    
    import cn.example.interceptor.YcxFeignRequestInterceptor;
    import org.springframework.context.annotation.Bean;
    
    public class YcxFeignConfiguration {
        /**
         * 将契约改为feign原生的默认契约。这样就可以使用feign自带的注解了。
         * @return 默认的feign契约
         */
    //    @Bean
    //    public Contract getAFeignContract() {
    //        System.out.println(">>> create getAFeignContract");
    //        return new feign.Contract.Default();
    //    }
        @Bean
        YcxFeignRequestInterceptor ycxFeignRequestInterceptor() {
            System.out.println(">>> create ycxFeignRequestInterceptor");
            return new YcxFeignRequestInterceptor();
        }
    }

    自定义拦截器

    package cn.example.interceptor;
    
    import feign.RequestInterceptor;
    import feign.RequestTemplate;
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class YcxFeignRequestInterceptor implements RequestInterceptor {
        @Override
        public void apply(RequestTemplate template) {
            log.info(">>> " + template.path());
            log.info(">>> " + YcxFeignRequestInterceptor.class.getSimpleName());
        }
    }

    feign接口

    package com.example.feignservice.feign;
    
    import cn.example.config.YcxFeignConfiguration;
    import com.example.commenservice.bean.YcxResult;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @FeignClient(value = "feign-service", configuration = {YcxFeignConfiguration.class})
    public interface YcxFeign {
        @RequestMapping("/testFeignA")
    //    @RequestLine("GET /testFeignA") // 自定义契约
        YcxResult testFeignA(@RequestParam("value") String value);
    }

    接口实现

    package com.example.feignservice.feign;
    
    import com.example.commenservice.bean.YcxResult;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.time.LocalTime;
    
    @RestController
    public class YcxFeignImpl implements YcxFeign {
        public YcxResult testFeignA(String value) {
            return YcxResult.ok(LocalTime.now().toString() + " " + value);
        }
    }

    调用端

    package com.example.helloservice;
    
    import com.example.commenservice.bean.YcxResult;
    import com.example.feignservice.feign.YcxFeign;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.servlet.http.HttpServletRequest;
    
    @SpringBootApplication
    @RestController
    @Slf4j
    @EnableFeignClients(basePackageClasses = {YcxFeign.class})
    public class HelloServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(HelloServiceApplication.class, args);
        }
    
        @Autowired
        YcxFeign feignA;
    
    //    @Autowired
    //    HelloServiceApplication(Decoder decoder, Encoder encoder, Client client, Contract contract) {
    //        this.feignA = Feign.builder().client(client)
    //                .encoder(encoder)
    //                .decoder(decoder)
    //                .contract(contract)
    //                .requestInterceptor(new YcxFeignRequestInterceptor())
    //                .target(YcxFeign.class, "http://feign-service");
    //    }
    
        @RequestMapping("/")
        public YcxResult home(HttpServletRequest request) {
            log.info(request.getServletPath());
            return feignA.testFeignA("Hello A");
        }
    
    }

    有两种方式,一种是自动注入,一种是被注释掉的,

        @Autowired
        HelloServiceApplication(Decoder decoder, Encoder encoder, Client client, Contract contract) {
            this.feignA = Feign.builder().client(client)
                    .encoder(encoder)
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(new YcxFeignRequestInterceptor())
                    .target(YcxFeign.class, "http://feign-service");
        }
  • 相关阅读:
    markdown文件的基本常用编写
    寒假作业安排及注意点
    Day2
    Day1
    Python格式化
    Python 遍历字典的键值
    python 判断是否为空
    git 回退版本
    Python获取当前文件夹位置
    Python3, Python2 获取当前时间
  • 原文地址:https://www.cnblogs.com/yangchongxing/p/10967322.html
Copyright © 2011-2022 走看看