SpringCloud學習筆記:宣告式呼叫Feign(4)

libingql發表於2018-05-10

1. Feign簡介

  Feign採用宣告式API介面的風格,將Java HTTP客戶端繫結到它的內部。

  Feign的首要目標是簡化Java HTTP客戶端呼叫過程。

2.Feign客戶端示例

  

  Feign依賴:spring-cloud-starter-feign

  Eureka Client依賴:spring-cloud-starter-eureka

  Web依賴:spring-cloud-starter-web

  Spring Boot測試依賴:spring-boot-starter-test

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>libing</groupId>
    <artifactId>eureka-feign-client</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>eureka-feign-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>libing</groupId>
        <artifactId>libing-eureka</artifactId>
        <version>1.0.0</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
    </dependencies>

</project>
pom.xml

  開啟Feign Client功能:@EnableFeignClients

package libing.eurekafeignclient;

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

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class EurekaFeignClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaFeignClientApplication.class, args);
    }

}

  FeignConfig類加註解@Configuration,表明該類是一個配置類,並注入一個BeanName為feignRetryer的Bean。

  組人該Bean之後,Feign在遠端呼叫失敗之後會進行重試。

package libing.eurekafeignclient.common;

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

import java.util.concurrent.TimeUnit;

@Configuration
public class FeignConfig {

    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1), 5);
    }

}

  DictClient介面:在介面上加上@FeignClient註解來宣告一個Feign Client。其中,

    value:遠端呼叫其他服務的服務名

    FeignConfig.class:Feign Client配置類

package libing.eurekafeignclient.client;

import libing.eurekafeignclient.client.model.Province;
import libing.eurekafeignclient.common.FeignConfig;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@FeignClient(value = "libing-dict-api", configuration = FeignConfig.class)
public interface DictClient {

    @GetMapping(value = "/dict/province")
    List<Province> getProvinceList();

}

  DictClientService類:注入EurekaClientFeign的Bean,通過EurekaClientFeign呼叫。

package libing.eurekafeignclient.service;

import libing.eurekafeignclient.client.DictClient;
import libing.eurekafeignclient.client.model.Province;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictClientService {

    @Autowired
    DictClient dictClient;

    public List<Province> getProvinceList(){
        return dictClient.getProvinceList();
    }

}

  DictFeignController.java

package libing.eurekafeignclient.controller;

import libing.eurekafeignclient.client.model.Province;
import libing.eurekafeignclient.service.DictClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "dictfeign")
public class DictFeignController {

    @Autowired
    DictClientService dictClientService;

    @GetMapping(value = "/getProvinceList")
    public List<Province> getProvinceList(){
        return dictClientService.getProvinceList();
    }

}

  @FeignClient註解用於建立宣告式API介面,該介面是RESTful風格。

  Feign被設計成插拔式,可以注入其他元件和Feign一起使用。

  Feign可以和Ribbon相結合進行負載均衡。

 

  示例程式碼:libing-eureka-feignclient.zip

相關文章