Feign是一个声明式 WebService 客户端,使用Feign可以让编写Web Service 客户端更加简单,它的使用方法是定义一个接口,而后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可插拔式的编码器和解码器。web
Spring Cloud 对 Fiegn 进行了封装,使其支持了Spring MVC 标准注解和HttpMessageConverts。Feign能够与Eureka和Ribbon组合使用以支持负载均衡。spring
前面使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,造成了一套模版化的调用方法,可是在实际的开发中,因为对服务依赖的调用可能不止一处。每每一个接口会被多处调用,因此一般会对每一个微服务自行封装一些客户端类来包装依赖服务的调用,因此Feign在此基础上作了进一步封装,有他来帮助咱们自定义和实现依赖服务接口的定义,在Feign的实现下,咱们只须要建立一个接口并使用注解的方式来配置他(之前是Dao接口上面标注Mapper注解,如今是一个微服务接口上面标注一个Feign注解便可),便可完成服务提供放的接口绑定,简化了使用Spring Cloud Ribbon时,自动封装服务调用客户端的开发量。api
新建一个消费者项目(Feign):microservicecloud-consumer-dept-feign浏览器
(1)添加pom.xml中的依赖app
<dependencies>
<dependency><!-- 本身定义的api -->
<groupId>com.yufeng.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- Ribbon相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 修改后当即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
(2)在service包下增长 FeignConsumerService 接口,并增长@FeignCilent负载均衡
@FeignClient(value = "microservicecloud-dept") public interface FeignConsumerService { @RequestMapping(value = "/dept/add", method = RequestMethod.POST) public boolean add(@RequestBody Dept dept); @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET) public Dept get(@PathVariable("id") Long id); @RequestMapping(value = "/dept/get/list", method = RequestMethod.GET) public List<Dept> list(); }
说明:@FeignClient的value值指明对哪一个微服务进行负载均衡spring-boot
@RequestMapping 是Spring提供的注解,这里能够直接使用之前使用SpringMVC时用过的各类注解,惟一不一样的是,这里只是把注解用在了接口上。
(3)在主启动类中增长 @EnableFeignClients 注解微服务
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class DeptConsumer80_Feign_App { public static void main(String[] args) { SpringApplication.run(DeptConsumer80_Feign_App.class, args); } }
(4)在controller包下增长 DeptController ,使用@Autowired直接注入上面定义的 FeignConsumerService接口对应的实例编码
@RestController public class DeptController { @Autowired private FeignConsumerService feignConsumerService; @RequestMapping(value = "/consumer/dept/get/{id}", method = RequestMethod.GET) public Dept get(@PathVariable("id") Long id) { return feignConsumerService.get(id); } @RequestMapping(value = "/consumer/dept/get/list", method = RequestMethod.GET) public List<Dept> list() { return feignConsumerService.list(); } }
(5)结果验证:启动eureka集群,启动3个生产者服务,启动本服务;spa
在浏览器中打开:http://localhost/consumer/dept/get/1,能够看到轮询调用了3个生产者服务;