server 端
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
启动类
@SpringBootApplication
@EnableHystrixDashboard
public class SpringbootHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootHystrixApplication.class, args);
}
}
配置文件
server:
port: 8089
spring:
application:
name: ludb-hystrix #应用名称
eureka:
instance:
prefer-ip-address: true #是否使用IP地址注册(用于eureka展示)
instance-id: ${spring.cloud.client.ip-address}:${server.port} #当前客户端IP:端口
client:
service-url: #设置服务注册中心地址【向注册中心集群注册(注册两台是为了防止第一台宕机还可以挂在第2台上)】
defaultZone: http://ludb:ludb123@localhost:8761/eureka/,http://ludb:ludb123@localhost:8762/eureka/
registry-fetch-interval-seconds: 10 #表示10秒拉取一次服务注册信息,默认30秒
register-with-eureka: true #是否将自己注册到注册中心,默认true
#默认情况下大多数端点都没有通过 http 公开,我们公开了所有端点,以便SpringAdmin访问
management:
endpoint.health.show-details: always
endpoints:
web:
exposure:
include: '*'
hystrix:
dashboard:
proxy-stream-allow-list: localhost
client端
pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
config
package com.eurekaclient3.config;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
public class HystrixConfig {
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}