SpringCloud学习笔记:声明式调用Feign(4)
1. Feign简介
Feign采用声明式API接口的风格,将Java HTTP客户端绑定到它的内部。
Feign的首要目标是简化Java HTTP客户端调用过程。
2.Feign客户端示例
Feign依赖:spring-cloud-starter-feign
Eureka Client依赖:spring-cloud-starter-eureka
Web依赖:spring-cloud-starter-web
Spring Boot测试依赖:spring-boot-starter-test
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>libing</groupId> <artifactId>eureka-feign-client</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>eureka-feign-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>libing</groupId> <artifactId>libing-eureka</artifactId> <version>1.0.0</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies> </project>
开启Feign Client功能:@EnableFeignClients
package libing.eurekafeignclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class EurekaFeignClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaFeignClientApplication.class, args); } }
FeignConfig类加注解@Configuration,表明该类是一个配置类,并注入一个BeanName为feignRetryer的Bean。
组人该Bean之后,Feign在远程调用失败之后会进行重试。
package libing.eurekafeignclient.common; import feign.Retryer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration public class FeignConfig { @Bean public Retryer feignRetryer() { return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1), 5); } }
DictClient接口:在接口上加上@FeignClient注解来声明一个Feign Client。其中,
value:远程调用其他服务的服务名
FeignConfig.class:Feign Client配置类
package libing.eurekafeignclient.client; import libing.eurekafeignclient.client.model.Province; import libing.eurekafeignclient.common.FeignConfig; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import java.util.List; @FeignClient(value = "libing-dict-api", configuration = FeignConfig.class) public interface DictClient { @GetMapping(value = "/dict/province") List<Province> getProvinceList(); }
DictClientService类:注入EurekaClientFeign的Bean,通过EurekaClientFeign调用。
package libing.eurekafeignclient.service; import libing.eurekafeignclient.client.DictClient; import libing.eurekafeignclient.client.model.Province; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class DictClientService { @Autowired DictClient dictClient; public List<Province> getProvinceList(){ return dictClient.getProvinceList(); } }
DictFeignController.java
package libing.eurekafeignclient.controller; import libing.eurekafeignclient.client.model.Province; import libing.eurekafeignclient.service.DictClientService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping(value = "dictfeign") public class DictFeignController { @Autowired DictClientService dictClientService; @GetMapping(value = "/getProvinceList") public List<Province> getProvinceList(){ return dictClientService.getProvinceList(); } }
@FeignClient注解用于创建声明式API接口,该接口是RESTful风格。
Feign被设计成插拔式,可以注入其他组件和Feign一起使用。
Feign可以和Ribbon相结合进行负载均衡。