微服務Spring Cloud (三)遠程調用 Feign

轉載請表明出處 https://blog.csdn.net/Amor_Leo/article/details/87877080謝謝

Spring Cloud Feign 概述

Spring Cloud Feign 基於Netflix Feign 整合了Spring Cloud Ribbon與Spring Cloud Hystrix,還提供了一種聲明式的web服務客戶端的定義方式,具備可插拔的註解,包括Feign註解和JAX-RS註解,還支持可插拔的HTTP編碼器與解碼器.

Spring Cloud Feign 搭建

  • pom
 <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.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
  • Application類上
@EnableDiscoveryClient
@EnableFeignClients  // 開啓Feign
  • 自定義Controller
@RestController
public class ConsumerController {
  @Autowired
  private FeignClient feignClient;

  @GetMapping("/{id}")
  public String findById(@PathVariable Long id) {
    return this.feignClient.findById(id);
  }
}

基本

  • yml
server:
  port: 8010
spring:
  application:
    name: consumer-service
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

單個參數請求

  • 制定Feign接口
@FeignClient(name = "provider-service")  //聲明需要調用的服務提供者的名稱
public interface FeignClient {
	
	 /** 
	 * 與提供者在Controller定義的一樣請求方式和請求路徑
     * 通過編寫"翻譯器"(Contract),可以讓Feign知道第三方註解的含義
     * 同樣,SpringCloud也提供了翻譯器,會將註解告訴Feign,因此,接口可以直接使用該註解
     * 默認支持註解:@RequestMapping、@RequestParam、@RequestHeader、@PathVariable
     */
    @RequestMapping(value = "/provider/{id}", method = RequestMethod.GET)
    public String findById(@PathVariable("id") Long id);
}

多參數請求

  • 自定義Feign接口
    使用Feign構造多參數請求:1.使用post傳遞對象;2.get在參數列表上分別一個一個,隔開的傳輸;3.get 傳遞map
@FeignClient(name = "provider-service")
public interface UserFeignClient {

  //該請求不會成功,錯誤代碼
  @RequestMapping(value = "/provider/get", method = RequestMethod.GET)
  public User get0(User user);

  @RequestMapping(value = "/provider/get", method = RequestMethod.GET)
  public User get1(@RequestParam("id") Long id, @RequestParam("username") String username);

  //傳遞Map<String, Object> map  提供者可以使用在代碼裏使用json轉換成map
  @RequestMapping(value = "/provider/get", method = RequestMethod.GET)
  public User get2(@RequestParam("map") String map);

  //如果是傳遞List,是不會成功的 傳遞數組可以
  @RequestMapping(value = "/provider/info/get", method = RequestMethod.GET)
  public User get3(@RequestParam("ids") String[] ids);

  @RequestMapping(value = "/provider/post", method = RequestMethod.POST)
  public User post(@RequestBody User user);
}

使用Feign註解

  • yml
server:
  port: 8010
spring:
  application:
    name: consumer-service
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
  • 自定義配置類
/**
 * 該類爲Feign的配置類
 * 注意:該不應該在主應用程序上下文的@ComponentScan中
 */
@Configuration
public class FeignConfiguration {
  /**
   * 將契約改爲feign原生的默認契約。這樣就可以使用feign自帶的註解了。
   * @return 默認的feign契約
   */
  @Bean
  public Contract feignContract() {
    return new feign.Contract.Default();
  }
}
  • 制定Feign接口
/**
 * 使用@FeignClient的configuration屬性,指定feign的配置類。
 */
@FeignClient(name = "provider-service",configuration = FeignConfiguration.class)
public interface FeignClient {

  /**
   * 使用feign自帶的註解@RequestLine
   */
    @RequestLine("GET /provider/{id}")
    public String findById(@Param("id") Long id);

}

Feign日誌配置

  • 自定義配置類
@Configuration
public class FeignLogConfiguration {
  @Bean
  Logger.Level feignLoggerLevel() {
    return Logger.Level.BASIC;
  }
}
  • 制定Feign接口
/**
 * 使用@FeignClient的configuration屬性,指定feign的配置類。
 */
@FeignClient(name = "provider-service",configuration = FeignLogConfiguration.class)
public interface FeignClient {

  /**
   * 使用feign自帶的註解@RequestLine
   */
    @RequestMapping(value = "/provider/{id}", method = RequestMethod.GET)
	public String findById(@PathVariable("id") Long id);

}
  • yml
server:
  port: 8010
spring:
  application:
    name: consumer-service
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
logging:
  level:
    com.amor.cloud.feign.FeignClient: DEBUG # 將Feign接口的日誌級別設置成DEBUG,因爲Feign的Logger.Level只對DEBUG作出響應。

Feign壓縮配置

feign.compression.request.enabled:設置爲true開啓請求壓縮
feign.compression.response.enabled:設置爲true開啓響應壓縮
feign.compression.request.mine-types:數據類型列表,默認爲text/xml,application/xml,application/json
feign.compression.request.min-request-size:設置請求內容的最小閥值,默認2048
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章