最近升级了项目springboot使用2.0版本,对应springcloud升级到Finchley.RELEASE。
因此以下的构建方法都是基于2.0的。高版本和低版本是有点区别的。首先依赖的名字不同,其次访问路径也有所不同。
监控应用
1. 配置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-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 配置yml
server:
port: 9001
eureka:
instance:
preferIpAddress: true
client:
serviceUrl:
defaultZone: http://localhost:7001/eureka/
spring:
application:
name: HystrixDashboard9001
3. 启动类
@SpringBootApplication
@EnableHystrixDashboard
@EnableDiscoveryClient
public class HystrixDashApplication9001 {
public static void main(String[] args) {
SpringApplication.run(HystrixDashApplication9001.class, args);
}
}
被监控应用
1. 配置pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2. 配置yml
server:
port: 8001
spring:
application:
name: GateView8001
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/gateview?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
username: root
password: 1234
# password: '{cipher}f26bfd9f2de080484c1d90317543f99d521d48d6f623547bd6d0444ee48d5bcd'
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时
jpa:
database: MYSQL
hibernate:
ddl-auto: update
naming-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
aop:
auto: true #启动aop配置
mvc:
view:
prefix: /
suffix: .html
resources:
static-locations: classpath:/static,classpath:/templates,classpath:/resources,classpath:/META-INF/resources
security:
basic:
# 关闭自带的安全验证
enabled: false
logging:
level:
com.nikola: DEBUG
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7001/eureka
feign:
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
include: ["health","info","hystrix.stream"]
这里需要注意需要暴露hystrix.stream访问接口
management:
endpoints:
web:
exposure:
include: ["health","info","hystrix.stream"]
3. 访问的接口
@GetMapping("/class/{id}")
@HystrixCommand(fallbackMethod="getSysClassError")
public String getSysClass(@PathVariable("id") Long id) throws InterruptedException, ExecutionException {
log.info("启动线程查找班级Id为: "+id);
CompletableFuture<String> cf = sysClassHighServiceI.getSysClass(id).thenApply(classPO->{
JSONObject jsonObj = new JSONObject((Object) classPO);
return jsonObj.toString();
});
return cf.get();
}
private String getSysClassError(@PathVariable("id") Long id) {
return "获取班级失败, id = [" + id + "]";
}
启动验证
在浏览器输入http://localhost:9001/hystrix,进入hystrixdashboard首页.
在文本框中输入要监控的服务地址http://localhost:8001/actuator/hystrix.stream
点击下方按钮,即可