**Feign:**
与Ribbon一样,Feign也是由Netflix提供的,Feign是一个声明式、
模块化的Web Service客户端,它简化了开发者编写Web客户端的操作,
开发者可以通过简单的接口和注解来调用HTTP API ,Spring Cloud Feign,
它整合了Ribbon和Hystrix,具有可插拔、基于注解、负载均衡、服务熔断
等一系列便捷功能。
相比较于Ribbon+RestTemplate的方式,Feign大大简化了代码的开发,
Feign支持多种注解,包括Feign注解、JAX-RS注解、Spring MVC注解等,
Spring Cloud 对Feign进行了优化,整合了Ribbon和Eureka,从而让Feign
的使用更加方便。
Ribbon和Feign的区别:
Ribbon是一个通用的HTTP客户端工具,Feign是基于Ribbon实现的。
Feign的特点:
1)Feign是一个声明式的Web Service客户端。
2)支持Feign注解、SpringMVC注解、JAX-RS注解。
3)Feign基于Ribbon实现,使用起来更加简单。
4)Feign集成了Hystrix,具备服务熔断的功能。
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200713174109858.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)
创建Module,pom.xml添加依赖
```yaml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
```
创建配置文件,application.yml
```yaml
server:
port: 8050
spring:
application:
name: feign
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
```
创建启动类
```java
package com.shuang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class,args);
}
}
```
创建声明式接口
```java
package com.shuang.feign;
import com.shuang.entity.Student;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Collection;
@FeignClient(value = "provider")
public interface FeignProviderClient {
@GetMapping("/student/findAll")
public Collection<Student> findAll();
@GetMapping("/student/index")
public String index();
}
```
Handler
```java
package com.shuang.controller;
import com.shuang.entity.Student;
import com.shuang.feign.FeignProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
@RestController
@RequestMapping("/feign")
public class FeignHandler {
@Autowired
private FeignProviderClient feignProviderClient;
@GetMapping("/findAll")
public Collection<Student> findAll(){
return feignProviderClient.findAll();
}
@GetMapping("/index")
public String index(){
return feignProviderClient.index();
}
}
```
服务熔断,application.yml中添加熔断机制
```java
server:
port: 8050
spring:
application:
name: feign
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true
```
feign.hystrix.enable:是否开启熔断器。
创建 FeignProviderClient 接口的实现类FeignError,定义容错处理逻辑,通过@Component
注解将FeignError实例注入IOC容器中。
```java
package com.shuang.feign.impl;
import com.shuang.entity.Student;
import com.shuang.feign.FeignProviderClient;
import org.springframework.stereotype.Component;
import java.util.Collection;
@Component
public class FeignError implements FeignProviderClient {
@Override
public Collection<Student> findAll() {
return null;
}
@Override
public String index() {
return "服务器维护中。。。。";
}
}
```
在FeignProviderClient定义处通过@FeignClient的fallback属性设置映射。
```java
package com.shuang.feign;
import com.shuang.entity.Student;
import com.shuang.feign.impl.FeignError;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Collection;
@FeignClient(value = "provider", fallback = FeignError.class)
public interface FeignProviderClient {
@GetMapping("/student/findAll")
public Collection<Student> findAll();
@GetMapping("/student/index")
public String index();
}
```
**依次启动:注册中心、Feign**
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200713174030134.png)