【沉舟側畔千帆過,病樹前頭萬木春】---OpenFeign的各種騷操作 -VS- 既生瑜何生亮的感嘆,小Ribbon怎麼辦?

前言:

本篇博客主要是介紹OpenFeign的簡介與OpenFeign服務調用,日誌增強,超時控制等等,相關騷操作。


Feign是一個聲明式WebService客戶端。使用Feign能讓編寫Web Service客戶端更加簡單。它的使用方法是定義一個服務接口然後在上面添加註解,Feign也支持可拔插式的編碼器和解碼器。springcloud 對Feign進行了封裝,使其支持了Spring MVC 標準註解和HttpMeddageConverters.Feign可以與Eureka和Ribbon組合使用可以支持負載均衡。

Feign能幹什麼
Feign旨在使編寫javaHttp 客戶端變得更加容易。我們之前在其他博客裏面說過利用RestTemplate對Http請求的封裝處理,形成了一套模板化的調用方法,但是在實際開發中,由於對服務依賴的調用可能不止一處,往往一個接口會被多處調用,所以通常都會針對每個微服務自行封裝一些客戶端來包裝這些依賴服務的調用,所以Feign在此基礎上做了進一步封裝,由他來幫助我們定義和實現依賴服務接口的定義,在Feign的實現下,我們只需要創建一個接口並使用註解的方式來配置它(以前是Dao接口上面標註Mapper註解,現在是一個微服務接口上面標註一個Feign註解即可),即可完成服務提供的接口綁定,簡化了使用Spring cloud Ribbon時,自動封裝服務調用客戶端的開發量。
Feign集成Ribbon
利用Ribbon維護了Payment的服務列表信息,並且通過輪詢實現了客戶端的負載均衡,而與Ribbon不同的是,通過Feign只需要定義服務綁定接口且以聲明式的方法,優雅而簡單的實現了服務調用。
在這裏插入圖片描述

OpenFeign 服務調用

  • 新建module
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
  • 修改pom
<?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">
    <parent>
        <artifactId>com.zcw.springcloud2020508</artifactId>
        <groupId>com.zcw</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-order9082</artifactId>
    <dependencies>
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入自定義的api的通用包,可以使用Payment支付Entity-->
        <dependency>
            <groupId>com.zcw</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

</project>


  • 創建YML

server:
  port: 9082

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone:  http://server2:7008/eureka/,http://server1:7001/eureka/


  • 修改啓動類
package com.zcw.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @ClassName : OrderFeignMain9082
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-18 20:05
 */
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain9082 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain9082.class,args);
    }
}



  • 創建業務類
    業務類接口+@FeignClient配置調用provider服務
  • 接口+註解:
    新建PaymentFeignService接口並新增註解@FeignClient
    在這裏插入圖片描述
    在這裏插入圖片描述

package com.zcw.springcloud.service;

import com.zcw.springcloud.entities.CommonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @ClassName : PaymentFeignService
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-18 20:21
 */
@Component
@FeignClient(value = "CLOUD-PROVIDER-PAYMENT-ZCW-SERVICE")
public interface PaymentFeignService {

    @GetMapping(value = "/payment/get/{id}")
     CommonResult getPaymentById(@PathVariable("id")Long id );
}


  • controller
package com.zcw.springcloud.controller;

import com.zcw.springcloud.entities.CommonResult;
import com.zcw.springcloud.entities.Payment;
import com.zcw.springcloud.service.PaymentFeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName : OrderFeignController
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-18 20:33
 */
@RestController
public class OrderFeignController {
    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        return paymentFeignService.getPaymentById(id);
    }
}



  • 測試
    在這裏插入圖片描述
  • 總結
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章