步骤1:需求
步骤2:先运行,看到效果,再学习
步骤3:模仿和排错
步骤4:创建子项目
步骤5:pom.xml
步骤6:ProductServiceTurbineApplication
步骤7:application.yml
步骤8:AccessViewService
步骤 1 : 需求
在上个知识点讲解了针对一个微服务的断路器监控,但是微服务通常会是多个实例组成的一个集群。 倘若集群里的实例比较多,难道要挨个挨个去监控这些实例吗? 何况有时候,根据集群的需要,会动态增加或者减少实例,监控起来就更麻烦了。
所以为了方便监控集群里的多个实例,springCloud 提供了一个 turbine 项目,它的作用是把一个集群里的多个实例汇聚在一个 turbine里,这个然后再在 断路器监控里查看这个 turbine, 这样就能够在集群层面进行监控啦。

步骤 2 : 先运行,看到效果,再学习
老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
1. 首先挨个运行 EurekaServerApplication, ConfigServerApplication, ProductDataServiceApplication, ProductViewServiceFeignApplication:8012,, ProductViewServiceFeignApplication:8013,ProductServiceHystrixDashboardApplication, ProductServiceTurbineApplication.
2. 运行视图微服务里的 AccessViewService 来周期性地访问 http://127.0.0.1:8012/products 和 http://127.0.0.1:8013/products。 因为只有访问了,监控里才能看到数据。
3. 打开监控地址
http://localhost:8020/hystrix
4. 在最上面输入
http: //localhost:8021/turbine.stream
|
这个地址就是汇聚了8012,8013 两个视图微服务 turbine聚合信息。
5. 然后点击 Monitor Stream 就可以看到监控信息了。

步骤 3 : 模仿和排错
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。
推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
步骤 4 : 创建子项目

步骤 5 : pom.xml
一堆jar
< 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 >
< parent >
< groupId >cn.how2j.springcloud</ groupId >
< artifactId >springcloud</ artifactId >
< version >0.0.1-SNAPSHOT</ version >
</ parent >
< artifactId >productServiceTurbine</ artifactId >
< 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 >
</ project >
|
步骤 6 : ProductServiceTurbineApplication
启动类,主要是注解:@EnableTurbine
package cn.how2j.springcloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
import cn.hutool.core.util.NetUtil;
@SpringBootApplication
@EnableTurbine
public class ProductServiceTurbineApplication {
public static void main(String[] args) {
int port = 8021 ;
if (!NetUtil.isUsableLocalPort(port)) {
System.err.printf( "端口%d被占用了,无法启动%n" , port );
System.exit( 1 );
}
new SpringApplicationBuilder(ProductServiceTurbineApplication. class ).properties( "server.port=" + port).run(args);
}
}
|
步骤 7 : application.yml
配置信息,主要是:
appConfig: product-view-service-feign
|
这就表示它会把所有微服务名称是product-view-service-feign 的实例信息都收集起来。
spring:
application.name: turbine
turbine:
aggregator:
clusterConfig: default
appConfig: product-view-service-feign ### 配置Eureka中的serviceId列表,表明监控哪些服务
clusterNameExpression: new String("default")
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
|
步骤 8 : AccessViewService
修改一下AccessViewService, 使得其访问 8012和 8013端口
package cn.how2j.springcloud.util;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpUtil;
public class AccessViewService {
public static void main(String[] args) {
while ( true ) {
ThreadUtil.sleep( 1000 );
access( 8012 );
access( 8013 );
}
}
public static void access( int port) {
try {
String html= HttpUtil.get(String.format( "http://127.0.0.1:%d/products" ,port));
System.out.printf( "%d 地址的视图服务访问成功,返回大小是 %d%n" ,port, html.length());
}
catch (Exception e) {
System.err.printf( "%d 地址的视图服务无法访问%n" ,port);
}
}
}
|
更多内容,点击了解: https://how2j.cn/k/springcloud/springcloud-turbine/2044.html