spring cloud feign學(xué)習(xí)一:快速入門(mén)

Feign是什么?官網(wǎng)的一段話。

Feign is a declarative(聲明式) web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable(可插拔) annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates(集成) Ribbon and Eureka to provide a load balanced http client when using Feign.

通過(guò)對(duì)spring cloud ribbonspring cloud hystrix的介紹,我們已經(jīng)掌握了開(kāi)發(fā)微服務(wù)應(yīng)用時(shí)的兩個(gè)重磅武器,學(xué)會(huì)了如何在微服務(wù)架構(gòu)中實(shí)現(xiàn)客戶端負(fù)載均衡的服務(wù)調(diào)用以及如何通過(guò)斷路器來(lái)保護(hù)我們的微服務(wù)應(yīng)用。這二個(gè)組件都被廣泛地應(yīng)用在各個(gè)微服務(wù)實(shí)現(xiàn)中,不僅包括我們自身的業(yè)務(wù)類微服務(wù),也包括一些基礎(chǔ)設(shè)施類微服務(wù)(比如網(wǎng)關(guān))。此外,在實(shí)踐中,我們發(fā)現(xiàn)幾乎對(duì)這二個(gè)框架的使用幾乎都是同時(shí)出現(xiàn)的。既然如此,那么是否有更高層次的封裝來(lái)整合這二個(gè)基礎(chǔ)工具以簡(jiǎn)化開(kāi)發(fā)呢?spring cloud feign就是一個(gè)這樣的工具。它基于Netfix Feign實(shí)現(xiàn),整合了spring cloud Ribbonspring cloud Hystrix,除了提供這二者的強(qiáng)大功能之外,它還提供了一種生命式的web服務(wù)客戶端定義方式。

我們?cè)谑褂?code>spring cloud ribbon時(shí),通常會(huì)利用它對(duì)rersttemplate的請(qǐng)求攔截來(lái)實(shí)現(xiàn)對(duì)依賴服務(wù)的接口調(diào)用,而RestTemplate已經(jīng)實(shí)現(xiàn)了對(duì)http請(qǐng)求的封裝處理,形成了一套模版花的調(diào)用方法。之前已經(jīng)介紹了RestTemplate調(diào)用的實(shí)現(xiàn),但是在實(shí)際開(kāi)發(fā)中,由于對(duì)服務(wù)依賴的調(diào)用可能不止于一處,往往一個(gè)接口會(huì)被多處調(diào)用,所以我們通常都會(huì)針對(duì)每個(gè)微服務(wù)自行封裝一些客戶端類來(lái)包裝這些依賴服務(wù)的調(diào)用。這個(gè)時(shí)候我們發(fā)現(xiàn),由于RestTemplate的封裝,幾乎每一個(gè)調(diào)用都是簡(jiǎn)單的模版化內(nèi)容。

綜合上述所說(shuō),spring cloud feign在此基礎(chǔ)上做了進(jìn)一步封裝,由他來(lái)幫助我們定義和實(shí)現(xiàn)依賴服務(wù)接口的定義。在spring cloud feign的實(shí)現(xiàn)下,我們只需創(chuàng)建一個(gè)接口并調(diào)用注解的方式來(lái)配置它,即可完成對(duì)服務(wù)提供方的接口綁定,簡(jiǎn)化了使用spring cloud ribbon時(shí)自動(dòng)封裝服務(wù)調(diào)用客戶端的開(kāi)發(fā)量。spring cloud feign具備可插拔的注解支持,包括feign注解和JAX-RS注解。同時(shí),為了適應(yīng)Spring的廣大用戶,它在Netfix Feign的基礎(chǔ)上擴(kuò)展了spring mvc的注解支持,這對(duì)于習(xí)慣spring mvc的開(kāi)發(fā)者來(lái)說(shuō),無(wú)疑是個(gè)好消息,因?yàn)檫@樣可以大大減少學(xué)習(xí)使用它的成本。另外,對(duì)于feign自身的一些主要組件,比如說(shuō)編碼器和解碼器等,它也以可插拔的方式提高,在有需要的時(shí)候我們可以方便地?cái)U(kuò)展和替換它們。

