首页 文章

使用带有Spring引导的Feign时,将URL加倍

提问于
浏览
0

我对Spring启动并不熟悉,我正在使用Feign Rest客户端与我的Web服务进行通信 . 但我得到的URL是双倍的,无法调用预期的服务 .

@FeignClient(name= "exchange-service", url="localhost:8000")

public interface ExchangeServiceProxy {

@GetMapping
@RequestMapping(value = "/exchange/from/{from}/to/{to}")
public ExchangeBean retrieveExchangeValue(@PathVariable("from") String from,
        @PathVariable("to") String to);

}

status 404 reading 
ExchangeServiceProxy#retrieveExchangeValue(String,String); content:
{"timestamp":"2018-11-22T05:50:45.822+0000","status":404,"error":"Not 
Found","message":"No message 
available","path":"/exchange/from/USD/to/XYZ/exchange/from/USD/to/XYZ"}

2 回答

  • 0

    你需要在 @SpringBootApplication 之后在主类中添加 @EnableFeignClients 像这样:

    @SpringBootApplication
    @ComponentScan
    @EnableScheduling
    @EnableAsync
    @EnableZuulProxy
    @EnableFeignClients
    public class ExampleApplication extends SpringBootServletInitializer{
        public static void main(String[] args) {
            SpringApplication.run(ExampleApplication.class, args);
        }
    
    
    }
    

    然后为 FeignClient 创建一个接口,如下所示:

    @FeignClient(name = "service_name", url = "http://localhost:port_to_the_another_application")
    public interface ExampleFeignClient {
    
        @RequestMapping(value = "/mapping", method = RequestMethod.POST)
        String createUserWallet(@RequestHeader("Authorization") String jwtToken);
    }
    

    我希望这将有所帮助 .

  • 0

    你还没有把Spring Starter Class放在你的问题中 . 如果你正在使用Feign来进行客户端负载均衡,那么@EnableFeignClients足以放入你的Spring入门级 . 我认为你已经在你的ExchangeServiceProxy中放了@GetMapping和@RequestMapping,这是不必要的 . 请删除@GetMapping,因为您已在@RequestMapping中提供了url模式 . 这可能会导致您的网址翻倍 .

    @FeignClient(name= "exchange-service", url="localhost:8000")
    public interface ExchangeServiceProxy {
    
    @RequestMapping(value = "/exchange/from/{from}/to/{to}")
    public ExchangeBean retrieveExchangeValue(@PathVariable("from") String 
    from,
            @PathVariable("to") String to);
    }
    

    如果你有像 url="localhost:8000" 那样的硬编码,那么它只会打到在端口8000下运行的实例 . 如果你的意图是我的话,那么使用Ribbon和Eureka命名服务器来充分利用客户端负载 balancer 是理想的 . 已经提到了 .

相关问题