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> <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:
name: hystrix-dashboard-turbine eureka: client: serviceUrl:
defaultZone: http://localhost:9999/eureka
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
lease-expiration-duration-in-seconds: 15
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> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <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
lease-expiration-duration-in-seconds: 15
lease-renewal-interval-in-seconds: 5 client: service-url: defaultZone: http://localhost:9999/eureka PROVIDER-USER: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
feign: hystrix: enabled: true
management: endpoints: web: exposure: include: "*"
|
Service 接口
1 2 3 4 5 6 7 8
|
@FeignClient(value ="PROVIDER-USER-2",fallback = ServiceHystrixImpl.class) public interface UserService { @GetMapping("/user/{id}") User getUser2(@PathVariable Long id); }
|
1 2 3 4 5 6 7
| @Component public class ServiceHystrixImpl implements UserService { @Override public User getUser2(Long id) { return new User(12345l); } }
|
查看监控
- 启动 hystrix-dashboard-turbine、俩个 Feign 客户端、两个提供者服务、eureka 客户端。
- 访问网址http://localhost:8767/hystrix ,输入输入: http://localhost:8767/turbine.stream ,然后点击 Monitor Stream ,可以看到出现了监控列表,分别访问http://localhost:8900/order/1、http://localhost:8901/order/1,显示界面如下: