微服务架构之spring cloud hystrix&hystrix dashboard

在前面介绍spring cloud feign中咱们已经使用过hystrix,只是没有介绍,spring cloud hystrix在spring cloud中起到保护微服务的做用,不会让发生的异常无边界的蔓延下去,很像咱们电路中的保险设置,有超压或者线路有问题就即时的断开,保护用电设备不被损坏,这篇文章就来介绍spring cloud hystrix及其hystrix dashboard。spring

(一) 版本说明浏览器

a) Spring boot 2.0.6.RELEASEapp

b) Spring cloud Finchley.SR2ide

c) Java version 1.8微服务

d) spring-cloud-starter-netflix-hystrix 2.0.2.RELEASEfetch

e) spring-cloud-starter-netflix-hystrix-dashboard 2.0.2.RELEASEurl

(二) 项目配置spa

1. 项目设置code

a) POM设置server

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

</dependency>

b) application.yml配置文件

eureka:

datacenter: ctm

environment: dev

instance:

hostname: 192.168.1.78

prefer-ip-address: true

ip-address: 192.168.1.129

lease-renewal-interval-in-seconds: 10

lease-expiration-duration-in-seconds: 30

instance-id: ${eureka.instance.ip-address}:${server.port}

client:

service-url:

defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1002/eureka/,http://${eureka.instance.hostname}:1003/eureka/

fetch-registry: true

register-with-eureka: true

healthcheck:

enabled: true

feign:

hystrix:

enabled: true

c) 主要参数说明

i. eureka.instance.prefer-ip-address 使用IP显示注册信息

ii. eureka.instance.ip-address 实例IP地址,

iii. eureka.instance.instance-id 自定义实例id,服务之间调用就是使用该配置,多个实例必须保证惟一性

iv. eureka.client.service-url.defaultZone 注册中心地址

v. feign.Hystrix.enabled 在feign中其中熔断器

d) 服务提供者API

@Value("${server.port}")

private String getPort;

@GetMapping(name = "index", value = "/index")

public String Index() {

return getPort;

}

2. feign消费端项目配置

a) POM设置

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

</dependency>

i. 这里把服务消费端也注册到了服务治理中心,消费者同时也是其它服务的提供者。

b) application.yml配置文件

eureka:

datacenter: ctm

environment: dev

instance:

hostname: 192.168.1.78

prefer-ip-address: true

ip-address: 192.168.1.129

lease-renewal-interval-in-seconds: 10

lease-expiration-duration-in-seconds: 30

instance-id: ${eureka.instance.ip-address}:${server.port}

client:

service-url:

defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1002/eureka/,http://${eureka.instance.hostname}:1003/eureka/

fetch-registry: true

register-with-eureka: true

healthcheck:

enabled: true

c) feign服务消费者API

@Configuration

public class FeignConfig {

@Bean

public Retryer feignRetryer(){

return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1),3);

}

}

@Component

public class FallBackService implements FeignService {

@Override

public String Index() {

return "hi,feign,error!";

}

}

@Component

@FeignClient(name = "DEMOSERVICEIMPL",configuration = FeignConfig.class,fallback = FallBackService.class)

public interface FeignService {

@GetMapping(value = "/index")

String Index();

}

i. FeignClient feign客户端配置,这里配置了服务名称、重试规则、失败回调,fallback 属性就是熔断器提供的功能,这点在后面的熔断器看板中能够更直观的看到。

3. 项目运行

a) 运行服务提供者

i. 运行3个服务者实例后,会在服务中心看到以下效果,服务提供者已经注册成功

clip_image002

b) feign运行服务消费者

i. 运行消费者,以下图所示

clip_image004

c) 在浏览器中输入消费者地址服务地址,便可看到以下效果

clip_image006

在看板地址中输入http://192.168.1.129:1201/actuator/hystrix.stream,而后点击Monitor Stream 按钮,便可看到熔断器监控,若是没有数据显示,能够用任何客户端调用几回API,便可看到相似效果。

clip_image008

  这样spring cloud hystrix&hystrix dashboard就介绍完了,若是在开发中遇到问题,也能够留言共同探讨共同进步。

相关文章
相关标签/搜索