解耦原因

下面代码使用@HystrixCommand(fallbackMethod = “getInfoFallback”)业务代码耦合度太高,不利于维护,所以需要整合Feign和Hystrix进行解耦

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* 获取信息
*
* @return
* @throws InterruptedException
*/
@RequestMapping("/getInfo")
@HystrixCommand(fallbackMethod = "getInfoFallback")
public Map<String, Object> getInfo() throws InterruptedException {
Thread.sleep(100);
Map<String, Object> map = new HashMap<>(16);
map.put("code", 200);
map.put("info", "业务数据1004");
return map;
}

/**
* 请求超时就请求我
*
* @return
*/
public Map<String, Object> getInfoFallback() {
Map<String, Object> map = new HashMap<>(16);
map.put("code", 500);
map.put("info", "系统出错,稍后重试");
return map;
}

实现过程

修改Hystrix服务提供者项目

项目名为:microservice-student-provider-hystrix-1004和microservice-student-provider-hystrix-1005

在StudentService.java中添加新的接口:

1
2
3
4
5
/**
* 获取信息
* @return
*/
Map<String,Object> getInfo();

在StudentServiceImpl.java中实现上面的接口

1
2
3
4
5
6
7
@Override
public Map<String, Object> getInfo() {
Map<String, Object> map = new HashMap<>(16);
map.put("code", 200);
map.put("info", "业务数据1004");
return map;
}

在StudentProviderController.java中调用

1
2
3
4
5
6
7
8
9
10
11
/**
* 获取信息
*
* @return
* @throws InterruptedException
*/
@RequestMapping("/getInfo")
public Map<String, Object> getInfo() throws InterruptedException {
Thread.sleep(500);
return studentService.getInfo();
}

修改公共项目

项目名为:microservice-common

在StudentClientService.java中添加getInfo接口方法,并修改@FeignClient注解为:@FeignClient(value = “MICROSERVICE-STUDENT”, fallbackFactory = StudentClientFallbackFactory.class)

1
2
3
4
5
6
7
/**
* 获取信息
*
* @return
*/
@RequestMapping("/student/getInfo")
Map<String, Object> getInfo();

新建StudentClientFallbackFactory.java,实现FallbackFactory<StudentClientService>接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.ledao.service;

import com.ledao.entity.Student;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author LeDao
* @company
* @create 2021-08-16 2:18
*/
@Component
public class StudentClientFallbackFactory implements FallbackFactory<StudentClientService> {

@Override
public StudentClientService create(Throwable throwable) {
return new StudentClientService() {
@Override
public Student get(Integer id) {
return null;
}

@Override
public List<Student> list() {
return null;
}

@Override
public boolean save(Student student) {
return false;
}

@Override
public boolean delete(Integer id) {
return false;
}

@Override
public Map<String, Object> getInfo() {
Map<String, Object> map = new HashMap<>(16);
map.put("code", 500);
map.put("info", "系统出错,稍后重试");
return map;
}
};
}
}

修改Feign消费者项目

项目名为:microservice-student-consumer-feign-80

在StudentConsumerFeignController.java中添加调用方法

1
2
3
4
5
6
7
8
9
/**
* 获取信息
*
* @return
*/
@RequestMapping("/getInfo")
public Map<String, Object> getInfo() {
return studentClientService.getInfo();
}

application.yml加上hystrix支持

1
2
3
feign:
hystrix:
enabled: true

配置超时时间

因为整合了Feign和Hystrix,原来服务提供者的Hystrix超时时间配置没用了,如何配置查看:Feign和Hystrix整合后配置超时时间

测试

启动以下项目:

microservice-eureka-server-2001

microservice-eureka-server-2002

microservice-eureka-server-2003

microservice-student-provider-hystrix-1004

microservice-student-provider-hystrix-1005

microservice-student-consumer-feign-80

如果超时的情况返回错误信息(500 系统出错,稍后重试),说明配置成功