Spring cloud(六)断路器监控(Dashboard+Turbin)

一、环境
jdk:13
spring cloud:Greenwich.RELEASE
spring boot:2.1.0.RELEASE
spring-cloud-starter-feign:2.0.0.M2
eurekaServer: 前面提到用于服务注册
eurekaClient: 前面提到用于提供服务
configServer: 前面提到用于提供分布式配置服务
serviceConsumer: 前面提到用于消费eurekaClient服务的项目

二、eurekaClient工程改造

  1. HelloService类中再增加一个方法getName()
package com.bry.springcloud.eurekaclient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class HelloService {
	private static Logger log = LoggerFactory.getLogger(HelloService.class);

	@Value("${eureka.instance.hostname}")
	String server;

	@Value("${server.port}")
	String port;

	@Value("${greeter}")
	String greeter;

	@RequestMapping("/hello")
	public String sayHello(String name) {
		log.info("HelloService.sayHello()-> name=" + name);
		return "Hello " + name + " from " + server + ":" + port + ", I am " + greeter;
	}

	@RequestMapping("/getName")
	public String getName() {
		return "Hello I am Bruce, from " + server + ":" + port + ", I am " + greeter;
	}

}

增加新方法主要用来验证多个服务熔断合并显示。

三、serviceConsumer工程改造

  1. 修改pom.xml 文件,添加以下内容
        ......
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
		</dependency>
		......

四、增加新的serviceTest工程
完全参照serviceConsumer创建一个新工程,主要用于演示熔断信息合并显示,其实也可以将serviceConsumer启动两份。
1. 新增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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.bry.springcloud</groupId>
	<artifactId>serviceTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>serviceTest</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath />
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>12</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
			<version>2.0.0.M2</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix</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-zipkin</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
		</dependency>

	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Greenwich.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

 
2. 新增application.yml文件

spring:
  application:
    name: serviceTest
server:
  port: 6549
eureka:
  instance:
    hostname: 127.0.0.1
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:6543/eureka/
feign:
  hystrix:
    enabled: true
management: 
  endpoints:
    web:
      exposure:
        include: "*"

 
3. 新增启动类文件

package com.bry.springcloud.servicetest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class ServiceTestApplication {
   public static void main(String[] args) {
   	SpringApplication.run(ServiceTestApplication.class, args);
   }
}

 
4. 增加HelloService接口用来调用eurekaClient中的restfull服务

package com.bry.springcloud.servicetest;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "eurekaClient", fallback = HelloServiceHystrix.class)
public interface HelloService {

	@RequestMapping(value = "/getName", method = RequestMethod.GET)
	public String getName();

}

5.增加HelloService的熔断实现HelloServiceHystrix类

package com.bry.springcloud.servicetest;

import org.springframework.stereotype.Service;

@Service
public class HelloServiceHystrix implements HelloService {

	public String getName() {
		return "Hello I am EE, Error  occurred! This is default message";
	}
}

6.增加对外的restfull服务器TestService

package com.bry.springcloud.servicetest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestService {
	private static Logger log = LoggerFactory.getLogger(TestService.class);
	@Autowired
	HelloService helloService;
	@RequestMapping("/getname")
	public String getName() {
		log.info("TestService.sayHello()-> ");
		return helloService.getName();
	}
}

五、增加turbineServer工程
1.新增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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.bry.springcloud</groupId>
	<artifactId>turbineServer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>turbineServer</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>12</java.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</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-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>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
		</dependency>


	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Greenwich.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

2.新增application.yml文件

server:
  port: 6548
spring:
  application:
    name: turbineServer
eureka:
  instance:
    hostname: 127.0.0.1
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:6543/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"
turbine:
  app-config: serviceConsumer,serviceTest
  aggregator:
    clusterConfig: default
  clusterNameExpression: new String("default")
  combine-host: true
  instanceUrlSuffix:
    default: actuator/hystrix.stream

3.增加启动类TurbineServerApplication

package com.bry.springcloud.turbineserver;

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.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableTurbine
public class TurbineServerApplication {
	public static void main(String[] args) {
		SpringApplication.run(TurbineServerApplication.class, args);
	}
}

六、测试
1.启动rabbitmq,确保git服务可用
2.启动eurekaServe工程
3.启动configServer工程
4.启动eurekaClient工程
5.启动serviceConsumer工程
6.启动serviceTest工程
7.启动turbineServer工程
8.浏览器中访问http://localhost:6543/
看到下面四个服务说明全部启动成功
在这里插入图片描述
9.浏览器中访问http://localhost:6545/testhello?name=bruce
在这里插入图片描述
10.浏览器中访问http://localhost:6549/getname
在这里插入图片描述
11.浏览器中访问http://localhost:6545/actuator/hystrix.stream
在这里插入图片描述
12.浏览器中访问http://localhost:6549/hystrix
在这里插入图片描述
三个文本框中分别填入http://localhost:6549/actuator/hystrix.stream, 2000, test 然后单击【Monitor Stream】应该出现下面的界面
在这里插入图片描述
13.和step12相同的方式测试6545端口,也应该出现下面的界面
在这里插入图片描述
14.浏览器中访问 http://localhost:6548/turbine.stream 在这里插入图片描述
15.浏览器中访问 http://localhost:6548/hystrix
在这里插入图片描述
三个文本框中分别输入 http://localhost:6548/turbine.stream,2000,test 然后点击【Monitor Stream】 按钮
在这里插入图片描述
出现上图界面,说明Hystix Dashboard+Turbin配置成功!

参考
断路器监控
断路器聚合监控
Circuit Breaker: Hystrix Dashboard

备注:
可以从 https://github.com/zhoupinheng/springclouddemo 下载完整代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值