springboot整合feign

1、jar包依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2、启动类添加注解

若A调B服务,则A服务启动类需要添加注解

@EnableFeignClients (为了扫描feign接口)
属性:
basePackageClasses 扫描类
basePackages 扫描包
@EnableFeignClients(basePackageClasses = RegistryService.class, basePackages = “com.wzj.feign”)

3、调用方式

A调B
第一种方式,直接url调用
name是为B服务起的名字,必输。

@FeignClient(url = "http://192.168.20.11:8081", name = "web-socket-api", configuration = FeignConfiguration.class)
public interface WebSocketManageClient {
    @RequestMapping(value = "/message", method = RequestMethod.POST)
    void sendMessage(@RequestParam(value = "message") String message);

}

使用时

    @Autowired
    private WebSocketManageClient webSocketManageClient;
    
	webSocketManageClient.sendMessage(message);

第二种方式,使用注册中心

value的值为B服务注册到注册中心的服务名,和name作用一样。name 和value必须存在一个。

@FeignClient(value = "user-server")
public interface UserFeign {
    @GetMapping(value = "/user/getUserById")
  	User getUserById(@RequestParam("id") String id) throws Exception;

4、问题点

曾经在这里遇到个问题,@RequestParam 和@PathVariable 没有使用value属性报错

错误:Feign PathVariable annotation was empty on param 0

例子:
@RequestParam String id
@PathVariable String id
使用vlue属性,给默认名后就可以了
@RequestParam(value = “id”) String id 或者@RequestParam(id) String id
@PathVariable(value = “id”) String id 或者@PathVariable(id) String id

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值