1

我有 2 个微服务,ProductStoreInvoiceStore

我希望ProductStore通过APIInvoiceStore提供产品信息来调用that API以从ProductStore获取产品信息。但是ProductStore需要Authorization检查信息,user authentication因此我将@RequestHeader("Authorization") String auth其用作我的参数变量FeignAPI将其发送到ProductStoreAuthorization但是它报告说我测试它时他没有收到数据。

@RequestHeader这样使用是因为我在feign-reactive all 功能的示例中看到了它

我不知道是我在某处做错了什么还是我误解了@RequestHeader.

请帮帮我!这是我的代码。

我的ProductStore提供了能够获取产品信息的 API。

    @GetMapping("products")
    public ResponseEntity<String> test(@RequestHeader("Authorization") String authorization) {
        log.debug("Authorization is {}", authorization);
        return ResponseEntity.ok().body("all products");
    }

我的InvoiceStorefeign-reactive使用WebReactiveFeign调用该 API 。

我按照Playtika feign-reactive的自述文件中的说明将其应用到我的项目中,如下所示

首先,我写 FeignAPI

@Headers({ "Accept: application/json" })
public interface FeignClientAPI {

    @RequestLine("GET /products")
    Mono<String> getProducts(@RequestHeader("Authorization") String authorization);
}

然后,我建立客户端IvoiceService

@Service
@Transactional
public class InvoiceService {
    private final FeignClientAPI client = WebReactiveFeign.<FeignClientAPI>builder().target(FeignClientAPI.class, "http://localhost:8082");

    public Mono<String> testFeign(String authorization){
        log.debug("Call api with authorization: {}", authorization);
        return client.getTest(authorization);
    }
}

然后,我创建了一个 API

    @GetMapping("/invoice/test")
    public Mono<ResponseEntity<String>> getProducts(@RequestHeader("Authorization") String authorization) {
        return invoiceService.testFeign(authorization)
            .switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)))
            .map(response -> ResponseEntity.ok().body(response));
    }

最后,我发出GET请求,localhost:8083/invoice/test但出现错误

{
    "title": "Internal Server Error",
    "status": 500,
    "detail": "[400 Bad Request] during [GET] to [http://localhost:8082/products] [FeignClientAPI#getTest(String)]: [{\n  \"title\" : \"Bad Request\",\n  \"status\" : 400,\n  \"detail\" : \"Required request header 'Authorization' for method parameter type String is not present\",\n  \"path\" : \"/products\",\n  \"message\" : \"error.http.400\"\n}]",
    "path": "/invoice/test",
    "message": "error.http.500"
}

告诉我我哪里做错了,拜托!!!谢谢你为我做的一切。

4

2 回答 2

1

你的代码完全错误,我认为你应该得到编译时错误,因为 testFeign(String authorization) 需要一个字符串输入,但是当你调用它时(invoiceService.testFeign().switchIfEmpty ...)你没有传递任何输入给它。

我应该检查主代码,但我认为你在客户端传递空值作为授权(可能)。

于 2021-12-27T08:57:16.077 回答
0

我找到了解决这个问题的方法。

我之前误解了如何使用reactive feign,导致它无法正常工作。

我已经为我的 Spring Boot 应用程序添加了@EnableReactiveFeignClients@EnableFeignClients

@EnableReactiveFeignClients
@EnableFeignClients
public class AnswerStoreApp {
// main method
}

然后,我创建一个界面@ReactiveFeignClient(name = "my-other-service")

@ReactiveFeignClient(name = "my-other-service")
public interface FeignClientService {

    @GetMapping("/api/questions/test-feign")
    Mono<String> demo(@RequestHeader("Authorization") String authorize);

    // More request
}

最后,我可以FeignClientService用来获取我需要的数据

@Autowired
private FeignClientService feignClientService;

// Some method

@GetMapping("/invoice/test")
    public Mono<ResponseEntity<String>> getProducts(@RequestHeader("Authorization") String authorization) {
        return feignClientService.testFeign(authorization)
            .switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)))
            .map(response -> ResponseEntity.ok().body(response));
    }
于 2022-01-07T19:14:15.370 回答