Feign is an open source project that makes rest service calls more concise, and many introductory articles or books also call it declarative rest. In a traditional Web service, the proxy class (or stub code) of some clients is generated by referencing WSDL, and feign is somewhat similar to this, but more flexible.
Let's look back at the Service-consumer call code for the service in the previous section:
1@GetMapping ("/order/{userid}/{orderno}")2 Publicstring FindOrder (@PathVariable Integer userId, @PathVariable string orderno) {3Userdto user = resttemplate.getforentity ("http://SERVICE-PROVIDER-DEMO/user/" + userId, userdto.class). GetBody ();4 if(User! =NULL) {5 returnUser.getusername () + "order" + OrderNo + "Found! ";6 }7 8 return"The user does not exist!" ";9}View Code
If the parameters are called more, the calling code will be filled with code such as a lot of assembly parameters, not too elegant. There is also a problem, usually for the sake of security, some services (or some methods in the service) may require authentication, before they can be called, if each call to the place to invoke the login and other services to handle, such business-independent code will be a lot of intrusion into the business logic, poor maintenance.
Here's a look at how to improve with feign:
One, add dependent references
Compile ' org.springframework.cloud:spring-cloud-starter-feign '
Second, define the Feignclient interface
Package Com.cnblogs.yjmyzz.spring.cloud.study.service.client;import Com.cnblogs.yjmyzz.spring.cloud.study.dto.userdto;import org.springframework.cloud.netflix.feign.FeignClient; Import Org.springframework.web.bind.annotation.pathvariable;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestMethod; @FeignClient (name = "Service-provider-demo", configuration = Basicauthconfiguration.class) public interface Userfeignclient { @RequestMapping (value = "/user/{id}", method = Requestmethod.get) userdto finduser (@PathVariable ("id") of the Integer userId);
It's a basicauthconfiguration class, and it's written by myself.
Package Com.cnblogs.yjmyzz.spring.cloud.study.service.client;import Feign.auth.BasicAuthRequestInterceptor; Import Org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;@ Configurationpublic class Basicauthconfiguration { @Bean public Basicauthrequestinterceptor Basicauthorizationinterceptor () { return new Basicauthrequestinterceptor ("App01", "passwd01");} }
The APP01, PASSWD01, is the username and password assigned by the server.
Attached: The application.yml of the service provider can be found in the following settings
Security: Basic: enabled:true User: name:app01 password:passwd01
Third,the use of feignclient
Package Com.cnblogs.yjmyzz.spring.cloud.study.service.controller;import Com.cnblogs.yjmyzz.spring.cloud.study.dto.userdto;import Com.cnblogs.yjmyzz.spring.cloud.study.service.client.userfeignclient;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.web.bind.annotation.getmapping;import org.springframework.web.bind.annotation.PathVariable; Import Org.springframework.web.bind.annotation.RestController; @RestControllerpublic class Ordercontroller { @ autowired private userfeignclient userfeignclient; @GetMapping ("/order/{userid}/{orderno}") Public String FindOrder (@PathVariable Integer userId, @PathVariable String orderno) { userdto user = Userfeignclient.finduser (userId); if (user! = null) { return user.getusername () + "order" + OrderNo + "Found! "; } Return "user does not exist! "; }}
Finally, an annotation is added to the main entry class.
Package Com.cnblogs.yjmyzz.spring.cloud.study.service;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.cloud.client.discovery.enablediscoveryclient;import org.springframework.cloud.netflix.feign.enablefeignclients;@ Enablediscoveryclient@springbootapplication@enablefeignclientspublic class Serviceconsumer {public static void Main (string[] args) { springapplication.run (serviceconsumer.class, args);} }
The main function is to @enablefeignclients this annotation.
Spring Cloud Learning (3)-Getting started with feign