目录
Spring boot 2.5.6------spring-cloud-starter-openfeign 3.0.3
1.新建Spring boot 项目 导入Spring Cloud Feign 依赖
2.启动类开启 Feign 注解 @EnableFeignClients
0.序言---版本
Spring boot 2.5.6------spring-cloud-starter-openfeign 3.0.3
Springboot 和 Cloud 版本冲突是个很让人头疼的问题,作者以前项目中 Spring boot 版本是1.5.9,现在因为一些原因boot版本 升到2.5.6,但是Feign的版本 便与之不匹配,爆了各种错最后才发现是版本冲突的问题,友情提示一点,Spring boot 和 Spring Cloud 整合的时候,报错一定要考虑到是不是,版本冲突的问题。
1.新建Spring boot 项目 导入Spring Cloud Feign 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.3</version>
</dependency>
2.启动类开启 Feign 注解 @EnableFeignClients
3.新建interface服务调用接口
package com.wlh.web.feign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "fegin-service",url = "http://localhost:9091/")
public interface CallFeginClientService {
@GetMapping("/testFegin")
public String getFeginMessage();
//参数
@GetMapping("/pushMessage.action")
public String testPutStringToMq(@RequestParam (value = "id") int id);
}
@FeignClient 注解 name:服务名(此处demo随意定义)
url:访问服务路径
第一个方法,没有参数;第二个方法带有参数
4.新建Controller 调用服务接口
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wlh.web.feign.service.CallFeginClientService;
@RestController
public class CallFeginClientController {
@Autowired
private CallFeginClientService feginervice;
@GetMapping("/testFeginClientToMessage")
public String getFeginMessage(int id){
Object feginMessage = feginervice.testPutStringToMq(id);
System.out.println(feginMessage);
return feginMessage.toString();
}
}
1234都是调用方
5.下面新建一个服务方接口,测试Feign远程调用接口
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MessageController {
@GetMapping("/pushMessage.action")
public Object get(@RequestParam("id") int id) {
System.out.println("调用成功,参数id为======"+id);
return "调用成功";
}
}