那么数据服务什么时候可用,什么时候不可用,如何监控这个事情呢? 我们就要用到 断路器监控 来可视化掌控这个情况。
创建监控子项目
创建子项目hystrix-dashboard 。
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
springcloud
edu.hpu.springcloud
1.0-SNAPSHOT
4.0.0
hystrix-dashboard
hystrix-dashboard
http://www.example.com
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
断路器监控启动类
ProductServiceHystrixDashboardApplication,主要就是这个注解@EnableHystrixDashboard,表示开启仪表盘功能。
package edu.hpu.springcloud;
import cn.hutool.core.util.NetUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class ProductServiceHystrixDashboardApplication {
public static void main(String[] args) {
int port = 8020;
if(!NetUtil.isUsableLocalPort(port)) {
System.err.printf(“端口%d被占用了,无法启动%n”, port );
System.exit(1);
}
new SpringApplicationBuilder(ProductServiceHystrixDashboardApplication.class).properties(“server.port=” + port).run(args);
}
}
application.yml
spring:
application:
name: hystrix-dashboard
ProductViewServiceFeignApplication
修改视图微服务,主要是在启动类中添加注解@EnableCircuitBreaker,使其可以把消息发送给监控中心。
package edu.hpu.springcloud;
import brave.sampler.Sampler;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NetUtil;
import cn.hutool.core.util.NumberUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
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.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class ProductViewServiceFeignApplication {
public static void main(String[] args) {
//判断 rabiitMQ 是否启动
int rabbitMQPort = 5672;
if(NetUtil.isUsableLocalPort(rabbitMQPort)) {
System.err.printf(“未在端口%d 发现 rabbitMQ服务,请检查rabbitMQ 是否启动”, rabbitMQPort );
System.exit(1);
}
int port = 0;
int defaultPort = 8012;
Future future = ThreadUtil.execAsync(() ->{
int p = 0;
System.out.println(“请于5秒钟内输入端口号, 推荐 8012 、 8013 或者 8014,超过5秒将默认使用”+defaultPort);
Scanner scanner = new Scanner(System.in);
while(true) {
String strPort = scanner.nextLine();
if(!NumberUtil.isInteger(strPort)) {
System.err.println(“只能是数字”);
continue;
总结
在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。
) {
System.err.println(“只能是数字”);
continue;
总结
在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。
[外链图片转存中…(img-9mnwHwZl-1719174784631)]
[外链图片转存中…(img-JUVjMzPc-1719174784632)]
[外链图片转存中…(img-tpjUyCae-1719174784633)]
[外链图片转存中…(img-RgZg65YZ-1719174784633)]