Spring Boot OpenFeign 动态切换 Url 报错解决方法

Spring Boot OpenFeign About 1,009 words

错误信息

No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer

错误代码

为了能动态切换路径编写了如下代码,@FeignClient注解中没有配置url属性。

通过@GetMapping透传路径。

@FeignClient(name = "my-feign-client")
public interface Router {

    @GetMapping("{path}")
    @ResponseBody
    ResponseEntity<JsonNode> get(@PathVariable(value = "path") String path,
                                 @SpringQueryMap MultiValueMap<String, String> queryString);
}

错误原因

在没有配置负载均衡的情况下,必须指定url路径,且不能为空。

解决方法

url参数随意指定一个占位符即可,在方法参数中传入URI对象。

@FeignClient(name = "my-feign-client", url = "placeholder")
public interface Router {

    @GetMapping("{path}")
    @ResponseBody
    ResponseEntity<JsonNode> get(URI uri, @PathVariable(value = "path") String path,
                                 @SpringQueryMap MultiValueMap<String, String> queryString);
}

使用

@Resource
private Router router;

URI uri = URI.create(url);

ResponseEntity<JsonNode> response = router.get(uri, path, queryString);
Views: 1,839 · Posted: 2023-07-12

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh