复习回顾昨日学习内容:
SpringCloud:微服务框架
1、Eureka注册中心
2、Ribbon负载均衡
3、Feign支持REST封装
分布式系统设计定理:CAP
C:一致性
A:可用性
P:分区一致性
Eureka和ZooKeeper的区别?
EureKa:侧重AP,可用性,结构:点对点,每个点户为主从,如果有一个数据发生变化,其他节点会自动同步数据。【本地缓存,尽量尝试】
ZooKeeper:设计侧重CP,一致性。结构:主从leader/follwer,如果主节点宕机,集群无法使用,内部触发选举动作。可以使用,不保证正确性。【本地缓存,但不推荐】
注册中心业务,EureKa比ZK更加合适,信息不常发生变化,一致性保证。
Ribbon与Nginx的区别?
Nginx基于C语言,TCP/IP7层,5w每秒基于4层、7层交换。F5硬件负载均衡器50万每秒。Zuul2.0 200万每秒集群。Lua语言,shell脚本语言,Linux。Ribbon SpringCloud中的一环,本地缓存Eureka服务列表,服务直接选择一个最佳路径就转向。
Feign支持REST封装。
-api RestTemplate对象;HttpClient调用+JSon。简单。
-增加一个借口(业务);dubbo调用。能增强应用程序,开发者无需写代码
=====================================
今日学习内容:
1、Hystrix断路器
2、Zuul API网关
3、NodeJS
一、Hystrix
程序出错:
1、传统解决思路,抛异常try/catch。问题,先调用业务,等待超时时间。设置超时时间,但无法设置非常短。高并发情况下使用性能不佳。
2、JT商品价格,微服务宕机,展现a出错,b默认价格显示
断路器(熔断器),如果服务正常,调用服务。如果服务不正常,返回一个默认处理。
优点:
1、快速失效,不等业务超时时间(时间性对长)返回数值(自定义或默认值)
2、如果服务一旦正常,自动重新连接到业务,不处理默认值。
双11应对高并发情况,洪峰,经典解决方案:降级。
降级举例:
后台有很多微服务,商品服务、OA(办公自动化)服务、CRM销售资源管理、ERP人财物管理。
把OA、CRM、ERP这些不重要服务暂时停掉 。
断路器状态-三种:关闭、打开、半开。
1、不是调用失败就立即打开?设置一个阈值,可以进行配置,可以根据网络情况去设置最优值。
2、如果超过阈值,还不能正常访问,说明服务器出现异常。打开断路器,走断路器规定流程,返回默认值。
3、过一个时间窗口1s,再次调用。如果成功调用就关闭状态。【如果失败,就去调用Fallback内容,具体处理就在这个方法中来编写。如果微服务正常,不再调用Fullback】
二、功能实现【要在已有Provider和Server的基础上】
1、Pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.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>
</properties>
<dependencies>
<!-- Hystrix,Feign是基于Hystrix的 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- Eureka依赖,连接注册中心的都需要有这个依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- Feign依赖,声明式开发 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、yaml
server:
port: 9001
spring:
application:
name: consumer-feign-hystrix
eureka:
client:
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
3、Feign接口
@FeignClient(value="provider-user")//provider的yaml文件里配置的
public interface HelloFeign {
//接口方法
/*
* Feign中没有原生的@GetMapping/@PostMapping/@DeleteMapping/@PutMapping,
* 需要指定需要用method进行
*/
@RequestMapping(value="/hello/{name}")
// @RequestMapping(value="/hello/{name}",method=RequestMethod.GET)
public String hello(@PathVariable("name") String name);
@RequestMapping(value="/eat/{name}")
public String eat(@PathVariable("name") String name);
}
4、Controller
@RestController
public class HelloController {
//注入Feign接口
@Autowired
private HelloFeign helloFeign;
@RequestMapping("/hello/{name}")
@HystrixCommand(fallbackMethod = "hahaFallback")
public String hello(@PathVariable String name){
return helloFeign.hello(name);
// "This is Comsumer Feign Hystrix:"+
}
//对应上面的方法,参数必须一致,当访问失败时,hystrix直接回调用此方法
public String hahaFallback(String name){
return "Ha Ha This Is Default Value"; //失败调用时,返回默认值
}
5、入口类
@SpringCloudApplication
@EnableFeignClients//支持Feign接口
@EnableCircuitBreaker//启动Hystrix
public class RunApplicationClientHystrix {
public static void main(String[] args) {
SpringApplication.run(RunApplicationClientHystrix.class, args);
}
}
错点:熔断器未加载:
1、入口类中注解添加错误【@EnableCircuitBreaker//启动Hystrix】
2、Controller中没有实现Feign接口方法
三、运行
1、启动EurekaServer:8761
2、启动provider-user:7900
3、启动consumer-Hystrix-Client
4、停止提供者的服务 provider-user
访问Consumer-Client-Hystrix【预期结果:访问结果显示默认值】
再次启动provider-user:
疑问:hello是从哪里来的?
解答:是从提供者来的。在Hystrix的Controller和Feign添加方法eat,访问结果均显示默认值。
原因:没有在提供者provider中添加eat方法。
==========================================
思考:断路器没有生效
查找 在Eclipse中ctrl+shift+T :FeignClientsConfiguration类,找到如下方法:
后续未知