SpringCloud服務調用之OpenFeign


 

 


 怎么使用?

注意:FeignClinet 是在消費段調用。Feign自帶負載均衡配置項

1.導入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>cloud2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-order80</artifactId>
    <description>訂單消費者之feign</description>

    <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-server</artifactId>
        </dependency>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-common</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>

</project>
 
View Code

 2.修改yml

YML
View Code

 3.主啟動類

package com.atguigu.springcloud;

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


@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderFeignMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain80.class, args);
    }
}
 
 
View Code

 4.總結

 


 OpenFeign默認等待1秒鍾,超過后報錯,OpenFeign默認支持Ribbon,YML文件里需要開啟OpenFeign客戶端超時控制

server:
  port: 80
eureka:
  client:
    register-with-eureka: false
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
# 設置feign客戶端超時時間(OpenFeign默認支持ribbon)
ribbon:
  # 指的是建立連接所用的時間,適用於網絡狀態正常的情況下,兩端連接所用的時間
  ReadTimeout: 5000
  # 指的是建立連接后從服務器讀取到可用資源所用的時間
  ConnectTimeout: 5000
 
View Code

OpenFeign日志打印功能

 日志級別:

 

 如果想在Openfeign里面使用日志服務的話,需要配置bean

package com.atguigu.springcloud.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * OpenFeignClient配置
 *
 * @author zzyy
 * @create 2020/3/6 18:02
 **/
@Configuration
public class FeignConfig {

    /**
     * feignClient配置日志級別
     *
     * @return
     */
    @Bean
    public Logger.Level feignLoggerLevel() {
        // 請求和響應的頭信息,請求和響應的正文及元數據
        return Logger.Level.FULL;
    }
}
 
 
View Code

 yml文件里面也是需要配置的

server:
  port: 80
eureka:
  client:
    register-with-eureka: false
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
# 設置feign客戶端超時時間(OpenFeign默認支持ribbon)
ribbon:
  # 指的是建立連接所用的時間,適用於網絡狀態正常的情況下,兩端連接所用的時間
  ReadTimeout: 5000
  # 指的是建立連接后從服務器讀取到可用資源所用的時間
  ConnectTimeout: 5000
logging:
  level:
    # feign日志以什么級別監控哪個接口
    com.atguigu.springcloud.service.PaymentFeignService: debug
 
View Code

日志如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM