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

 

posted @ 2019-06-03 14:15  翠微  阅读(832)  评论(0编辑  收藏  举报