spring cloud 聚合监控Turbine
因为本文大部分代码会重用Hystrix监控面板dashboard文章的代码,所以读者在阅读Hystrix监控面板dashboard文章后理解会更透彻一点
1.版本概述
- spring boot:2.1.9.RELEASE
- spring cloud:Greenwich.SR3
2.示例结构
结构.png
3.代码
在Hystrix监控面板dashboard文章的代码基础上,我会新增一个服务生产者,代码如下:
- pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 启动类
@EnableEurekaClient
@SpringBootApplication
public class ScProviderPeer1Application {
public static void main(String[] args) {
SpringApplication.run(ScProviderPeer1Application.class, args);
}
}
- 接口层
@Slf4j
@RestController
public class MultiplyController {
@Value("${spring.application.name}")
private String name;
@Value("${server.port}")
private String port;
@RequestMapping(value = "/multi",method = RequestMethod.GET)
public Integer multiply(@RequestParam("a") int a,@RequestParam("b") int b){
log.info("{}服务-端口:{}的multiply方法被调用---"+ LocalDateTime.now(),name,port);
return a*b+1;
}
}
- applicaiton.properties
spring.application.name=service-multiply
server.port=7778
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
启动应用,再新增一个服务消费者,方法为直接将feign
应用复制一个启动配置文件并在VM options
中输入-Dserver.port=8888
来确定新的应用端口与之前不重复,如下:
完成以上步骤后,直接应用确定,然后启动它
现在需要创建一个turbine服务端
- pom.xml
<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-turbine</artifactId>
</dependency>
- 启动类
@EnableTurbine
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudTurbineApplication.class, args);
}
}
- application.properties
spring.application.name=turbine
server.port=6666
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.client.service-url.defakltZone=http://localhost:8761/eureka/
#以下为默认配置
#指定要监控的应用,可以是多个应用,用逗号分隔
turbine.app-config=feign
#是否通过主机ip和端口来区分 若选择false,那么我刚才新增的`feign`应用被监控的只有一个
turbine.combine-host-port=true
#处理集群和APP之间的关系
turbine.cluster-name-expression="default"
启动turbine服务端
4.测试
访问hystrix-dashboard(本例为http://localhost:9999/hystrix
)进入主页面,页面如下:
http://localhost:6666/turbine.stream
进入监控页面,页面如下:turbine监控页面.png
这里要说一下:
GetMultiply#multiply(int,int)
:GetMultiply
是服务调用者的接口名称,multiply(int,int)
是接口下的某个方法
service-multiply
:被调用的服务名称
可能有人会注意到上图中的hosts
的值是2,现在修改turbine服务端的配置文件,修改如下:
turbine.combine-host-port=true
,或者直接注释掉,因为默认就是false
,修改后,在进入turbine
监控页面,页面如下:
注意:多台主机,但如果只监控一台的话,智慧监控第一台调用服务的主机,也就是说:如果
8887
端口的主机先调用了服务,那么turbine
监控的就是8887
端口的主机,别的主机后面就算调用了服务也不会监控,试验后得出的结论,源代码都不知道在哪。。。
刚才是turbine
默认配置,可以查看所有接口的调用情况,我们也可以对单独的集群进行监控,如下:
首先需要给服务消费者添加一行代码eureka.instance.metadata-map.cluster=cus
,代表本应属于cus
集群,集群名称自定义就行,现在我新增一个服务消费者sc-feign-peer
,代码如下:
- pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-netflix-eureka-client</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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
- 启动类
@EnableFeignClients
@SpringCloudApplication
public class ScFeignPeerApplication {
public static void main(String[] args) {
SpringApplication.run(ScFeignPeerApplication.class, args);
}
}
- 接口层
@Slf4j
@RestController
public class MultiplyController {
@Autowired
private GetMultiplyPeerService getMultiplyPeerService;
@Autowired
private ServiceInstance serviceInstance;
@RequestMapping(value = "/multi",method = RequestMethod.GET)
public Integer multiplyPeer(){
log.info("{}服务-端口:{}的multiply方法被调用---"+ LocalTime.now(),serviceInstance.getServiceId(),serviceInstance.getPort());
return getMultiplyPeerService.multiplyPeer(1,2);
}
}
- feignService
@FeignClient(value = "service-multiply",fallback = GetMultiplyPeerImpl.class)
public interface GetMultiplyPeerService {
@RequestMapping(value = "/multi",method = RequestMethod.GET)
Integer multiplyPeer(@RequestParam("a") int a, @RequestParam("b") int b);
}
- 断路器调用类
@Slf4j
@Component
public class GetMultiplyPeerImpl implements GetMultiplyPeerService {
@Autowired
private ServiceInstance serviceInstance;
@Override
public Integer multiplyPeer(int a, int b) {
log.error("{}服务-端口:{}调用报错!!",serviceInstance.getServiceId(),serviceInstance.getPort());
return 0;
}
}
- application.properties
spring.application.name=feign-peer
server.port=8886
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
feign.hystrix.enabled=true
eureka.instance.metadata-map.cluster=cus-peer
management.endpoints.enabled-by-default=true
management.endpoints.web.exposure.include=*
启动应用,现在需要修改turbine
服务端配置了,修改部分如下:
#监控的应用列表
turbine.app-config=feign,feign-peer
#监控的集群列表
turbine.aggregator.cluster-config=cus,cus-peer
#监控方案
turbine.cluster-name-expression=metadata['cluster']
下面可以重启turbine
服务器,在hystrix-dashboard
主页面访问http://localhost:6666/turbine.stream?cluster=cus
或者http://localhost:6666/turbine.stream?cluster=cus-peer
来监控cus
和cus-peer
集群,页面分别如下:
cus-peer集群.png
5.代码地址
新增的服务消费者:https://gitee.com/xiongzelin/sc-feign-peer.git
turbine服务端:https://gitee.com/xiongzelin/spring-cloud-turbine.git