栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Sentinel

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Sentinel

Sentinel源码解析 Sentinel版本

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 List getDefaultConsoleIps() {
        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;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/434682.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号