文章目录
Spring Cloud 学习 | - 07 - Feign方式断路器Hystrix
学习
Spring Cloud
之路,文中Spring Boot
版本为2.1.3.RELEASE
,Spring Cloud
版本为Greenwich.SR1
。因能力有限,难免会有不足或者错误之处,还望不吝指正,谢!
接上篇 Spring Cloud 学习 | - 06 - Ribbon整合断路器Hystrix,我们用Ribbon的方式整合了Hystrix,这里,我们用Feign的方式整合断路器Hystrix。
Feign默认有对Hystix的集成:
开始代码
1. 启动Eureka注册中心和服务提供者
1.1 Eureka注册中心
详细情况参阅 Spring Cloud 学习 | - 01 - Eureka服务注册与发现,启动注册中心。
1.2 服务提供者
之前的篇幅已经讲解过,这里贴一下主要代码及配置等。
1.2.1 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1.2.2 配置
server:
port: 8090
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true # 返回ip而不是host名称
# ip-address: 127.0.0.1 # 指定自己的ip信息
lease-expiration-duration-in-seconds: 15 # 服务失效时间,默认值90秒
lease-renewal-interval-in-seconds: 5 # 服务续约(renew)的间隔,默认为30秒
spring:
application:
name: user-provider
1.2.3 开启注册发现
@SpringBootApplication
@EnableDiscoveryClient
public class UserProviderApplication {
public static void main(String[] args) {
SpringApplication.run(UserProviderApplication.class, args);
}
}
1.2.4 接口API
@RestController
@RequestMapping("/user")
public class UserController {
@Value("${server.port}")
private String port;
@Autowired
private UserService userService;
private Map<String, Object> data = null;
@GetMapping("/{name}")
public Object hello(@PathVariable String name) {
String hello = userService.hello(name);
data = new HashMap<>(4);
data.put("port", port);
data.put("msg", hello);
return data;
}
}
1.2.5 启动服务提供者
启动user-provider
2. 修改Feign消费者
2.1 主要依赖
<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>
2.2 配置文件
Feign默认有对Hystix的集成,默认情况下是关闭的。我们需要通过配置来开启:
application.yml
:
server:
port: 8080
spring:
application:
name: user-consumer-feign
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true # Feign默认是集成了Hystrix的,只是默认关闭,这里开启Feign的熔断功能
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000 # 设置hystrix的超时时间为5000ms
2.3 Hystrix支持
Feign中的Fallback配置不像Ribbon中那样简单。
2.3.1 编写Fallback处理类
我们要定义一个类UserServiceFallback
,实现UserService
接口,作为Fallback的处理类,同时要加入到Spring的IoC容器中:
@Component
public class UserServiceFallback implements UserService {
/**
* hello,${name}
*
* @param name
* @return
*/
@Override
public String hello(String name) {
return "系统异常,请稍后重试-【" + name + "】";
}
}
2.3.2 指定Fallback实现类
在接口UserService
上的注解@FeignClient
里添加属性fallback = UserServiceFallback.class
,指定Fallback实现类
@FeignClient(value = "user-provider",fallback = UserServiceFallback.class)
public interface UserService {
/**
* hello,${name}
* @param name
* @return
*/
@GetMapping("/user/{name}")
String hello(@PathVariable String name);
}
2.4 启动类上开启Feign注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients // 启用Feign功能
public class UserConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(UserConsumerFeignApplication.class, args);
}
}
3. 测试
依次启动Eureka注册中心、服务提供者user-provider,编写测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserConsumerFeignApplicationTests {
@Autowired
private UserService userService;
@Test
public void contextLoads() {
for (int i = 0; i < 10; i++) {
String map = userService.hello("Cindy" + i);
System.err.println(map);
}
}
}
在user-provider启动时,运行测试类:
断开user-provider服务,运行测试类: