Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。Feign整合了Ribbon和Hystrix,可以让我们不再需要显式地使用这两个组件。Spring Cloud为Feign添加了Spring MVC的注解支持,我们只需要创建一个接口并用注解方式配置它,即可完成服务提供方的接口绑定。
创建Feign模块
创建feign-client-8006模块,pom文件添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.top</groupId>
<artifactId>spring-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>feign-client-8006</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign-client-8006</name>
<description>Feign客户端8006</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
添加配置,将服务注册到注册中心
server:
port: 8006
spring:
application:
name: feign-client
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/
启动类加上注解@EnableFeignClients 标明是feign客户端 @EnableEurekaClient 标明是eureka客户端
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class FeignDemoApplication {
public static void main(String[] args) {
SpringApplication.run(FeignDemoApplication.class, args);
}
}
创建一个FeignService 接口,value代表示映射到哪个服务,这里是映射到eureka-client服务
@FeignClient(value = "eureka-client")
public interface FeignService {
//映射到sayHello方法
@RequestMapping("/sayHello")
String sayHello();
}
创建controller用来访问
@RestController
@RequestMapping("/feign")
public class FeignController {
@Autowired
private FeignService feignService;
@RequestMapping("/sayHello")
public void sayHello(){
System.out.println(feignService.sayHello());
}
}
接着启动服务,可以看到调用feign模块的sayHello方法,最终会通过feign客户端映射到eureka-client模块上的sayHello方法,最终返回结果。同样我们可以看到feign和ribbon一样,实现了负载均衡
Feign客户端访问带参数的方法
我们先来修改一下两个eureka客户端的代码,添加一个带参数的方法:
@RequestMapping("/sayName")
public String sayName(String name){
return "EurekaClient8003客户端:"+name;
}
-----------------------------------------------------------
@RequestMapping("/sayName")
public String sayName(String name){
return "EurekaClient8004客户端:"+name;
}
接着在FeignService中进行映射,使用注解@RequestParam映射(不加这个是映射不到的)
@RequestMapping("/sayName")
//使用注解@RequestParam映射(不加这个是映射不到的)
String sayName(@RequestParam("name") String name);
controller中进行调用
@RequestMapping("/sayName")
public void sayName(String name){
System.out.println(feignService.sayName(name));
}
最后访问接口,http://localhost:8006/feign/sayName?name=张三可以 看到方法成功映射
Feign中进行服务降级
加上这个配置,表示启用hystrix,否则不能进行服务降级
feign:
hystrix:
enabled: true
添加fallback属性指定降级逻辑处理类
@FeignClient(value = "eureka-client", fallback = FeignServiceImpl.class)
public interface FeignService {
//映射到sayHello方法
@RequestMapping("/sayHello")
String sayHello();
@RequestMapping("/sayName")
//使用注解@RequestParam映射(不加这个是映射不到的)
String sayName(@RequestParam("name") String name);
}
该降级类实现FeignService 接口,重写该接口中的服务调用方法实现降级逻辑
@Component
public class FeignServiceImpl implements FeignService {
@Override
public String sayHello() {
return "调用sayHello方法出错";
}
@Override
public String sayName(String name) {
return "调用sayName方法出错";
}
}
接着只启动两个注册中心和feign模块,访问该接口,会发现调用了降级方法
相关阅读
项目代码
SpringCloud 汇总【Greenwich 版】
SpringCloud(一):Eureka注册中心【Greenwich 版】
SpringCloud(二):Ribbon负载均衡【Greenwich 版】
SpringCloud(三):Feign声明式服务调用【Greenwich 版】
SpringCloud(四):Hystrix熔断器介绍【Greenwich 版】
SpringCloud(五):Hystrix的请求熔断与服务降级【Greenwich 版】
SpringCloud(六):Hystrix的请求合并【Greenwich 版】
SpringCloud(七):Hystrix仪表盘与Turbine集群监控【Greenwich 版】
SpringCloud(八):Zuul网关【Greenwich 版】
SpringCloud(九):Config配置中心【Greenwich 版】
SpringCloud(十):Bus消息总线【Greenwich 版】
SpringCloud(十一):Stream消息驱动 + RabbitMQ【Greenwich 版】
SpringCloud(十二):Sleuth链路跟踪【Greenwich 版】