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);
    }
    
}
原文地址:https://www.cnblogs.com/linjiqin/p/10182773.html