本文共 4232 字,大约阅读时间需要 14 分钟。
新建hystrix-dashboard-service-hi模块
pom文件
4.0.0 com.xiaobu springcloud-demo 0.0.1-SNAPSHOT hystrix-dashboard-service-hi 0.0.1-SNAPSHOT hystrix-dashboard-service-hi hystrix-dashboard-service-hi project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-netflix-hystrix org.springframework.cloud spring-cloud-starter-netflix-hystrix-dashboard org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
application.properties
eureka.client.service-url.defaultZone=http://localhost:8001/eureka/server.port=8010spring.application.name=hystrix-dashboard-service-hi# 下面的必须配置 不然访问https://hystrix-app:port/actuator/hystrix.stream 会报404management.endpoints.web.exposure.include=*management.endpoints.web.cors.allowed-origins=*management.endpoints.web.cors.allowed-methods=*
HystrixDashboardServiceHiApplication.java
package com.xiaobu;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** * @author xiaobu */@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClient@RestController@EnableHystrix@EnableHystrixDashboard@EnableCircuitBreaker@Slf4jpublic class HystrixDashboardServiceHiApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run( HystrixDashboardServiceHiApplication.class, args ); } @Value("${server.port}") String port; @RequestMapping("/hi") @HystrixCommand(fallbackMethod = "hiError") public String home(@RequestParam(value = "name", defaultValue = "admin") String name) { return "hi " + name + " ,i am from port:" + port; } public String hiError(String name) { return "hi,"+name+",sorry,error!"; } @Override public void run(String... args) throws Exception { log.info("服务hystrix-dashboard-service-hi在端口:{},启动成功....",port); }}
运行程序: 依次开启eureka-server(与第一章相同)和hystrix-dashboard-service-hi.
访问 http://localhost:8010/hi
然后访问 http://localhost:8010/actuator/hystrix.stream
访问 http://localhost:8010/hystrix
依次输入
http://localhost:8010/actuator/hystrix.stream 2000 admin
再次访问 http://localhost:8010/hystrix 出现如下示图
转载地址:http://fkgai.baihongyu.com/