springcloud学习(五)之hystrix容错机制和spring cloud 配置中心-爱代码爱编程
一.Hystrix容错机制:在不改变各个微服务调用关系的前提下,针对错误情况进行预先的处理
设计原则
服务隔离机制
服务降级机制
熔断机制
提供实时的监控和报警功能
提供实时的配置修改功能
二.Hystrix数据监控需要结合Spring Cloud Actuator提供了对服务的健康监控,数据统计,可以通过hystrix-stream节点获取监控的请求数据,提供了可视化监控界面
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ygdSpringCloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Hystrix</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
</project>
2.application.yml
server:
port: 8060
spring:
application:
name: hystrix
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true
# s数据监控节点 localhost:8060/actuator/hystrix.stream
management:
endpoints:
web:
exposure:
include: 'hystrix.stream'
3.启动类
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker //声明启用数据监控
@EnableHystrixDashboard //声明启用可视化数据监控
public class HystrixApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixApplication.class,args);
}
}
4.查看监控数据http://localhost:8060/actuator/hystrix.stream

5.可视化平台http://localhost:8060/hystrix 填入监控的节点:http://localhost:8060/actuator/hystrix.stream

三.Spring Cloud 配置中心
本地配置

server端配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
server:
port: 8762
spring:
application:
name: nativeconfigserver
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:/shared
shared配置文件 configclient-dev.yml
server:
port: 8070
foo: foo version 1
client端配置文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
配置bootstrap.yml文件
spring:
application:
name: configclient
profiles:
active: dev
#本地ConfigDServer的访问路径
cloud:
config:
uri: http://localhost:8762
fail-fast: true
2.远程拉取配置
我们拉取config配置文件

server端配置:
server:
port: 8888
spring:
application:
name: configserver
cloud:
config:
server:
git:
#githhub地址
uri:
search-paths: config
#远程仓库的用户名密码
username:
password:
default-label: master
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/