【问题标题】:Feign client connexion假装客户端连接
【发布时间】:2020-10-07 16:51:42
【问题描述】:

Heloo 每个人,我正在尝试通过 feign 客户端连接到 api,我在 jhipster 网关中使用 feign 客户端.. 我已经在微服务中使用了相同的代码,它运行良好这是我的代码写道:



@FeignClient( name = "berrycord" ,url = "https://dev1.digitalberry.fr/bcs-berrycord-direct/")

/**
 * This interface is used to call berryscheduler APIs ,
 * using netflix feign client
 * @param body  host to manage
 */

public interface TraceClientInterface {

    @PostMapping("api/v1/records/")
    public JSONObject sendReport(@RequestBody JSONObject report);

    // @GetMapping(value="/jokes/count")
    // public JSONObject sendReport();

}
@Component
public class UserFeignClientInterceptor implements RequestInterceptor {
    private static final String AUTHORIZATION_HEADER = "Authorization";
    private static final String BEARER = "Bearer";

    @Override
    public void apply(RequestTemplate template) {

        System.out.println("test ========================" +template.request());
        System.out.println("test ========================2" +template.toString());
        System.out.println("test ========================3" +new String(template.body()));

        SecurityUtils.getCurrentUserJWT()
            .ifPresent(s -> template.header(AUTHORIZATION_HEADER,String.format("%s %s", BEARER, s)));


        SecurityUtils.getCurrentUserLogin()
            .ifPresent(user -> template.header("X-Forwarded-User", user));

        SecurityUtils.getCurrentUserAuthorities()
            .ifPresent(authorities -> template.header("X-Forwarded-Role", authorities));

    }
}
/**
     * This service communicates with berryCord to create a send report POST
     * /api/v1/report/ endpoint, is called when creating or updating the host
     * resource
     * 
     * @param task
     */
    public JSONObject sendReport(JSONObject report) {

        log.debug("Request to create log report in berrycord ");
        JSONObject rep = new JSONObject() ;
        try {
            log.info("=========== Request to create log report in berrycord 2 " , report);
            rep =  traceClientInterface.sendReport(report);
            log.info("=========== Request to create log report in berrycord 3 " , report);

        } catch (FeignException e) {
            e.getStackTrace();
        }
        return rep;

    }

feign:
    hystrix:
        enabled: false
    client:
        url: 
            berryCordUrl: https://dev1.digitalberry.fr/bcs-berrycord-direct/

但是两者之间的联系从未完成,我看不到调用 API 的结果.. 谁能告诉我我做错了什么..谢谢:) :)

【问题讨论】:

    标签: java client spring-cloud-feign netflix


    【解决方案1】:

    您应该有一个@Configuration 如下并将@EnableFeignClients 添加到您的Application.java

    @Configuration
    public class FooConfiguration {
    
        @Bean
        public UserFeignClientInterceptor userFeignClientInterceptor() {
            return new UserFeignClientInterceptor();
        }
    }
    

    【讨论】:

    • 感谢您的回复..我也有同样的问题...客户从未被调用过:/...还有其他解决方案吗?
    猜你喜欢
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多