Feign是从Netflix中分离出来的轻量级项目,能够在类接口上添加注释,成为一个REST API 客户端,Feign默认集成了Ribbon。
Feign中对 Hystrix 有依赖关系。Feign只是一个便利的rest框架,简化调用,最后还是通过ribbon在注册服务器中找到服务实例,然后对请求进行分配
Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求。
一、使用方式
1、首先写好需要注册进注册中心的服务接口UserServiceController,该接口相当于service层,只是以RestFULL接口的形式供远程调用
@RestController @RequestMapping("userService") public class UserServiceController { @GetMapping("getUser/{userId}") public User getUser(@PathVariable("userId") String userId){ System.out.println("获取User,更具UserId,查询User-->" + userId); User user = new User(); user.setUserName("Tom"); user.setPassword("123");return user; } @PostMapping("saveUser") public String saveUser(@RequestBody User user){ System.out.println("保存User--->" + JSON.toJSONString(user)); return "userId:010100100101"; } @GetMapping("deleteUser/{userId}") public Boolean deleteUser(@PathVariable("userId") String userId){ System.out.println("删除用户--->" + userId); return true; } @RequestMapping(value = "findUserByUserNameAndPassword") public User findUserByUserNameAndPassword(String userName, String password){ System.out.println("userService--->" + userName + " password--->" + password); User user = new User(); user.setUserName("Tomcat"); user.setPassword("123");return user; }
2、在请求远程服务的项目的POM.XML文件中引入对Feign依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
3、创建FeignClient
@FeignClient(value="springcloud-userservcie") public interface UserClient { @RequestMapping(value = "userService/getUser/{userId}", method = RequestMethod.GET) public User getUser(@PathVariable("userId") String userId); @RequestMapping(value = "userService/saveUser", method = RequestMethod.POST) public String saveUser(User user); @RequestMapping(value = "userService/deleteUser/{userId}", method = RequestMethod.GET) public Boolean deleteUser(@PathVariable("userId") String userId); @RequestMapping(value = "userService/findUserByUserNameAndPassword", method = RequestMethod.GET) public User findUserByUserNameAndPassword(@RequestParam("userName") String userName, @RequestParam("password") String password); }
- @FeignClient(value="springcloud-userservcie"):用于通知Feign组件对该接口进行代理(不需要编写接口实现),value属性指定我们要调用注册中心的服务ID。使用者可直接通过@Autowired注入。
- @RequestMapping表示在调用该方法时需要向/group/{groupId}发送请求。
- @PathVariable与SpringMVC中对应注解含义相同。
原理:Spring Cloud应用在启动时,Feign会扫描标有@FeignClient注解的接口,生成代理,并注册到Spring容器中。生成代理时Feign会为每个接口方法创建一个RequetTemplate对象,该对象封装了HTTP请求需要的全部信息,请求参数名、请求方法等信息都是在这个过程中确定的,Feign的模板化就体现在这里
4、在Controller调用
@RestController @RequestMapping("userClient") public class UserController { @Autowired private UserClient userClient; @RequestMapping(value = "getUser/{userId}", method = RequestMethod.GET) public User getUser(@PathVariable("userId") String userId){ return userClient.getUser(userId); } @RequestMapping(value = "saveUser", method = RequestMethod.POST) public String saveUser(User user){ return userClient.saveUser(user); } @RequestMapping(value = "deleteUser/{userId}", method = RequestMethod.GET) public Boolean deleteUser(@PathVariable("userId") String userId){ return userClient.deleteUser(userId); } @RequestMapping(value = "findUserByUserNameAndPassword") public User findUserByUserNameAndPassword(String userName, String password){ System.out.println("userClient--->" + userName + " password--->" + password); return userClient.findUserByUserNameAndPassword(userName, password); } }
5、启动类上添加Feign注解
@Configuration @ComponentScan @EnableAutoConfiguration @EnableEurekaClient @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }