spring cloud feign 注意事项

一、feign参数传递

1、 不支持@GetMapping类似注解声明请求,需使用@RequestMapping(value = “url”,method = RequestMethod.GET)

2、当参数比较复杂时,feign即使声明为get请求也会强行使用post请求,解决方推荐第4项

3、使用@RequestParam注解时必须要在后面加上参数名;feign 客户端的配置,包含
熔断、调用服务名称,url 或日志等信息

 

4、案例

@FeignClient(name ="API-SERVICE",fallbackFactory = FeignFallBack.class )
public interface FeignService {
    /***
     * @methodName feign 多参数接口请求
     * @Description: 查询船图信息

     **/
    @RequestMapping(value = "/demo/list", method = RequestMethod.GET)
    public ok<?> list(@RequestParam("name") String name, @RequestParam("sex") String sex, @RequestParam("address") String address, @RequestParam("telphone") String telphone, @RequestParam("pcgod") String pcgod);

    /***
    * TODO: feign 传递对象进行get请求
    * @methodName  

    */
    @RequestMapping(value = "/demo/listByObject", method = RequestMethod.GET)
    public ok<?>  list(@SpringQueryMap DemoVo demoVo );

}

二、异常

`The injection point has the following annotations: - @org.springframework.beans.factory.annotation.The injection point has the following annotations:

- @org.springframework.beans.factory.annotation.Autowired(required=true)`

调用方配置 @EnableFeignClients指定clients

@EnableEurekaClient
@EnableAsync
@EnableCircuitBreaker
@SpringBootApplication(scanBasePackages="com.port")
@EnableFeignClients(clients = {FeignService.class})
@EnableHystrixDashboard
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

在调用方扫描feignClient 所在包

@EnableFeignClients(basePackages = {"com.gjtdport.*.api"})

@EnableEurekaClient
@EnableAsync
@EnableCircuitBreaker
@SpringBootApplication(scanBasePackages="com.gjtdport")
@EnableFeignClients(basePackages = {"com.gjtdport.*.api"})
@EnableHystrixDashboard
public class XxApplication {
	public static void main(String[] args) {

		SpringApplication.run(XxApplication.class, args);
	}

}

二、建议同一微服务提供的feign 服务在同一接口中提供,规避 feignClient  name 相同导致bean 重复定义问题

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值