springcloud实战之Hystrix(2)

java

多应用监控

新建一个子模块 hystrix-dashboard-turbine

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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>springcloud</artifactId>
<groupId>com.qn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hystrix-dashboard-turbine</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--spring boot 1.X:spring-cloud-starter-hystrix-dashboard-->
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
</dependencies>
</project>

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server:
# 服务端口号
port: 8767
spring:
application:
# 服务名,即serviceId
name: hystrix-dashboard-turbine
eureka:
client:
serviceUrl:
# 安全认证的服务注册中心地址
defaultZone: http://localhost:9999/eureka
# 熔断器turbine
turbine:
aggregator:
cluster-config: default
cluster-name-expression: new String("default")
app-config: consumer-feign, consumer-feign-2

启动类

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableTurbine
@EnableHystrixDashboard
public class TurbineApp {
public static void main(String[] args) {
SpringApplication.run(TurbineApp.class,args);
}
}

复制一个提供者服务

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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>springcloud</artifactId>
<groupId>com.qn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>provider-user-2</artifactId>

</project>

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server:
port: 7901 #自定义boot项目访问端口
spring:
application:
name: provider-user-2
eureka:
instance:
hostname: localhost
# 发呆时间,即服务失效时间(缺省为90s),就是超过15秒没有续约就会从注册表中剔除
lease-expiration-duration-in-seconds: 15
# 心跳时间,即服务续约间隔时间(缺省为30s)
lease-renewal-interval-in-seconds: 5
client:
service-url:
defaultZone: http://localhost:9999/eureka

复制一个 Feign 客户端

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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>springcloud</artifactId>
<groupId>com.qn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>consumer-feign-2</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--spring boot 1.X:spring-cloud-starter-hystrix-->
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--spring boot 1.X:spring-cloud-starter-hystrix-dashboard-->
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
</project>

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server:
port: 8901
spring:
application:
name: consumer-feign-2
eureka:
instance:
hostname: localhost
# 发呆时间,即服务失效时间(缺省为90s),就是超过15秒没有续约就会从注册表中剔除
lease-expiration-duration-in-seconds: 15
# 心跳时间,即服务续约间隔时间(缺省为30s)
lease-renewal-interval-in-seconds: 5
client:
service-url:
defaultZone: http://localhost:9999/eureka
PROVIDER-USER:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
# 在feign使用hystrix
feign:
hystrix:
enabled: true
# 熔断器DashBoard: actuator在boot2.0调整后开关web端点的配置,*代表开启所有
management:
endpoints:
web:
exposure:
include: "*"

Service 接口

1
2
3
4
5
6
7
8
/**
* 定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务
**/
@FeignClient(value ="PROVIDER-USER-2",fallback = ServiceHystrixImpl.class)
public interface UserService {
@GetMapping("/user/{id}")
User getUser2(@PathVariable Long id);
}
  • fallback 服务降级
1
2
3
4
5
6
7
@Component
public class ServiceHystrixImpl implements UserService {
@Override
public User getUser2(Long id) {
return new User(12345l);
}
}

查看监控

  1. 启动 hystrix-dashboard-turbine、俩个 Feign 客户端、两个提供者服务、eureka 客户端。
  2. 访问网址http://localhost:8767/hystrix ,输入输入: http://localhost:8767/turbine.stream ,然后点击 Monitor Stream ,可以看到出现了监控列表,分别访问http://localhost:8900/order/1、http://localhost:8901/order/1,显示界面如下:
    springcloud实战之Hystrix(三)_2019-11-19-17-19-13.png
文章目录
  1. 1. java
    1. 1.1. 新建一个子模块 hystrix-dashboard-turbine
      1. 1.1.0.1. 依赖
      2. 1.1.0.2. 配置
      3. 1.1.0.3. 启动类
  2. 1.2. 复制一个提供者服务
    1. 1.2.0.1. 依赖
    2. 1.2.0.2. 配置
  • 1.3. 复制一个 Feign 客户端
    1. 1.3.0.1. 依赖
    2. 1.3.0.2. 配置
    3. 1.3.0.3. Service 接口
  • 2. 查看监控
  • |