1.新建项目hystrixDashboard, pom.xml中hystrix相关依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
2.hystrixDashboard application.properties
server.port=8080 spring.application.type=hystrixDashboard spring.application.group=group spring.application.name=${spring.application.group}-${spring.application.type} dashboard.register.host=http:// eureka.client.service-url.defaultZone=${dashboard.register.host}/eureka eureka.instance.lease-renewal-interval-in-seconds=10 eureka.instance.lease-expiration-duration-in-seconds=30 eureka.instance.prefer-ip-address=true eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port} eureka.client.eureka-server-read-timeout-seconds=60
3.hystrixDashboard 启动入口配置
@EnableEurekaClient @SpringBootApplication @EnableHystrix @EnableHystrixDashboard public class HystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(HystrixDashboardApplication.class, args); } }
4. 启动后访问 http://localhost:8080/hystrix
5. 新建测试项目hystrix, pom.xml依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
6.hystrix application.properties
server.port=8081 spring.application.type=hystrix spring.application.group=group spring.application.name=${spring.application.group}-${spring.application.type} dashboard.register.host=http:// eureka.client.service-url.defaultZone=${dashboard.register.host}/eureka eureka.instance.lease-renewal-interval-in-seconds=10 eureka.instance.lease-expiration-duration-in-seconds=30 eureka.instance.prefer-ip-address=true eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port} eureka.client.eureka-server-read-timeout-seconds=60
7. 测试项目启动入口配置
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @RestController @EnableHystrix public class HystrixApplication { public static void main(String[] args) { SpringApplication.run(HystrixApplication.class, args); } @RequestMapping("/test") @HystrixCommand(fallbackMethod = "error") public String test(String name) { return new RestTemplate().getForObject("http://一个不可访问的地址/hi?name=" + name, String.class); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public String error(String name) { return "error"; } }
8. 启动hystrix, 访问http://localhost:8081/test?name=123
9.访问hystrix dashboard