一、环境
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工程改造
- 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工程改造
- 修改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 下载完整代码。