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);
    }
    
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值