随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

Feign快速入门

一、Feign简介
1、Feign是一个声明式的web服务客户端,使用Feign编写web服务客户端更加容易
2、具有可插拔注解支持,包括Feign注解和JAX-RS注解,还支持可插拔的编码器与解码器
3、Spring Cloud 增加了对 Spring MVC的注解的支持,Spring Web 默认使用了HttpMessageConverters
4、Spring Cloud 集成了 Ribbon 和 Eureka,在使用Feign时提供负载均衡的HTTP客户端

二、导入依赖

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

三、开启注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启Feign支持
public class ConsumerApplication {

}

 

四、Feign入门示例
1、ProducerController——服务提供者,在mima-cloud-producer项目中

复制代码
@RestController
public class ProducerController {
    
    @GetMapping("/get/{id}")
    public String get(@PathVariable String id) {
        return "hi,"+id;
    }
    
    @GetMapping("/getuser/{id}")
    public User getUser(@PathVariable String id) {
        System.out.println("getUser.....");
        User user = new User();
        user.setId(id);
        user.setName("wangwu" + id);
        return user;
    }
    
    @PostMapping("/postuser")
    public User postUser(@RequestBody User user) {
        System.out.println("postUser.....");
        return user;
    }
    
    @GetMapping("/getuser2")
    public User getUser2(User user) {
        System.out.println("getUser2.....");
        return user;
    }
    
    @GetMapping("/listAll")
    public List<User> listAll(){
        List<User> users = new ArrayList<User>();
        users.add(new User("1","kevin1"));
        users.add(new User("2","kevin2"));
        users.add(new User("3","kevin3"));
        return users;
    }
}
复制代码

 

以下代码在cloud-consumer-feign项目中
2、FeignTestClient——定义Feign客户端,声明式接口与ProducerController服务提供的方法一一对应

复制代码
//定义Feign客户端,value参数为provider的serviceName。name参数实际是value的别名
//@FeignClient("mima-cloud-producer")与@FeignClient(name="mima-cloud-producer")本质相同
//@FeignClient(url="")参数已经作废,必须使用name属性
//如果设置url属性, 则name属性则只代表Feign客户端的别名,而不代表服务端的serviceName
@FeignClient(name="mima-cloud-producer")
public interface FeignTestClient {

    // 可以使用GetMapping组合注解,以前是不能使用的
    @GetMapping(value = "/get/{id}")
    // @PathVariable必须指定value,否则异常:PathVariable annotation was empty on param 0.
    public String get(@PathVariable("id") String id);

    @RequestMapping(value = "/getuser/{id}")
    public User getUser(@PathVariable("id") String id);
    
    // 调用远程的post方法,如果参数为复杂对象,就算指定了method=RequestMethod.GET,依然会使用post方式请求
    // 远程的方法是get的时候就会失败,错误消息: status 405 reading FeignTestClient#getUser2(User); content:{"timestamp":1511326531240,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/getuser2"}
    @RequestMapping(value = "/getuser2", method = RequestMethod.GET)
    public User getUser2(User user);

    // 调用远程的post方法,可以使用@RequestBody
    @RequestMapping(value = "/postuser")
    public User postUser(@RequestBody User user);

    // 调用远程的post方法,可以不使用@RequestBody
    @RequestMapping(value = "/postuser")
    public User postUser2(User user);

    // 调用远程的post方法,如果参数为复杂对象,就算指定了method=RequestMethod.GET,依然会使用post方式请求
    // 远程的方法也是post的,所以可以调用成功
    @RequestMapping(value = "/postuser", method = RequestMethod.GET)
    public User postUser3(User user);

    @GetMapping(value = "/listAll")
    List<User> listAll();
}
复制代码

 

3、FeignTestController——调用Feign客户端

复制代码
@RestController
public class FeignTestController {
    
    @Autowired
    private FeignTestClient feignTestClient;
    
    @GetMapping("/feign/get/{id}")
    public String get(@PathVariable String id) {
        String result = feignTestClient.get(id);
        return result;
    }
    
    @GetMapping("/feign/getuser/{id}")
    public User getUser(@PathVariable String id) {
        User result = feignTestClient.getUser(id);
        return result;
    }
    
    @GetMapping("/feign/getuser2")
    public User getUser2(User user) {
        User result = feignTestClient.getUser2(new User());
        return result;
    }
    
    @GetMapping("/feign/listAll")
    public List<User> listAll() {
        return feignTestClient.listAll();
    }
    
    @PostMapping("/feign/postuser")
    public User postUser(@RequestBody User user) {
        User result = feignTestClient.postUser(user);
        return result;
    }
    
    @PostMapping("/feign/postuser2")
    public User postUser2(@RequestBody User user) {
        User result = feignTestClient.postUser2(user);
        return result;
    }
    
    @PostMapping("/feign/postuser3")
    public User postUser3(@RequestBody User user) {
        User result = feignTestClient.postUser3(user);
        return result;
    }
    
}
复制代码

 

4、开启Feign注解

复制代码
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启Feign支持
public class ConsumerApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
    
}
复制代码

 

posted on   Ruthless  阅读(2848)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
历史上的今天:
2011-12-27 四十五、android camera
2011-12-27 四十四、Android之android:layout_weight详解
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示