eureka和Feign实现远程调用

实际配置中需要注意的点:

1、在consumer工程中需要在启动类上开启:@EnableFeignClients;

2、eureka的配置中需要注意为hostname添加hosts配置,否则会提示:feign java.net.UnknownHostException:consumer ,这样的异常

eureka:
  instance:
    hostname: consumer    #  这里需要注意在hosts中添加配置,例如:192.168.0.107 consumer
    instance-id: ${server.address}:${server.port}

3、如果get请求中需要传参,按照springmvc的传参方式,如下所示:

@FeignClient(name="myfeign")
public interface FeignClientService {
    @GetMapping("/fclient/addstr")
    public String dealString(String str);
}

     会碰到如下类似异常:

feign.FeignException$MethodNotAllowed: [405] during [GET] to [http://myfeign/fclient/addstr] [FeignClientService#dealString(String)]: [{"timestamp":"2022-01-18T09:19:18.522+00:00","status":405,"error":"Method Not Allowed","path":"/fclient/addstr"}]

 

     正确的写法如下:

@FeignClient(name="myfeign")
public interface FeignClientService {
    @GetMapping("/fclient/addstr")
    public String dealString(@RequestParam("str") String str);
}

 





posted @ 2021-10-22 22:02  奋斗吧🚗少年  阅读(263)  评论(0)    收藏  举报