在不改变各个微服务调用关系的前提下,针对错误情况进行预先处理。
1、服务隔离机制
2、服务降级机制
3、熔断机制
4、提供实时的监控和报警功能
5、提供实时的配置修改功能
Hystrix 数据监控需要结合 Spring Boot Actuator 来使用,Actuator 提供了对服务的健康健康、数据统计,可以通过 hystrix.stream 节点获取监控的请求数据,提供了可视化的监控界面。
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.0.2.RELEASE
org.springframework.cloud
spring-cloud-starter-openfeign
2.0.2.RELEASE
org.springframework.boot
spring-boot-starter-actuator
2.0.7.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
2.0.2.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
2.0.2.RELEASE
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
management:
endpoints:
web:
exposure:
# 通过该节点获取监控
include: 'hystrix.stream'
package com.northwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class HystrixApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixApplication.class,args);
}
}
注解说明:
@EnableCircuitBreaker
:声明启用数据监控
@EnableHystrixDashboard
:声明启用可视化数据监控
package com.northwind.controller;
import com.northwind.entity.Student;
import com.northwind.feign.FeignProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
@RestController
@RequestMapping("/hystrix")
public class HystrixController {
@Autowired //需要接口
private FeignProviderClient feignProviderClient;
@GetMapping("/findAll")
public Collection findAll(){
return feignProviderClient.findAll();
}
@GetMapping("/index")
public String index(){
return feignProviderClient.index();
}
}
http://localhost:8060/actuator/hystrix.stream
可以监控到请求数据,http://localhost:8060/hystrix
,可以看到可视化的监控界面,输入要监控的地址节点即可看到该节点的可视化数据监控。========================================================================
Spring Cloud Config,通过服务端可以为多个客户端提供配置服务。Spring Cloud Config 可以将配置文件存储在本地,也可以将配置文件存储在远程 Git 仓库,创建 Config Server,通过它管理所有的配置文件。
本地文件系统
org.springframework.cloud
spring-cloud-config-server
2.0.2.RELEASE
server:
port: 8762
spring:
application:
name: nativeconfigserver
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:/shared
注解说明
profiles.active
:配置文件的获取方式(本地)
cloud.config.server.native.search-locations
:本地配置文件存放的路径
server:
port: 8070
foo: foo version 1
package com.northwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class NativeConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(NativeConfigServerApplication.class,args);
}
}
注解说明
@EnableConfigServer
:声明配置中心。
org.springframework.cloud
spring-cloud-starter-config
2.0.2.RELEASE
spring:
application:
name: configclient
profiles:
active: dev
cloud:
config:
uri: http://localhost:8762
fail-fast: true
注解说明
cloud.config.uri
:本地 Config Server 的访问路径
cloud.config.fail-fase
:设置客户端优先判断 Config Server 获取是否正常。
通过spring.application.name
结合spring.profiles.active
拼接目标配置文件名,configclient-dev.yml,去 Config Server 中查找该文件。
package com.northwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NativeConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(NativeConfigClientApplication.class,args);
}
}
package com.northwind.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/native")
public class NativeConfigController {
@Value("${server.port}")
private String port;
@Value("${foo}")
private String foo;
@GetMapping("/index")
public String index(){
return this.port+"-"+this.foo;
}
}
先打开注册中心,再启动nativeconfigserver,再启动nativeconfigclient
configclient-dev.yml中的8070端口,作为nativeconfigclient的端口。
configserver用于管理配置文件。
=============================================================================================
server:
port: 8070
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: configclient
org.springframework.cloud
spring-cloud-config-server
2.0.2.RELEASE
server:
port: 8888
spring:
application:
name: configserver
cloud:
config:
server:
git:
uri: https://github.com:shi-gan/aispringcloud.git
searchPaths: config
username: root
password: root
label: master
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
package com.northwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
远程配置完成。
========================================================================================
org.springframework.cloud
spring-cloud-starter-config
2.0.2.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.0.2.RELEASE
spring:
cloud:
config:
name: configclient
label: master
discovery:
enabled: true
service-id: configserver
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
注解说明
spring.cloud.config.name
:当前服务注册在 Eureka Server 上的名称,与远程仓库的配置文件名对应。
spring.cloud.config.label
:Git Repository 的分支。
spring.cloud.config.discovery.enabled
:是否开启 Config 服务发现支持。
spring.cloud.config.discovery.service-id
:配置中心在 Eureka Server 上注册的名称。
package com.northwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class,args);
}
}
package com.northwind.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@Value("${server.port}")
private String port;
@GetMapping("/index")
public String index(){
return this.port;
}
}
依次启动注册中心,启动ConfigServer,和ConfigClient。
Spring Cloud Zipkin
Zipkin 是一个可以采集并且跟踪分布式系统中请求数据的组件,让开发者可以更加直观的监控到请求在各个微服务所耗费的时间等,Zipkin:Zipkin Server、Zipkin Client。
创建 Zipkin Server
org.springframework.boot
spring-boot-starter-web
io.zipkin.java
zipkin-server
2.9.4
io.zipkin.java
zipkin-autoconfigure-ui
2.9.4
server:
port: 9090
package com.northwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.internal.EnableZipkinServer;
@SpringBootApplication
@EnableZipkinServer
public class ZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinApplication.class,args);
}
}
注解说明
@EnableZipkinServer
:声明启动 Zipkin Server
org.springframework.cloud
spring-cloud-starter-zipkin
2.0.2.RELEASE
server:
port: 8090
spring:
application:
name: zipkinclient
sleuth:
web:
client:
enabled: true
sampler:
probability: 1.0
zipkin:
base-url: http://localhost:9090/
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
属性说明
spring.sleuth.web.client.enabled
:设置开启请求跟踪
spring.sleuth.sampler.probability
:设置采样比例,默认是 1.0
srping.zipkin.base-url
:Zipkin Server 地址
package com.northwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ZipkinClientApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinClientApplication.class,args);
}
}
package com.northwind.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/zipkin")
public class ZipkinHandler {
@Value("${server.port}")
private String port;
@GetMapping("/index")
public String index(){
return this.port;
}
}
像上面一样,有一次请求,可以被监控到(localhost:9090/zipkin/刷新后显示)。
会显示请求时间等数据。