您好,登錄后才能下訂單哦!
Hystrix Dashboard是一款針對(duì)Hystrix進(jìn)行實(shí)時(shí)監(jiān)控的工具,通過(guò)Hystrix Dashboard可以直觀(guān)地看到各Hystrix Command的請(qǐng)求響應(yīng)時(shí)間,請(qǐng)求成功率等數(shù)據(jù)。
工程名 | 端口 | 作用 |
---|---|---|
eureka-server | 8761 | 注冊(cè)中心 |
service-hi | 8762 | 服務(wù)提供者 |
service-consumer | 8763 | 服務(wù)消費(fèi)者 |
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:/${server.port}/eureka/
spring:
application:
name: eureka-server
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run( EurekaServerApplication.class, args );
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
server:
port: 8762
spring:
application:
name: service-hi
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
@RestController
public class HelloController {
@GetMapping("/hi")
public String hi() {
return "hello ~";
}
@GetMapping("/hey")
public String hey() {
return "hey ~";
}
@GetMapping("/oh")
public String oh() {
return "ah ~";
}
@GetMapping("/ah")
public String ah() {
//模擬接口1/3的概率超時(shí)
Random rand = new Random();
int randomNum = rand.nextInt(3) + 1;
if (3 == randomNum) {
try {
Thread.sleep( 3000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "來(lái)了老弟~";
}
}
@SpringBootApplication
@EnableEurekaClient
public class ServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceHiApplication.class, args );
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</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-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
server:
port: 8763
tomcat:
uri-encoding: UTF-8
max-threads: 1000
max-connections: 20000
spring:
application:
name: service-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
@Service
public class HelloService {
@Autowired
private RestTemplate restTemplate;
/**
* 簡(jiǎn)單用法
*/
@HystrixCommand
public String hiService() {
return restTemplate.getForObject("http://SERVICE-HI/hi" , String.class);
}
/**
* 定制超時(shí)
*/
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "30000") })
public String heyService() {
return restTemplate.getForObject("http://SERVICE-HI/hey" , String.class);
}
/**
* 定制降級(jí)方法
*/
@HystrixCommand(fallbackMethod = "getFallback")
public String ahService() {
return restTemplate.getForObject("http://SERVICE-HI/ah" , String.class);
}
/**
* 定制線(xiàn)程池隔離策略
*/
@HystrixCommand(fallbackMethod = "getFallback",
threadPoolKey = "studentServiceThreadPool",
threadPoolProperties = {
@HystrixProperty(name="coreSize", value="30"),
@HystrixProperty(name="maxQueueSize", value="50")
}
)
public String ohService() {
return restTemplate.getForObject("http://SERVICE-HI/oh" , String.class);
}
public String getFallback() {
return "Oh , sorry , error !";
}
}
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hi")
public String hi() {
return helloService.hiService();
}
@GetMapping("/hey")
public String hey() {
return helloService.heyService();
}
@GetMapping("/oh")
public String oh() {
return helloService.ohService();
}
@GetMapping("/ah")
public String ah() {
return helloService.ahService();
}
}
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableHystrix
@EnableCircuitBreaker
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceConsumerApplication.class, args );
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
先訪(fǎng)問(wèn)http://localhost:8762/hi
再打開(kāi)http://localhost:8763/actuator/hystrix.stream,可以看到一些具體的數(shù)據(jù):
單純的查看json數(shù)據(jù),很難分析出結(jié)果,所以,要在Hystrix儀表盤(pán)中來(lái)查看這一段json,在hystrix儀表盤(pán)中輸入監(jiān)控地址進(jìn)行監(jiān)控:
打開(kāi)儀表盤(pán)地址:http://localhost:8762/hystrix
在界面依次輸入:http://localhost:8763/actuator/hystrix.stream 、2000 、service-consumer;點(diǎn)確定。
編一個(gè)測(cè)試腳本curl.sh
while true;
do
curl "http://localhost:8763/hi";
curl "http://localhost:8763/hey";
curl "http://localhost:8763/oh";
curl "http://localhost:8763/ah";
done
執(zhí)行測(cè)試腳本,查看Hystrix儀表盤(pán):
可以看出 ahService 因?yàn)槟M了1/3的概率超時(shí),所以監(jiān)控中呈現(xiàn)了30%左右的錯(cuò)誤百分比。
https://github.com/gf-huanchupk/SpringCloudLearning/tree/master/chapter17
歡迎關(guān)注我的公眾號(hào)《程序員果果》,關(guān)注有驚喜~~
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。