1. Concept: Feign Interface Service
2. Specific content
There is actually a very awkward situation with all the Rest service invocations that have been made so far, such as the following code:
Dept Dept = This.resttemplate . Exchange (Dept_get_url + ID, httpmethod.get, new Httpentity<object> ( this.headers), Dept.class) . GetBody ();
All the data calls and transformations must be done by the user themselves, and we are not good at these, we are accustomed to the programming pattern is: through the interface to achieve business operations, rather than through the specific Rest data.
2.1, feign basic use
For convenience, now copy the "microcloud-consumer-80" module for the "Microcloud-consumer-feign" module.
1, "microcloud-consumer-feign" in order to be able to use the feign support, need to modify the Pom.xml configuration file, the introduction of dependent packages:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId> Spring-cloud-starter-feign</artifactid> </dependency>
Feign includes the Ribbon support, so importing the above dependency package indicates that the Ribbon related support library already exists in the project.
2, "Microcloud-service" set up a new module, this module is dedicated to the definition of client interface;
3, "Microcloud-service" Modify the Pom.xml configuration file, referring to the "Microcloud-api" module, so that it can be used to VO class;
<dependency> <groupId>cn.study</groupId> <artifactid>microcloud-api</ Artifactid> </dependency>
4, "Microcloud-service" if you want to make remote Rest calls through feign, you must consider the authentication of the service.
· You can remove the configuration processing from the original restconfig, and then add the Feign authentication configuration class
Package Cn.study.commons.config;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import feign.auth.basicauthrequestinterceptor;@ Configurationpublic class Feignclientconfig { @Bean public Basicauthrequestinterceptor Getbasicauthrequestinterceptor () { return new Basicauthrequestinterceptor ("Studyjava", "Hello");} }
5, "Microcloud-service" to establish a ideptclientservice interface;
Package Cn.study.service;import Java.util.list;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;import Cn.study.commons.config.feignclientconfig;import cn.study.vo.dept;/** * Add the remote Micro service name value= "Microcloud-provider-dept" and * Service authentication configuration= via annotation @feignclient to the interface Feignclientconfig.class * */@FeignClient (value= "microcloud-provider-dept", Configuration=feignclientconfig.class) Public interface Ideptclientservice { @RequestMapping (method=requestmethod.get,value= "/dept/get/{id}") Public Dept Get (@PathVariable ("id") long ID); @RequestMapping (method=requestmethod.get,value= "/dept/list") Public list<dept> list (); @RequestMapping (method=requestmethod.post,value= "/dept/add") Public Boolean Add (Dept dept);
6, "Microcloud-consumer-feign" Modify the Pom.xml configuration file, introduce the Microcloud-service development package:
<dependency> <groupId>cn.study</groupId> <artifactid>microcloud-service</ Artifactid> </dependency>
7, "Microcloud-consumer-feign" Modify the Consumerdeptcontroller controller program class;
Package Cn.study.microcloud.controller;import Javax.annotation.resource;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.restcontroller;import Cn.study.service.ideptclientservice;import cn.study.vo.Dept; @RestControllerpublic class Consumerdeptcontroller { @Resource private Ideptclientservice Deptservice; @RequestMapping (value = "/consumer/dept/get") public Object getdept (long id) { return This.deptService.get (ID ); } @RequestMapping (value = "/consumer/dept/list") public Object listdept () { return this.deptService.list (); } @RequestMapping (value = "/consumer/dept/add") public Object adddept (Dept Dept) throws Exception { Return This.deptService.add (dept);} }
8, "Microcloud-consumer-feign" Modify the program to start the main class, append operation processing.
Package Cn.study.microcloud;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.cloud.netflix.eureka.enableeurekaclient;import org.springframework.cloud.netflix.feign.enablefeignclients;@ Springbootapplication@enableeurekaclient@enablefeignclients (basepackages={"Cn.study.service"})// The ideptclientservice of the interface is generated so that it can be injected into the Consumerdeptcontroller public class Consumer_80_startspringcloudapplication { Public static void Main (string[] args) { Springapplication.run (consumer_80_ Startspringcloudapplication.class, args);} }
9. Start Test: http://client.com/consumer/dept/get?id=1
· You can see that the feign is self-balancing with load-balanced configuration items when processing
2.2, feign related configuration
1, "microcloud-consumer-feign" feign the most important role is to transform the Rest service information into an interface, but in the actual use also need to consider some configuration conditions, such as: Data compression, Rest of the core essence is: JSON Data transfer (XML, text), so you have to think of a situation, if the user sends a large data, this time can consider modifying the application.yml configuration file to compress the transmitted data;
Feign: compression: Request: mime-types: # Types that can be compressed -text/xml -Application/xml - Application/json min-request-size:2048 # More than 2048 bytes for compression
2, if necessary, you can open feign related log information in the project (default does not open):
· "Microcloud-consumer-feign" modifies the application.yml configuration file to append log tracking:
Logging: Level : Cn.study.service:DEBUG
· "Microcloud-service" modifies the feignclientconfig to turn on the output of the log:
Package Cn.study.commons.config;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import feign. Logger;import feign.auth.BasicAuthRequestInterceptor; @Configurationpublic class Feignclientconfig { @Bean Public Logger.level Getfeignloggerlevel () { return feign. Logger.Level.FULL; } @Bean public basicauthrequestinterceptor getbasicauthrequestinterceptor () { return new Basicauthrequestinterceptor ("Studyjava", "Hello");} }
Running the program again now allows you to observe the following process:
· When using feign to access the Rest service through an interface, the request is made based on the type of service set, and the request is sent to Eureka (address: "Http://MICROCLOUD-PROVIDER-DEPT/dept/list");
· Subsequently, due to the configuration of authorization processing, so continue to send authorization information ("Authorization");
· When making the service call, feign fused the Ribbon technology, so it also supports load-balanced processing;
Summary : feign = resttempate + httpheader + Ribbon + Eureka complex = automatic instantiation of the Business interface
Springcloud series six: Feign Interface transformation invoke service (feign basic use, feign related configuration)