sentinel版本:1.6.3
spring-cloud-alibaba-dependencies版本:2.1.0.RELEASE
自动装配通过引入spring-cloud-starter-alibaba-sentinel来引入sentinel
com.alibaba.cloud spring-cloud-starter-alibaba-sentinel
2.1.0版本的spring-cloud-starter-alibaba-sentinel引入了
com.alibaba.cloud spring-cloud-alibaba-sentinel
查看spring-cloud-alibaba-sentinel包里meta-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration, com.alibaba.cloud.sentinel.SentinelWebFluxAutoConfiguration, com.alibaba.cloud.sentinel.endpoint.SentinelEndpointAutoConfiguration, com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration, com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker= com.alibaba.cloud.sentinel.custom.SentinelCircuitBreakerConfiguration
其中SentinelWebAutoConfiguration为入口配置类,注入一个拦截器CommonFilter,拦截 final HeartbeatSender sender, long interval) { pool.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { sender.sendHeartbeat(); } catch (Throwable e) { RecordLog.warn("[HeartbeatSender] Send heartbeat error", e); } } }, 5000, interval, TimeUnit.MILLISECONDS); RecordLog.info("[HeartbeatSenderInit] HeartbeatSender started: " + sender.getClass().getCanonicalName()); }
sender默认加载的实现是SimpleHttpHeartbeatSender,SimpleHttpHeartbeatSender构造方法中获取到了application.yml中配置的
spring.cloud.sentinel.transport.dashboard的配置,使用逗号分割并解析
private ListgetDefaultConsoleIps() { List newAddrs = new ArrayList (); try { // 获取spring.cloud.sentinel.transport.dashboard的配置 String ipsStr = TransportConfig.getConsoleServer(); if (StringUtil.isEmpty(ipsStr)) { RecordLog.warn("[SimpleHttpHeartbeatSender] Dashboard server address not configured"); return newAddrs; } // 使用逗号分割遍历解析 for (String ipPortStr : ipsStr.split(",")) { if (ipPortStr.trim().length() == 0) { continue; } if (ipPortStr.startsWith("http://")) { ipPortStr = ipPortStr.trim().substring(7); } String[] ipPort = ipPortStr.trim().split(":"); int port = 80; if (ipPort.length > 1) { port = Integer.parseInt(ipPort[1].trim()); } newAddrs.add(new InetSocketAddress(ipPort[0].trim(), port)); } } catch (Exception ex) { RecordLog.warn("[SimpleHeartbeatSender] Parse dashboard list failed, current address list: " + newAddrs, ex); ex.printStackTrace(); } return newAddrs; }
sendHeartbeat()方法完成心跳请求发送,请求到sentinel-dashboar的/registry/machine。该接口会存储appName,ip,端口等信息。sentinel控制台就能查询到对应的资源信息
@Override
public boolean sendHeartbeat() throws Exception {
// 判断端口
if (TransportConfig.getRuntimePort() <= 0) {
RecordLog.info("[SimpleHttpHeartbeatSender] Runtime port not initialized, won't send heartbeat");
return false;
}
// 获取第一个ip端口
InetSocketAddress addr = getAvailableAddress();
if (addr == null) {
return false;
}
// 包装请求,请求路径为HEARTBEAT_PATH = /registry/machine
SimpleHttpRequest request = new SimpleHttpRequest(addr, HEARTBEAT_PATH);
// 设置请求参数
request.setParams(heartBeat.generateCurrentMessage());
try {
SimpleHttpResponse response = httpClient.post(request);
if (response.getStatusCode() == OK_STATUS) {
return true;
}
} catch (Exception e) {
RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr + " : ", e);
}
return false;
}



