Spring Cloud (Netflix) Feign: Use feign to expose services in Dubbo

Source: Internet
Author: User
Requirements Scenario

In a micro-service architecture, the way in which a requester of a service invokes a remote service is a problem that must be addressed. In the spring Cloud (Netflix) technology stack, each micro-service is exposed in the form of an HTTP rest interface, which normally requires an HTTP client to be used when executing a remote call, and then an HTTP request to the service. In fact, if you use Netflix's feign as an HTTP client, we can do it like Dubbo, the service's caller calls the interface method directly to invoke the remote service without having to parse the return data through a regular HTTP client-construct request. Solution

Netflix feign is a templated HTTP Client that allows you to "make" templates programmatically, and then, when you initiate an HTTP request, you do not need to specify the cumbersome information for the URL request header request parameters, such as:

@FeignClient (name = "EA") public
interface Advertgroupremoteservice {

    @RequestMapping (value = "/group/{ GROUPID} ")
    Advertgroupvo findbygroupid (@PathVariable (" GroupId ") Integer adgroupid);

In the code above we declare a "template" (actually an interface), a method findbygroupid in the template, and an annotation that defines the HTTP request information that the method needs to initiate (the annotation is identical to the SPRINGMVC). When used, we only need to inject advertgroupremoteservice in the service of the caller to execute the remote invocation like the local method :

@RestController public
class Testctr {

    @Autowired
    private Advertgroupremoteservice Advertgroupremoteservice;

    Public ADVERTGROUPVO FindByID (Integer ID) {return
        advertgroupremoteservice.findbygroupid (ID);
    }
}

Normally, the HTTP client should be used by the service's caller to initiate a request to the remote service, but the disadvantage is that the caller must know the HTTP rest interface of the remote service very well. As a workaround, we create an API package for each micro-service, which has only @feignclient declarations, and then introduces the API package through Maven on the calling side of the service, so The caller does not need to care about the actual rest interface but calls the remote service as if it were a local method.

One area to note is that for spring to automatically scan the @feignclient interface, you must add @enablefeignclients (basepackages = "package name") Annotations to the configuration class in spring boot. Failure to do so will cause the injection to fail.

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.