Spring Cloud Learning--feign

Source: Internet
Author: User

Feign Introduction:

Feign is a declarative Web service client that uses feign to make it easier to write Web service clients. It has pluggable annotation support, including feign annotations and jax-rs annotations, feign also supports pluggable encoders and decoders, Spring Cloud adds support for spring MVC annotations, and Httpmessageconverters uses the same way that is used by default in the Spring Web. Spring Cloud integrates the Ribbon and Eureka to provide load-balanced HTTP clients when using feign. The Fegin encapsulates the Robbin, and if you need to configure your own load algorithm, you can customize the Ribbon algorithm to

1.pom File Modification
<dependencies><dependency><groupid>org.springframework.cloud</groupid><artifactid >spring-cloud-starter-feign</artifactId></dependency><dependency><groupId> org.springframework.cloud</groupid><artifactid>spring-cloud-starter-eureka</artifactid></ Dependency><dependency><groupid>org.springframework.cloud</groupid><artifactid> Spring-cloud-starter-ribbon</artifactid></dependency><dependency><groupid> org.springframework.cloud</groupid><artifactid>spring-cloud-starter-config</artifactid></ Dependency><dependency><groupid>org.springframework.boot</groupid><artifactid> Spring-boot-starter-web</artifactid></dependency><dependency><groupid> org.springframework</groupid><artifactid>springloaded</artifactid></dependency>< Dependency><groupid>org.springframework.boot</groUpid><artifactid>spring-boot-devtools</artifactid></dependency></dependencies> 
2.application.yml
Server:  Port:8002eureka:   client:     register-with-eureka:false    service-url:       defaultzone:http ://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/,http://eureka7001.com:7001/eureka/
3. Writing ClientService
@FeignClient (value = "cloud-provider-dept") public interface Deptclientservice {@RequestMapping (value = "/dept/add", Method=requestmethod.post) Public boolean add (@RequestBody Dept D); @RequestMapping (value = "/dept/get/{id}", method= requestmethod.get) public Dept GET (@PathVariable ("id") Long ID); @RequestMapping (value = "/dept/list", method= requestmethod.get) public list<dept> List ();}
4. Writing a Controller
@RestControllerpublic class Deptcontroller_consumer {@Autowiredprivate deptclientservice service; @RequestMapping ( Value = "/consumer/dept/add") public boolean Add (@RequestBody dept d) {return service.add (d);} @RequestMapping (value = "/consumer/dept/get/{id}") Public dept Get (@PathVariable ("id") Long ID) {return service.get (ID) ;} @RequestMapping (value = "/consumer/dept/list") public list<dept> list () {return service.list ();}}
5. Modify the main startup class
@SpringBootApplication @enableeurekaclient@enablefeignclients (basepackages = {"Com.wuzz.springcloud"}) @ Componentscan ("Com.wuzz.springcloud") public class Deptconsumer_feign_app {public    static void Main (string[] args)    {    springapplication.run (deptconsumer_feign_app.class, args);}    }
Ribbon and Feign differences

The ribbon adds Maven dependent Spring-starter-ribbon using @ribbonclient (value= "service name") to invoke the remote service corresponding method using Resttemplate
Feign add maven dependent Spring-starter-feign service provider provides external interface to callers using @FeignClient on interface ("Specify Service name")
The difference between ribbon and feign:
Both the ribbon and the feign are used to invoke other services, but in different ways.
1. The annotations used by the start-up class are different, and the Ribbon is @ribbonclient,feign with @enablefeignclients.
2. Depending on the location of the service, the Ribbon is declared on the @ribbonclient annotation and feign is used in the interface that defines the abstract method @feignclient declaration.
3. In different ways, the ribbon needs to build its own HTTP request, simulate an HTTP request and then send it to other services using resttemplate, which is cumbersome.
Feign has been improved on the basis of the ribbon, using interfaces to define methods of other services that need to be invoked as abstract methods.
You do not need to build your own HTTP requests. Note, however, that the annotations of the abstract method, the method signature, and the method of providing the service are exactly the same.

Spring Cloud Learning--feign

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.