Spring Cloud Feign 其实是整合了Spring Cloud Ribbon 和 Spring Cloud Hystrix
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>serice-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>serice-feign</name>
<description>Demo project for Spring Cloud</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类加上 @EnableFeignClients
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class SericeFeignApplication {
public static void main(String[] args) {
SpringApplication.run(SericeFeignApplication.class, args);
}
//开启指定Feign客户端的DEBUG日志(默认的Logger.Level为NONE,需要创建bean修改)
// Logger级别有4种:NONE(不记录任何信息),
// BASIC(仅记录请求方法,URL及响应状态码和时间),
// HEADERS(除了BASIC的信息外,还会记录请求与响应的头信息),
// FULL(所有的明细);
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
application.properties
management.security.enabled=false
logging.level.com.example.sericefeign.HelloService=debug
application.yml
server:
port: 8085
context-path: /service-feign
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka-server/eureka/
# spring.application.name,这个很重要,在以后的服务与服务之间相互调用一般都是根据这个name
spring:
application:
name: service-feign
#Feign是自带断路器的,但它没有默认打开,需要手动开启
feign:
hystrix:
enabled: true
HelloController.java
@RestController
public class HelloController {
@Autowired
private HelloService service;
@RequestMapping(value = "/hi/{name}", method = RequestMethod.GET)
public String sayHi(@PathVariable("name") String name) {
return service.sayHiFromClientOne(name);
}
}
定义一个feign接口,通过@ FeignClient(”服务名”)来指定调用哪个服务,这里的服务名不区分大小写.
//只要在注解中加上fallback的指定类就行了(注意:需要现在配置文件中加上 feign.hystrix.enabled=true)
@FeignClient(value = "eureka-client", fallback = HelloServiceHystric.class)
public interface HelloService {
@RequestMapping(value = "/eureka-client/hi/{name}", method = RequestMethod.GET)
String sayHiFromClientOne(@PathVariable("name") String name);
}
@FeignClient 标注的接口实现类,如果触发了服务降级,就会调用实现类中相应的方法
@Service
public class HelloServiceHystric implements HelloService {
@Override
public String sayHiFromClientOne(String name) {
return "sorry " + name;
}
}