繁体   English   中英

假装客户端连接

[英]Feign client connexion

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 的结果..谁能告诉我我做错了什么..谢谢:):)

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

@Configuration
public class FooConfiguration {

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM