What is Feign
The official explanation: read the official document, a better understanding of
http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign
Declarative REST client: Feign
Feign Web service client is a declarative. It makes writing Web service clients easier. To use Feign, please create an interface and annotate. It has annotation support insertable, comprising Feign notes and annotations JAX-RS. Feign also pluggable encoder and decoder. Spring Cloud adds support for Spring MVC annotations, and use the comments HttpMessageConvertersSpring Web in use by default. Spring Cloud integrates Ribbon and Eureka, provide load balancing http client while using Feign.
Declarative REST Client: Feign
Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. 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 for using the same HttpMessageConverters
used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign.
Example spring boot app
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Feign Web service client is a declarative. It makes writing Web service clients easier.
Just create an interface, then you can add annotations on top.
Reference official website: https://github.com/OpenFeign/feign
Feign is how come
Before you load balancing with ribbon, very powerful, and even their own custom algorithms
a. Most of us can introduce direct call service to our micro access
// public static final String REST_URL_PREFIX = "http://localhost:8001";
change into
public static final String REST_URL_PREFIX = "http://SPRINGCLOUD-MODEL-DEPT";
b. We are currently used to interface-oriented programming, such as our Dao Interface / WebService interfaces, this is already our current specification
The name is derived micro service call address
Interface + is through notes, get our call service.
Other programmers to adapt the proposed community, or uniform oriented programming interface routines. ---- Feign
Only you need to create an interface, then you can add the above comment.
@Mapper
Public interface UserDao{
}
Feign can do
Feign designed to make writing Java Http client easier.
When using the foregoing Ribbon + Rest Template, Rest Template packaging process using the HTTP request, a calling method of forming a templated. But in the actual development, due to the dependence of the call to service may be more than one, often a multiple interfaces will be called, so usually each micro-service package for some clients like to wrap themselves depend on these services call. So, Feign On this basis, made a further package by him to help us define and implement service interface definitions. Feign in the realization of, we just need to create an interface and use annotations way to configure it (previously marked Dao Interface Mapper above notes, now is a microblogging service interface to the top mark a comment Feign ) to complete the service provides an interface to bind parties to simplify the amount of development time using spring cloud Ribbon, automatic packaging service calls the client.
Feign integrated Ribbon
Use Ribbon maintenance service list information SpringServiceCloud-Dept, and by polling achieve load balancing client. The difference is that with the Ribbon, by feign only need to define the service binding interfaces and methods declarative, elegant and simple implementation of a service call
Feign use the steps
first step:
New springcloud-model-consumer-dept-feign (for interface development)
参考 spring cloud model-consumer-dept-80 (Ribbon + Rest Template)
Copy main project structure
Create / modify the master boot class DeptConsumer80_Feign_App
package com.jiangjy.springcloud;
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;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableEurekaClient //和eureka整合并使用客户端
@EnableFeignClients(basePackages = {"com.jiangjy.springcloud"}) //扫描Feign服务的包
@ComponentScan(value = "com.jiangjy.springcloud")
public class DeptConsumer80_Feign_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_Feign_App.class, args);
}
}
springcloud-model-dept-feign project pom.xml, add support for the feign
<!-- feign相关jar -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
Step two:
Modify springcloud-model-aip project
pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 子类里面显示声明才能有明确的继承表现,无意外就是父类的默认版本否则自己定义 -->
<parent>
<groupId>com.jiangjy.springcloud</groupId>
<artifactId>springcloud-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<!-- 当前Module我自己叫什么名字 -->
<artifactId>springcloud-model-api</artifactId>
<!-- 当前Module需要用到的jar包,按自己需求添加,如果父类已经包含了,可以不用写版本号 -->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- feign相关jar -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
</project>
New DeptClientService interfaces and add comments @FeignClient
Details:
package com.jiangjy.springcloud.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 com.jiangjy.springcloud.pojo.Dept;
@FeignClient(value = "SPRINGCLOUD-MODEL-DEPT") //面向服务中的注解+接口
public interface DeptClientService {
@RequestMapping(value = "/dept/getAll",method = RequestMethod.GET)
public List<Dept> getAll();
@RequestMapping(value = "/dept/getById/{id}", method = RequestMethod.GET)
public Dept getById(@PathVariable("id") Long id);
@RequestMapping(value = "/dept/add",method = RequestMethod.POST)
public boolean add(Dept dept);
}
Public parts: first mvn clean ------ "mvn install
springcloud-model-dept-feign engineering change / create controller package DeptController_Consumer.java
Adding new private DeptClientService service; Interface
package com.jiangjy.springcloud.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.jiangjy.springcloud.pojo.Dept;
import com.jiangjy.springcloud.service.DeptClientService;
@RestController
public class DeptController_Consumer {
@Autowired
private DeptClientService service;
@RequestMapping("/consumer/dept/getAll")
public List<Dept> getAll(){
return this.service.getAll();
}
@RequestMapping("/consumer/dept/getById/{id}")
public Dept getById(@PathVariable("id") Long id) {
return this.service.getById(id);
}
@RequestMapping("/consumer/dept/add")
public Object add(Dept dept) {
return this.service.add(dept);
}
}
Modify / create a master boot class (see the first step)
test:
Start three clusters eureka
Start three departments microService 8001/8002/8003
Start their own start Feign
http://localhost/consumer/dept/getAll
Feign own load-balancing configuration items
Small summary: Feign Rest call service by means of an interface (before a Ribbon + RestTemplate)
Sends the request to the server Eukeka ( HTTP: // the MODEL-the DEPT-SRPINGCLOUD / Dept / the getAll ),
Feign find the service interfaces directly due to the time during service calls combines Ribbon technology, it also supports load balancing effect.