什么是Feign
Feign是一个声明式WebService客户端。使用Feign能让编写WebService客户端更简单,它是使用是定义一个接口,然后在
上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器SpringCloud对Feign进行了封
装,使其支持了SpringMVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
Feign能干什么
Feign旨在使编写Java Http客户端变得更加容易。前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封
装处理,形成了一套模板化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处
调用,所有多会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础了做了进一步封
装,由它来帮我们定义和实现依赖服务接口的定义,在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置
它,即可完成对服务提供方的接口绑定,简化了使用Springcloud Ribbon时,自己封装服务调用客户端的开发量。
Feign集成了Ribbon,利用Ribbon维护了服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过Feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。Feign通过接口的方法调用Rest服务(之前是Ribbon + RestTemplate),该请求发送给Eureka服务器,通过Feign直接找到服务接口,由于在进行服务调用的时候融合了Ribbon技术,所以也支持负载均衡作用。
Nginx、Ribbon、Feign区别
Nginx、Ribbon、Feign都可以实现负载均衡。
Nginx是一个反向代理、负载均衡的服务器,是在消费端和服务提供者之间的一个设施。
Ribbon是一个集成在消费端的进程内的一个软负载均衡,用来为客户端提供负载均衡功能。
Feign是用来简化消费端调用微服务,以面向接口编程的方式来调用,且内置了Ribbon负载均衡。
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.6.RELEASE
com.newer.demo
microservice-consumer-emp-80-feign
0.0.1-SNAPSHOT
microservice-consumer-emp-80-feign
Demo project for Spring Boot
1.8
Hoxton.SR4
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
application.yml
server:
port: 80
#设置Eureka请求地址
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
#调整显示ip
instance:
instance-id: microservice-consumer-emp-80-feign
prefer-ip-address: true
#注册服务名称
spring:
application:
name: microservice-consumer-feign
MicroserviceConsumerEmp80FeignApplication
package com.newer.demo.microservice.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class MicroserviceConsumerEmp80FeignApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerEmp80FeignApplication.class, args);
}
}
FeignClientEmpService.java
package com.newer.demo.microservice.consumer.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
/**
* 定义Feign客户端接口
*/
//指定服务名称
@FeignClient(value ="EmpService" )
public interface FeignClientEmpService {
// 服务提供方映射地址
@GetMapping("/api/emps")
public ResponseEntity> emps();
}
EmpContrioller.java
package com.newer.demo.microservice.consumer.controller;
import com.newer.demo.microservice.consumer.service.FeignClientEmpService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/api")
public class EmpController {
// 注入
@Resource
private FeignClientEmpService feignClientEmpService;
@GetMapping("/emps")
public ResponseEntity> emps(){
return feignClientEmpService.emps();
}
}
与https://blog.csdn.net/weixin_44364444/article/details/105955997中的microservice-consumer-emp-80效果一样。
分布式系统面临的问题:
复杂分布式系统结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免的失败。
服务雪崩:
在实际项目中,微服务之间的相互调用可能会遇上网络抖动、延迟、超时等一些列问题,如果不加以处理,可能引发更加严
重的问题,如一开始的用户服务不可用导致调用此服务的售票服务受阻,导致占用了很多系统资源,因而导致调用售票服务
的支付服务也挂掉,这期间又导致了资源占用无法释放,持续滚雪球可能导致整个系统都宕掉,这即所谓的“雪崩效应”(是
一种因服务提供者的不可用导致服务调用者的不可用,并将不可用逐渐放大的过程)。
Hystrix是什么
Hystrix是一个用于处理分布式系统的延迟和容错的开源库(NetFlix)。在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix能够保证在一个依赖出问题情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
断路器本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要的占用,从而避免了故障在分布式系统的蔓延,乃至雪崩。
Hystrix的设计原则:
资源隔离
熔断器
命令模式
Hystrix能干什么
服务熔断、服务降级、服务限流、服务监控等。
服务熔断(服务提供方)
熔断机制是应对雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务不可用或响应时间太长时,会进行服务的降
级,进而熔断该节点微服务的调用,快速返回错误的响应信息。当检测到该节点微服务调用响应正常后恢复调用链路。在
SpringCloud框架里,熔断机制通过Hystrix实现,Hystrix会监控微服务间的调用状况,当失败的调用到一定的阈值,缺省是
5秒内20次调用失败就会启动熔断机制。熔断机制的注解是 @HystrixCommand。
服务降级(Consumer端)
当服务器压力剧增的情况下,根据实际业务情况及流量,对一些服务和页面有策略的不处理或换种简单的方式处理,从而释放服务器资源以保证核心交易正常运作或高效运作。
服务降级一般是从整体负荷考虑,就是当某个服务熔断之后,服务器将不再被调用,此时客户端可以自己准备一个本地的fallback回调,返回一个缺省值。这样做,虽然服务水平下降,但好歹可用,比直接挂掉要强。服务降级处理是在客户端完成的,与服务端没有关系。
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.6.RELEASE
com.newer.demo
microservice-consumer-emp-80
0.0.1-SNAPSHOT
microservice-consumer-emp-80
Demo project for Spring Boot
1.8
Hoxton.SR4
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
application.yml
server:
port: 80
#设置Eureka请求地址
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
#调整显示ip
instance:
instance-id: microservice-consumer-emp-80
prefer-ip-address: true
#注册服务名称
spring:
application:
name: microservice-consumer
EmpController3.java
package com.newer.demo.microservice.consumer.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* 使用Hystrix 进行容错处理
*/
@RestController
@RequestMapping("/api")
public class EmpController3 {
//服务路径
private final String url = "http://EmpService/api/emps";
@Autowired
private RestTemplate restTemplate3;
@Bean("restTemplate3")
@LoadBalanced //不能少该注解
public RestTemplate create(){
return new RestTemplate();
}
@GetMapping({"/emps3", "/list3"}) //需要引入容错机制
@HystrixCommand(fallbackMethod = "errorFallBack") //指定容错方法
public ResponseEntity> emps() {
ResponseEntity> resp = restTemplate3.getForEntity(url, List.class);
return resp;
}
//定义容错方法
public ResponseEntity> errorFallBack() {
ResponseEntity> resp = new ResponseEntity("服务出错了", HttpStatus.INTERNAL_SERVER_ERROR);
return resp;
}
}
MicroserviceConsumerEmp80Application
package com.newer.demo.microservice.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@SpringBootApplication
//@EnableDiscoveryClient比@EnableEurekaClient范围广
@EnableDiscoveryClient
//或者@EnableEurekaClient
@EnableCircuitBreaker
//或者@EnableHystrix
public class MicroserviceConsumerEmp80Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerEmp80Application.class, args);
}
}
启动程序,浏览器打开。可以看到Hystri熔断 起作用了。
但是等服务器稳定下来,这台服务器又可以使用了
关于 Spring Cloud之Feign&Hystri熔断 就到这,后续会细讲,有问题的小伙伴,欢迎留言!!!