程序小牛

来了就随便看看,虽然没有什么好看的~~

远程调用组件

Feign是Netflix开发的一个轻量级Restflu的HTTP服务客户端(用它来发起请求,远程调用的),是以接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直接调用,Feign被广泛应用再Spring Cloud的解决方案中。

类似于Dubbo,服务消费者拿到服务提供者的接口,然后像调用本地接口方法一样去调用,实际发出的是远程的请求。

本质:封装了Http调用流程,更符合面向接口化编程习惯,类似于Dubbo的服务调用

一、Feign配置应用

在效果上:

Feign = RestTemplate + Ribbon + Hystrix

1、消费者工程应用

  1. 引入Feign依赖
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. 启动类添加注解
1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableDiscoveryClient // 开启服务发现
@EnableFeignClients // 开启Feign
public class AutodeliverFeignApplication8092 {
public static void main(String[] args) {
SpringApplication.run(AutodeliverFeignApplication8092.class,args);
}
}

注意:此时去掉Hystrix熔断支持注解,@EnableCircuitBreaker 即可包括引⼊的依赖,因为Feign会⾃动引⼊

  1. 常见feign接口
1
2
3
4
5
6
@FeignClient(name = "sc-service-resume")	// name:调⽤的服务名称,和服务提供者yml⽂件中spring.application.name保持⼀致
public interface ResumeClientFeign {

@RequestMapping(value = "/resume/openstate/{userId}",method= RequestMethod.GET)
public Integer findResumeOpenState(@PathVariable(value ="userId") Long userId);
}
  1. 使用feign接口
1
2
3
4
5
6
7
8

@Autowired
ResumeClientFeign resumeClientFeign; //声明、注入feingclient


Integer resumeOpenState = resumeClientFeign.findResumeOpenState(15451321L); //方法里直接使用
System.out.println("resumeOpenState = " + resumeOpenState);

2、Feign对负载均衡的支持

Feign本身已经集成了Ribbon依赖和自动配置,我们不需要额外引入依赖,可以通过ribbon.xx来进行全局配置,也可以通过服务名.ribbon.xx来对指定服务进行细节配置

  • Feign默认请求处理时长1s,可自定义Feign超时设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#针对的被调用方微服务名称,不加就是全局生效
sc-service-resume:
ribbon:
#请求连接超时时间
ConnectTimeout: 2000
#请求处理超时时间
##########################################Feign超时时长设置
ReadTimeout: 3000
#对所有操作都进行重试
OkToRetryOnAllOperations: true
####根据如上配置,当访问到故障请求的时候,它会再尝试访问一次当前实例(次数由MaxAutoRetries配置),
####如果不行,就换一个实例进行访问,如果还不行,再换一次实例访问(更换次数由MaxAutoRetriesNextServer配置),
####如果依然不行,返回失败信息。
MaxAutoRetries: 0 #对当前选中实例重试次数,不包括第一次调用
MaxAutoRetriesNextServer: 0 #切换实例的重试次数
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载策略调整

3、Feign对熔断器的支持

  1. 开启Feign对熔断器的支持

  2. 超时时长

    1. Feign的超时时长就是上面Ribbon的超时时长设置
    2. Hystrix超时设置(就按照之前Hystri设置方式就OK)

    超时时长,熔断的时候就是根据这两个时间的最小值来进行的,即处理时长超过了最短的那个超时时间了就熔断进行回退降级逻辑

1
2
3
4
5
6
7
8
9
10
11
12
# 开启Feign的熔断功能
feign:
hystrix:
enabled: false
hystrix:
command:
default:
execution:
isolation:
thread:
##########################################Hystrix的超时时长设置
timeoutInMilliseconds: 15000

编写回退逻辑

  1. 编写降级逻辑 类 UserInfoFallbackService 实现 feign接口,添加注解@Comment能被扫描到,实现接口方法
  2. 类添加 fallback = UserInfoFallbackService.class 关联回退处理类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@FeignClient(name = "sc-service-resume",fallback = ReusmeFailBackService.class,path = "/resume")
public interface ResumeClientFeign {

@RequestMapping(value = "/openstate/{userId}",method= RequestMethod.GET)
public Integer findResumeOpenState(@PathVariable(value ="userId") Long userId);
}

/**
* 降级回退逻辑需要定义⼀个类,实现FeignClient接⼝,实现接⼝中的⽅法
*/
@Component //需要被扫描到
class ReusmeFailBackService implements ResumeClientFeign{
@Override
public Integer findResumeOpenState(Long userId) {
return -1;
}
}