【Feign】自定義配置

【Feign】自定義配置java

轉載:web

自定義配置,若是在同一個工程,注意配置不要和@SpringBootApplication或@ComponentSacan放在用一個包下,就是不要被掃描上spring

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

自定義攔截器app

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接口ide

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

接口實現ui

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

 

 

調用端this

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

}

有兩種方式,一種是自動注入,一種是被註釋掉的,spa

    @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");
    }
相關文章
相關標籤/搜索