Feign是一個聲明式的模板化的Http客戶端,其作用與RestTemplate類似,能夠使用HTTP請求訪問遠程服務??梢岳斫鉃镽estTemplate的一種簡化方案。Open Feign是Spring Cloud 基于Netflix Feign的基礎上開發(fā)的聲明式服務調用組件,支持Spring MVC注解,可以像Spring Web一樣使用HttpMessageConverters等。
簡單使用
在項目中添加新模塊openfeign,選擇以下依賴
修改配置文件application.properties,將服務注冊到Eureka
spring.application.name=openfeign
server.port=4000
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
在啟動類添加@EnableFeignClients注解,開啟Feign的支持
定義HelloService接口,使用OpenFeign:
@FeignClient("provider")
public interface HelloService {
@GetMapping("/hello")
String hello();
}
最后在HelloController中調用HelloService進行測試,provider和OpenFeign都注冊到Eureka上之后就可以通過OpenFeign調用provider中的方法了。
參數傳遞
OpenFeign參數傳遞特點:
1.參數一定要綁定參數名。
2.如果通過header傳參數,一定要中文轉碼。
在provider中提供服務接口,在OpenFeign中添加調用
在HelloService中添加調用接口,凡是KeyValue類型的參數一定要記得綁定參數名稱
@FeignClient("provider")
public interface HelloService {
@GetMapping("/hello")
String hello();
@GetMapping("/hello2")
String hello2(@RequestParam("name") String name);
@PostMapping("/user2")
User addUser(@RequestBody User user);
@DeleteMapping("/user2/{id}")
void deleteUserById(@PathVariable("id") Integer id);
@GetMapping("/user3")
void getUserByName(@RequestHeader("name") String name);
}
在controller中添加測試接口,放在header中的參數一定要編碼之后再傳遞
@GetMapping("/hello")
public String hello() throws UnsupportedEncodingException {
String s = helloService.hello2("openfeign");
System.out.println(s);
User user = new User();
user.setId(1);
user.setUsername("zby");
user.setPassword("123");
User u = helloService.addUser(user);
System.out.println(u);
helloService.deleteUserById(1);
helloService.getUserByName(URLEncoder.encode("萊昂納多","UTF-8"));
return helloService.hello();
}
繼承特性
在使用聲明式feign客戶端工具的時候,因為書寫的方式代碼可能會產生重復??梢詫rovider和OpenFeign中的公共部分提取出來,一起使用減少代碼。
新建一個Model,hello-api,由于這個模塊需要被其他模塊所依賴,所以新建一個Maven項目(Spring Boot項目依賴比較麻煩),這個模塊也需要使用Spring MVC中的內容,因此創(chuàng)建成功后在項目中添加web依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
然后定義公共接口,也就是provider和OpenFeign中的公共模塊
public interface IUserService {
@GetMapping("/hello")
String hello();
@GetMapping("/hello2")
String hello2(@RequestParam("name") String name);
@PostMapping("/user2")
User addUser(@RequestBody User user);
@DeleteMapping("/user2/{id}")
void deleteUserById(@PathVariable("id") Integer id);
@GetMapping("/user3")
void getUserByName(@RequestHeader("name") String name);
}
定義完成后在provider和OpenFeign中添加該模塊的依賴
<dependency>
<groupId>com.example</groupId>
<artifactId>hello-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
添加成功后在provider中實現(xiàn)這些接口,由于公共模塊中已經定義了接口地址,所以實現(xiàn)接口方法時可以不用再添加請求注釋直接重寫接口中的方法即可。
@Override
public String hello(){
return "hello cloud"+port;
}
@Override
public String hello2(String name){
System.out.println(new Date()+">>>"+name);
return "hello "+name;
}
@Override
public User addUser2(@RequestBody User user){
return user;
}
@Override
public void deleteUser2(@PathVariable Integer id){
System.out.println(id);
}
@Override
public void getUserByName(@RequestHeader String name) throws UnsupportedEncodingException {
System.out.println(URLDecoder.decode(name,"UTF-8"));
}
在OpenFeign中定義接口,繼承自公共接口
@FeignClient("provider")
public interface HelloService extends IUserService {
}
- 這樣就可以更方便的調用接口和更改接口名稱,使服務端和客戶端代碼統(tǒng)一,避免因為接口名稱不對造成的錯誤,缺點則是提高了代碼耦合度
- 參數傳遞方式在使用繼承特性之后依然不變
日志
OpenFeign中可以通過配置日志查看整個請求調用的過程。日志級別分四種:
- NONE:不開啟日志(默認方案)
- BASIC:記錄請求方法、URL、響應狀態(tài)碼、執(zhí)行時間
- HEADER: 在BASIC的基礎上加載請求、響應頭
- FULL:在HEADER基礎上增加body及請求元素
可以在啟動類中,通過Bean來配置
@Bean
Logger.Level loggerLevel(){
return Logger.Level.FULL;
}
在配置文件中將日志級別調整至debug
logging.level.com.example.openfeign.service.HelloService=debug
之后重啟OpenFeign發(fā)送請求即可看到日志。
數據壓縮相關配置
#開啟請求的數據壓縮
feign.compression.request.enabled=true
#開啟請求的響應壓縮
feign.compression.response.enabled=true
#壓縮的數據類型
feign.compression.request.mime-types=text/html,application/json
#壓縮的數據下限(當要傳輸數據大于2048時進行數據壓縮)
feign.compression.request.min-request-size=2048
服務降級
首先定義服務降級的方法,這個方法同樣實現(xiàn)hello-api中的IUserService接口。使用@Component將該方法注冊為一個組件,為防止請求地址重復還要再添加一個@RequestMapping注解
@Component
@RequestMapping("/fallback")//防止請求接口重復
public class HelloServiceFallback implements HelloService {
@Override
public String hello() {
return "error";
}
@Override
public String hello2(String name) {
return "error2";
}
@Override
public User addUser2(User user) {
return null;
}
@Override
public void deleteUser2(Integer id) {
}
@Override
public void getUserByName(String name) throws UnsupportedEncodingException {
}
}
在HelloService的@FeignClient注解中配置服務降級類
@FeignClient(value = "provider",fallback = HelloServiceFallback.class)
最后在配置文件中開啟hystrix
feign.hystrix.enabled=true
這樣在請求失敗時會自動調用HelloServiceFallback中的方法