Feign简介
Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。
使用
修改公共模块项目
公共模块项目名称为: microservice-common
pom.xml引入Feign依赖:
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
|
新建StudentClientService接口(新建一个service包并存放在里面),加入@FeignClient(value = “MICROSERVICE-STUDENT”)注解定义FeignClient并指定调用的服务名称MICROSERVICE-STUDENT
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| package com.ledao.service;
import com.ledao.entity.Student; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@FeignClient(value = "MICROSERVICE-STUDENT") public interface StudentClientService {
@RequestMapping("/student/get/{id}") Student get(@PathVariable("id") Integer id);
@RequestMapping("/student/list") List<Student> list();
@RequestMapping("/student/save") boolean save(Student student);
@RequestMapping("/student/delete/{id}") boolean delete(@PathVariable("id") Integer id); }
|
新建Feign消费者项目
新建一个Module,命名为:microservice-student-consumer-feign-80
将microservice-student-consumer-80消费者项目的文件都复制到新建的Feign消费者项目,将pom.xml引入的依赖也全部复制过去并引入feign依赖:
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
|
修改启动类
启动类如果也复制了过来,就重命名为:StudentConsumerFeignApplication_80,并添加注解:@EnableFeignClients
修改Controller
因为现在用Fiegn,所以把restTemplate去掉,改成注入service,调用service方法来实现服务的调用
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| package com.ledao.controller;
import com.ledao.entity.Student; import com.ledao.service.StudentClientService; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestClientException;
import javax.annotation.Resource; import java.util.List;
@RestController @RequestMapping("/student") public class StudentConsumerFeignController {
@Resource private StudentClientService studentClientService;
@RequestMapping("save") public boolean save(Student student) { return studentClientService.save(student); }
@SuppressWarnings("unchecked") @RequestMapping("/list") public List<Student> list() { return studentClientService.list(); }
@RequestMapping("/get/{id}") public Student get(@PathVariable("id") Integer id) { return studentClientService.get(id); }
@RequestMapping("/delete/{id}") public boolean delete(@PathVariable("id") Integer id) { try { studentClientService.delete(id); return true; } catch (RestClientException e) { return false; } } }
|
运行
启动三个Eureka服务注册中心和三个提供者服务,再启动上面新建的Feign消费者项目,然后浏览器地址栏输入:http://localhost/student/list ,如果看到返回的学生列表说明Feign配置成功