快速入門(mén)

  • 創(chuàng)建pay-service服務(wù),加入依賴,與之前的模塊不一樣的就是加入了spring-cloud-starter-feign依賴,
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
    </dependencies>
  • 創(chuàng)建主體應(yīng)用類,并在主體應(yīng)用類上加上注解@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class PayApplication {

    public static void main(String[] args) {
        SpringApplication.run(PayApplication.class,args);
    }
}
  • 定義UserService接口,通過(guò)@FeignClient("user-service")注解指定服務(wù)名來(lái)綁定服務(wù),然后在使用Spring mvc的注解綁定具體的user-service服務(wù)中提供的rest接口。
@FeignClient("user-service")
public interface UserService {

    @RequestMapping(value = "/user/index",method = RequestMethod.GET)
    String index();

    @RequestMapping(value = "/user/hello",method = RequestMethod.GET)
    String hello();
}

這里,這里的服務(wù)名不區(qū)分大小寫(xiě),所以user-serviceUSER_SERVICE都可以的。另外,在Camden.SR7版本中,原本的serviceId屬性已經(jīng)被廢棄了,若要寫(xiě)屬性名,可以使用namevalue

  • 接著,創(chuàng)建一個(gè)PayController來(lái)實(shí)現(xiàn)對(duì)feign客戶端的調(diào)用,使用@Autowired注解直接注入上面定義的UserService實(shí)例,并在相應(yīng)的方法中調(diào)用這個(gè)綁定了user-service服務(wù)接口的客戶端來(lái)向改服務(wù)發(fā)起接口的定義。
@RestController
@RequestMapping("/pay")
public class PayController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    UserService userService;

    @RequestMapping("/index")
    public String index(){
        return userService.index();
    }

    @RequestMapping("/hello")
    public String hello(){
        return userService.hello();
    }
}
  • 最后,同ribbon實(shí)現(xiàn)的服務(wù)消費(fèi)一樣,需要在application.yml中指定服務(wù)注冊(cè)中心,
spring:
  application:
    name: pay-service
eureka:
  client:
    service-url:
     defaultZone: http://localhost:8761/eureka
  instance:
    instance-id:  ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
server:
  port: 7070
  • 測(cè)試驗(yàn)證Ribbon客戶端負(fù)載均衡,同時(shí)啟動(dòng)服務(wù)注冊(cè)和user-servcei,user-service啟動(dòng)了二個(gè)實(shí)例,然后啟動(dòng)pay-service
Eureka控制面板

發(fā)送多個(gè)請(qǐng)求http://192.168.5.3:7070/pay/index,發(fā)現(xiàn)二個(gè)user-service都能在控制臺(tái)打印日志,我們看到了feign實(shí)現(xiàn)的消費(fèi)者,依然是利用了Ribbon維護(hù)了user-service的服務(wù)列表信息,并且通過(guò)輪詢實(shí)現(xiàn)了客戶端的負(fù)載均衡。而與Ribbon不同的是,通過(guò)feign我們只需要定義服務(wù)綁定接口,以聲明式的方法,優(yōu)雅而簡(jiǎn)單的實(shí)現(xiàn)了服務(wù)調(diào)用。

注意
我們知道為了適應(yīng)Spring的廣大用戶,它在Netfix Feign的基礎(chǔ)上擴(kuò)展了spring mvc的注解支持,但是springmvc4.0出現(xiàn)的一系列注解比如@GetMapping,@PostMapping,@PutMapping等等是Feign是不支持的,比如在pay項(xiàng)目中定義的UserService接口,如果在

@FeignClient(value = "user-service")
public interface UserService {

//    @RequestMapping(value = "/user/index",method = RequestMethod.GET)
    @GetMapping("/user/index")
    String index();
...

在啟動(dòng)的時(shí)候就會(huì)拋出如下的異常


當(dāng)然這些所謂的坑在開(kāi)發(fā)階段便會(huì)發(fā)現(xiàn)。

參考資料
Declarative REST Client: Feign

代碼地址
代碼地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容