生活随笔
收集整理的這篇文章主要介紹了
Feign 简介和使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
聲明式服務消費Feign 一、簡介 二、使用Feign實現服務消費者 三、實現普通的服務提供者 四、Feign服務調用測試 五、Feign消費者測試
一、簡介
Feign是Netflix公司開發的一個聲明式的REST調用客戶端 ; Ribbon負載均衡、Hystrix服務熔斷是我們Spring Cloud開發中非常基礎的組件,在使用過程中他們一般是同時出現的,配置也非常相似,每次開發都有許多相同的代碼。因此,Spring Cloud基于Netflix Feign整合了Ribbon和Hystrix兩個組件,讓我們的開發變的更加簡單。 Spring Cloud Feign :對Ribbon負載均衡、Hystrix服務熔斷進行了簡化,在其基礎上進行了進一步的封裝,不僅在配置上大大簡化了開發工作,同時還提供了一種聲明式的Web服務客戶端定義方式。
二、使用Feign實現服務消費者
創建SpringBoot工程 添加依賴 spring-cloud-starter-netflix-eureka-client spring-cloud-starter-openfeign spring-cloud-starter-netflix-hystrix 在入口函數上激活功能 @EnableEurekaClient @EnableFeignClients 配置文件
server
. port
= 80
spring
. application
. name
= feign
- eureka
- client
- consumer
eureka
. client
. service
- url
. defaultZone
= http
: // eureka7001
: 7001 / eureka
service接口
@FeignClient ( name
= "feign-eureka-client-provider" )
public interface TestService { @RequestMapping ( "/test" ) String
test ( ) ; @RequestMapping ( "/testParam01" ) String
testParam01 ( @RequestParam String name
, @RequestParam Integer age
) ; @RequestMapping ( "/testParam02" ) String
testParam02 ( @RequestBody User user
) ; @RequestMapping ( "/testReturnUser" ) User
testReturnUser ( ) ; @RequestMapping ( "/testReturnList" ) List
< User> testReturnList ( ) ;
}
controller調用遠程服務
@RestController
public class TestController { @Resource private TestService testService
; @RequestMapping ( "/test" ) public String
test ( ) { String result
= testService
. test ( ) ; return "使用了Feign的服務消費者..." + result
; } @RequestMapping ( "/testParam01" ) public String
testParam01 ( ) { String result
= testService
. testParam01 ( "lily" , 24 ) ; return "使用了Feign的服務消費者..." + result
; } @RequestMapping ( "/testParam02" ) public String
testParam02 ( ) { User user
= new User ( "xiaoming" , 23 ) ; String result
= testService
. testParam02 ( user
) ; return "使用了Feign的服務消費者..." + result
; } @RequestMapping ( "/testReturnUser" ) public String
testReturnUser ( ) { User user
= testService
. testReturnUser ( ) ; return "使用了Feign的服務消費者..." + user
. toString ( ) ; } @RequestMapping ( "/testReturnList" ) public String
testReturnList ( ) { List
< User> list
= testService
. testReturnList ( ) ; return "使用了Feign的服務消費者..." + list
. toString ( ) ; }
}
三、實現普通的服務提供者
創建SpringBoot工程 添加依賴 spring-cloud-starter-netflix-eureka-client 在入口函數上激活功能 @EnableEurekaClient 配置文件
server
. port
= 8001
spring
. application
. name
= feign
- eureka
- client
- provider
eureka
. client
. service
- url
. defaultZone
= http
: // eureka7001
: 7001 / eureka
controller提供服務
@RestController
public class TestController { @GetMapping ( "/test" ) public String
test ( ) { return "【服務提供者返回內容】" ; } @GetMapping ( "/testParam01" ) public String
testParam01 ( String name
, Integer age
) { return "【服務提供者返回內容 name:" + name
+ " age:" + age
+ "】" ; } @RequestMapping ( "/testParam02" ) public String
testParam02 ( @RequestBody User user
) { return "【服務提供者返回內容 name:" + user
. getName ( ) + " age:" + user
. getAge ( ) + "】" ; } @RequestMapping ( "/testReturnUser" ) public Object
testReturnUser ( ) { User user
= new User ( "小黃" , 23 ) ; return user
; } @RequestMapping ( "/testReturnList" ) public Object
testReturnList ( ) { List
< User> list
= new ArrayList < > ( ) ; User user1
= new User ( "小黃" , 23 ) ; User user2
= new User ( "小李" , 24 ) ; User user3
= new User ( "小明" , 25 ) ; list
. add ( user1
) ; list
. add ( user2
) ; list
. add ( user3
) ; return list
; }
}
四、Feign服務調用測試
訪問:http://localhost/test http://localhost/testParam01 訪問:http://localhost/testParam02 訪問:http://localhost/testReturnUser 訪問:http://localhost/testReturnList
五、Feign消費者測試
負載均衡
Spring Cloud提供了Ribbon來實現負載均衡,使用Ribbon直接注入一個RestTemplate對象即可,RestTemplate已經做好了負載均衡的配置;在Spring Cloud下,使用Feign也是直接實現負載均衡的,定義一個有@FeignClient注解的接口,然后使用@RequestMapping注解到方法上映射遠程的REST服務,此方法也是做好負載均衡配置的。
服務熔斷
在application.properties文件開啟feign對hystrix功能支持(支持熔斷)
feign.hystrix.enabled=true
指定熔斷回調邏輯
@FeignClient ( name
= "feign-eureka-client-provider" , fallback
= MyFallback
. class )
@FeignClient ( name
= "feign-eureka-client-provider" , fallbackFactory
= MyFallbackFactory
. class )
public interface TestService { . . . }
自定義異常熔斷器
@Component
public class MyFallback implements TestService { @Override public String
test ( ) { return "test請求熔斷" ; } @Override public String
testParam01 ( String name
, Integer age
) { return "testParam01請求熔斷" ; } @Override public String
testParam02 ( User user
) { return "testParam02請求熔斷" ; } @Override public User
testReturnUser ( ) { return null
; } @Override public List
< User> testReturnList ( ) { return null
; }
}
@Component
public class MyFallbackFactory implements FallbackFactory < TestService> { @Override public TestService
create ( Throwable throwable
) { return new TestService ( ) { @Override public String
test ( ) { System
. out
. println ( throwable
. getClass ( ) ) ; System
. out
. println ( throwable
. getMessage ( ) ) ; return "test請求熔斷" ; } @Override public String
testParam01 ( String name
, Integer age
) { return "testParam01請求熔斷" ; } @Override public String
testParam02 ( User user
) { return "testParam02請求熔斷" ; } @Override public User
testReturnUser ( ) { return null
; } @Override public List
< User> testReturnList ( ) { return null
; } } ; }
}
服務異常
@GetMapping ( "/test" ) public String
test ( ) { System
. out
. println ( 1 / 0 ) ; return "【服務提供者返回內容】" ; }
調用服務進行熔斷測試 http://localhost/test
總結
以上是生活随笔 為你收集整理的Feign 简介和使用 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。