Feign really right way to use--for old project calls

Source: Internet
Author: User
Tags bind json

Feign is the calling framework for service consumption in spring cloud, typically used in combination with Ribbon,hystrix.

However, in some projects, the entire system is not a spring cloud project, or even a spring project, due to legacy reasons, and the focus of the user is simply to simplify the writing of the HTTP calling code.

If you use a relatively heavy frame such as httpclient or okhttp, coding and learning curve will be a challenge for beginners, and using spring resttemplate, there is no configured solution, so you can get out of spring cloud , using feign independently. maven Dependency

<dependency>
    <groupId>com.netflix.feign</groupId>
    <artifactid>feign-core</ artifactid>
    <version>8.18.0</version>
</dependency>
Custom Interfaces
Import feign. Param;
Import feign. Requestline;

Public interface Remoteservice {
    
    @RequestLine ("Get/users/list?name={name}")
    String GetOwner (@Param (value = "Name") String name);
}

Specifying the HTTP protocol and URL address configuration classes via @requestline

Remoteservice service = Feign.builder ()
            . Options (new options (+ 3500))
            . Retryer (New Retryer.default (5000, 3)
            . Target (Remoteservice.class, "http://127.0.0.1:8085");

The options method specifies the connection timeout length and the response timeout length, the Retryer method specifies the retry policy, the target method binds the interface to the server-side address. Returns the type of the interface type that is bound. called

String result = Service.getowner ("Scott");

Invokes the feign wrapper interface in the same way that the local method is called, directly getting the return value provided by the remote service. attached: Service producer

Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping (value= "users") public
class Usercontroller {
    
    @RequestMapping (value= "/list ", method={requestmethod.get,requestmethod.post,requestmethod.put})
    @ResponseBody public
    String list (@ Requestparam String name) throws interruptedexception{
        return Name.touppercase ();
    }
}
go further (the front content can be ignored just look down here)

In a project, the data exchanged between the service consumer and the production end is often one or more objects, and feign also provides a JSON-based object conversion tool that allows us to interact directly with objects. Business Interface

Public interface Remoteservice {
    
    @Headers ({"Content-type:application/json", "Accept:application/json"})
    @ Requestline ("post/users/list")
    user getOwner (user user);

Adding @headers annotations, specifying Content-type as JSON configuration

Remoteservice service = Feign.builder ()
                . Encoder (New Jacksonencoder ()).
                Decoder (new Jacksondecoder ())
                . Options (new options (3500)).
                Retryer (New Retryer.default (3))
                . Target (Remoteservice.class, "http://127.0.0.1:8085");

ENCODER specifies how the object is encoded, decoder specifies how the object is decoded. This is based on Jackson's codec, and the need to add Jackson's dependency in Pom.xml

<dependency>
    <groupId>com.netflix.feign</groupId>
    <artifactid>feign-jackson </artifactId>
    <version>8.18.0</version>
</dependency>
called
User result = Service.getowner (u);
attached: Service producer
@Controller
@RequestMapping (value= "users") public
class Usercontroller {
    
    @RequestMapping (value= "/list ", method={requestmethod.get,requestmethod.post,requestmethod.put})
    @ResponseBody public
    User list (@ Requestbody user user) throws interruptedexception{
        System.out.println (User.getusername ());
        User.setid (100L);
        User.setusername (User.getusername (). toUpperCase ());
        return user;
    }
}

The only change is the use of @requestbody to receive JSON-formatted data. Related Documents

Author: samhxm
Links: https://www.jianshu.com/p/3d597e9d2d67
Source: Pinterest

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.