The first Feign program (Feign is not integrated in this chapter, but used separately)

Keywords: Java Spring github Apache

The first Feign program

 

1. Feign is integrated into Spring Cloud Netflix module. When Eureka and Ribbon are integrated, Feign has the function of load balancing. Feign itself is easy to use, and the integration with Spring Cloud will greatly reduce the workload of our development.


2. It is an open source project on Github. Its purpose is to simplify the development of Web Service client. When using Feign, annotations can be used to modify the interface. The modified interface has the ability to access web service. These annotations can either use Feign's own annotations or support the use of third-party annotations. Feign also supports plug-in encoders and decoders that allow users to encapsulate and parse requests and responses.

 

Feign will be more object-oriented. Let's use Feign to compare CXF. Before that, we need to be ready to provide an interface to the outside world.

If you don't have friends with projects such as interfaces, you can refer to the previous chapters. Ribbon is used in Spring Cloud (default polling rule + custom rule) "Build (just build servers and service providers)

 

 

Here we introduce all dependencies on CXF and Feign at once: pom.xml

<dependencies>
    <!-- CXF -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-client</artifactId>
        <version>3.1.0</version>
    </dependency>

    <!-- Feign -->
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-core</artifactId>
        <version>9.5.0</version>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-gson</artifactId>
        <version>9.5.0</version>
    </dependency>
</dependencies>

Let's write a CXF client and test it: CxfClient.java

public class CxfClient {
    
    public static void main(String[] args) throws IOException {
        // Establish WebClient
        WebClient client = WebClient.create("http://localhost:9090/getPoliceById/123");
        // Get response
        Response response = client.get();
        // Getting response content
        InputStream is = (InputStream) response.getEntity();
        String content = IOUtils.readStringFromStream(is);
        // Print results
        System.out.println("Request results:" + content);
    }

}

If there are many URLs in the WebClient.create method, it will be troublesome to maintain. Next, we write a Feign program to compare it with CXF.

 

At the beginning of the article, we mentioned that Feign programs will be more object-oriented, so we first create an entity class to receive the result object: Police.java

public class Police {
    
    private String id;// Police number, used to save user input parameters
    private String url;// Server handling requests url
    private String message;// Prompt information
    
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    
}

Secondly, we create an interface class: FeignService.java

public interface FeignService {
    
    @RequestLine("GET /getPolice")
    public String getPolice();
    
    @RequestLine("GET /getPolice")
    public Police getPoliceObj();
    
    @RequestLine("GET /getPoliceById/{id}")
    public String getPoliceById(@Param("id") String id);

}

Finally, we create a test class to see the results: TestMain.java

public class TestMain {

    public static void main(String[] args) {
        // Return string
        FeignService client = Feign.builder().target(FeignService.class, "http://localhost:9090");
        String policeStr = client.getPolice();
        System.out.println("===== Direct return:"+policeStr);
        String policeByIdStr = client.getPoliceById("123");
        System.out.println("===== according to id Return:"+policeByIdStr);
        
        // Return object
        FeignService client2 = Feign.builder().
                decoder(new GsonDecoder()).// Decoder
                target(FeignService.class, "http://localhost:9090");
        Police police = client2.getPoliceObj();
        System.out.println("===== Request result return object:");
        System.out.println("id: "+police.getId());
        System.out.println("url: "+police.getUrl());
        System.out.println("message: "+police.getMessage());
    }
}

Posted by jib on Sun, 27 Jan 2019 11:18:15 -0800