1. Import OpenFeign dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.2.5. RELEASE</version>
</dependency>
Copy the code

Note that the version number here needs to be compatible with the SpringBoot version. The following address has the corresponding relationship between SpringCloud, SpringBoot and SpringCloud Alibaba version. Version mapping

2. Invoke the Http request using OpenFeign

The aggregated data API is used as an example for testing

The JSON data returned by the weather forecast interface is mapped to Java classes using the IDEA GsonFormatPlus plug-in

New WeatherResult class

Right click Generate

Choose GsonFormatPlus

Copy and paste the JSON data into the window and click OK

Select the field for the mapping

Finally, click OK to generate the Java field properties, which look like this

Create the WeatherFeignClient interface

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * com.example.learning.feignclient
 * Description:
 *
 * @author jack
 * @date2021/6/20 11:04 * /
@Component
@FeignClient(name = "weather", url = "http://apis.juhe.cn", fallback = WeatherFeignClientFactory.class)
public interface WeatherFeignClient {

    /** * Query weather forecast by city **@paramCity City name *@paramThe key secret key *@returnWeather forecast */
    @GetMapping(value = "/simpleWeather/query")
    WeatherResult getWeather(@RequestParam(value = "key") String key, @RequestParam(value = "city") String city);

}

Copy the code

New WeatherFeignClientFactory call failure tolerance

/**
 * com.example.learning.feignclient.fallback
 * Description:
 *
 * @author jack
 * @date2021/6/19 17:44 * /
@Component
public class WeatherFeignClientFactory implements WeatherFeignClient {


    @Override
    public WeatherResult getWeather(String key, String city) {
        return null; }}Copy the code

@ FeignClient properties

  • Url: URL is used for debugging. You can manually specify the address of the @FeignClient call
  • Name: Specifies the name of FeignClient. If the project uses the Ribbon, the name property will be used as the name of the microservice for service discovery
  • Configuration: Feign configuration class, you can customize Feign Encoder, Decoder, LogLevel, Contract
  • Fallback: Defines a fault-tolerant processing class that invokes the fault-tolerant logic of the corresponding interface when the remote interface fails or times out
  • FallbackFactory: Factory class used to generate examples of fallback classes. With this property we can implement fault-tolerant logic common to each interface and reduce repetitive code
  • Path: defines the unified prefix of the current FeignClient

3. The test

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * com.example.learning.feign
 * Description:
 *
 * @author jack
 * @date2021/6/20 11:29 * /
@SpringBootTest
public class WeatherFeignTest {

    @Resource
    private WeatherFeignClient weatherFeignClient;

    @Test
    public void getWeather(a) {
        String city = "Hangzhou";
        String key = "xxxxx"; WeatherResult weather = weatherFeignClient.getWeather(key, city); System.out.println(weather); }}Copy the code

Call result: