天天看點

微服務學習之OpenFeign【Hoxton.SR1版】

目錄

1 Feign(OpenFeign)是什麼

2 Feign能幹什麼

3 Feign與OpenFeign的差別

4 使用

4.1 pom.xml

4.2 application.yml

4.3 啟動類

4.4 業務類

5 關于日志

6 Feign逾時控制

1 Feign(OpenFeign)是什麼

官網介紹:https://cloud.spring.io/spring-cloud-openfeign/reference/html/。

Feign是一個聲明式WebService用戶端,使用Feign能讓編寫WebService用戶端更加簡單。

Feign的使用方法是:定義一個服務接口,并在上面添加注解。Feign可以與Eureka、Ribbon組合使用進而支援負載均衡。

兩個注解:1)@EnableFeignClients 啟動Feign用戶端 2)@FeignClient 定義Feign用戶端。

微服務學習之OpenFeign【Hoxton.SR1版】

2 Feign能幹什麼

Feign緻力于讓編寫Http用戶端更加簡單。

之前部落格中使用Ribbon+RestTemplate組合,利用RestTemplate對http請求的封裝處理,形成一套模闆化的封裝方法。

但是實際開發中,大多數時候對服務依賴的調用并不止一處,往往一個接口會被多處調用,是以通常都會針對每個微服務自行封裝一些用戶端類來包裝這些依賴服務的調用。Feign就是在這個基礎上做了進一步的封裝,由它來定義并實作依賴服務接口的定義。

在Feign的實作下,開發者隻需要建立一個接口并使用注解的方式進行配置,即可完成對服務提供方接口的綁定,簡化了使用SpringCloud Ribbon時,自動封裝服務調用調用用戶端的開發量。(此前dao接口上标注Mapper注解,現在是在一個微服務接口上面标注一個Feign注解即可)

Feign內建了Ribbon。

微服務學習之OpenFeign【Hoxton.SR1版】

3 Feign與OpenFeign的差別

微服務學習之OpenFeign【Hoxton.SR1版】

4 使用

4.1 pom.xml

<?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>cloud2020</artifactId>
        <groupId>com.bighuan.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-order80</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>
        <!--引入自定義的api通用包,可以使用Payment支付Entity-->
        <dependency>
            <groupId>com.bighuan.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.bighuan.springcloud.OrderFeignMain80</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
           

4.2 application.yml

server:
  port: 80
spring:
  application:
    name: cloud-order-service
eureka:
  instance:
    instance-id: order${server.port}   #例如:order80
    prefer-ip-address: true  # 通路路徑可以顯示ip位址
  client:
    # 表示是否将自己注冊進Eureka Server,預設為true
    register-with-eureka: true
     # 是否從Eureka Server擷取已有的注冊資訊,預設為true,單節點無所謂,叢集必須設定為true,才能配合ribbon使用負載均衡
    fetch-registry: true
    service-url:
      # defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 叢集版
ribbon:
  # 處理請求的逾時時間,預設為1秒
  ReadTimeout: 5000
  # 連接配接建立的逾時時間,預設1秒
  ConnectTimeout: 5000
logging:
  level:
    # feign日志以什麼級别監控哪個接口
    #com.bighuan.springcloud.service.PaymentFeignService: debug
    com.bighuan.springcloud.service: debug


           

4.3 啟動類

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients // 激活Feign
public class OrderFeignMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain80.class, args);
    }
}
           

4.4 業務類

接口類:

@Component
// // 設定遠端服務,将一個遠端服務映射為一個本地方法調用
@FeignClient(name = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {

    /**
     * 查詢
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/payment/get/{id}")
    CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);

    @GetMapping(value = "/payment/feign/timeout")
     String paymentFeignTimeOut();
}
           

控制類:

@RestController
@Slf4j
public class OrderFeignController {

    @Resource
    private PaymentFeignService paymentFeignService;


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

    @GetMapping(value = "/consumer/payment/feign/timeout")
    public String paymentFeignTimeOut() {
        return paymentFeignService.paymentFeignTimeOut();
    }
}
           

5 關于日志

Feign提供了日志列印功能,可通過配置調整日志列印級别,進而了解Feign中Http請求的細節。

微服務學習之OpenFeign【Hoxton.SR1版】

配置示例:

@Configuration
public class FeignConfig {
    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}
           

yml檔案中需要進行相應的配置,已在前面的檔案中貼出。

6 Feign逾時控制

檢視這篇文章:https://www.cnblogs.com/WaterGe/p/11687118.html

繼續閱讀