ITPub博客

首页 > IT职业 > IT生活 > .NetCore实践篇:成功解决分布式监控ZipKin聚合依赖问题(三

.NetCore实践篇:成功解决分布式监控ZipKin聚合依赖问题(三

IT生活 作者:tomlzy 时间:2021-09-09 10:19:35 0 删除 编辑

今日重点--成功启动zipkin-dependencies

上次被zipkin-dependencies的启动问题卡了很晚,就结束了那篇文章,今天继续解决问题。我从网上搜到一篇类似的博文 部署生产环境时踩到的一些坑,里面提到直接在tomcat的catalina.sh的JAVA_OPTS注释处,加一行JAVA_OPTS="-server -Xms1024m -Xmx1624m -XX:PermSize=128M -XX:MaxPermSize=256m",即可解决。

#   JAVA_OPTS       (Optional) Java runtime options used when any command#                   is executed.#                   Include here and not in CATALINA_OPTS all options, that#                   should be used by Tomcat and also by the stop process,#                   the version command etc.#                   Most options should go into CATALINA_OPTS.JAVA_OPTS="-server -Xms1024m -Xmx1624m -XX:PermSize=128M -XX:MaxPermSize=256m"

tomcat启动成功。

[root@izwz9fwifc2eniq3lbdzmgz bin]# ./startup.sh Using CATALINA_BASE:   /usr/local/tomcat/apache-tomcat-8.5.32
Using CATALINA_HOME:   /usr/local/tomcat/apache-tomcat-8.5.32
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-8.5.32/temp
Using JRE_HOME:        /usr/lib/jdk1.8.0_181
Using CLASSPATH:       /usr/local/tomcat/apache-tomcat-8.5.32/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-8.5.32/bin/tomcat-juli.jar
Tomcat started.

后来又搜到这条链接how to increase heap size?
原来直接在执行java命令时,追加-Xmx就行了,脑袋太死板,想不到这个点。

[root@izwz9fwifc2eniq3lbdzmgz cusD]# java -Xmx1024m -versionjava version "1.8.0_181"Java(TM) SE Runtime Environment (build 1.8.0_181-b13)Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

提示内存不足,分配失败。

[root@izwz9fwifc2eniq3lbdzmgz cusD]# STORAGE_TYPE=cassandra3  java -Xmx1024m -Xms1024m -jar zipkin-dependencies.jar `date -u -d '1 day ago' +%F`Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000d5550000, 715849728, 0) failed; error='Cannot allocate memory' (errno=12)## There is insufficient memory for the Java Runtime Environment to continue.# Native memory allocation (mmap) failed to map 715849728 bytes for committing reserved memory.# An error report file with more information is saved as:# /cusD/hs_err_pid6871.log

解决步骤:

执行刚才的vi catalina.sh,清理掉设置的JAVA_OPTs信息,关掉mysql,重启tomcat等步骤。利用free或top查看内存消耗。

[root@izwz9fwifc2eniq3lbdzmgz cusD]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        228M        1.2G        388K        396M        1.4G
Swap:            0B          0B          0B
[root@izwz9fwifc2eniq3lbdzmgz cusD]# STORAGE_TYPE=cassandra3  java -Xmx512m -Xms128m -jar zipkin-dependencies.jar `date -u -d '1 day ago' +%F`18/09/17 16:42:21 INFO CassandraDependenciesJob: Running Dependencies job for 2018-09-16: 1537056000000000 ≤ Span.timestamp 1537142399999999
18/09/17 16:42:21 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
18/09/17 16:42:22 WARN Java7Support: Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added
Exception in thread "main" java.io.IOException: Failed to open native connection to Cassandra at {127.0.0.1}:9042

未能加载JDK7 types,不过日志级别为Warn,可以忽略。

启动成功后,dependency依然没有数据, 需要继续查询问题。

今日重点--深思流程解决span聚合问题

上次是由于启动问题,今天解决了启动问题,再看不到数据,就需要深思整个流程了。
再让我们回忆下下面这张图,parentid为空,是不是意味着跨站点需要强指定parentid或站点名称?

图片描述


继续翻看zipkin4Net示例代码,发现使用了IHttpClientFactory的CreateClient方法,又在ConfigureServices里指定了applicationName,也许这才是能显示出聚合站点的关键!


namespace frontend
{    public class Startup : CommonStartup
    {        public override void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpClient("Tracer").AddHttpMessageHandler(provider =>
                TracingHandler.WithoutInnerHandler(provider.GetService<IConfiguration>()["applicationName"]));
        }        protected override void Run(IApplicationBuilder app, IConfiguration config)
        {
            app.Run(async (context) =>
            {
                var callServiceUrl = config["callServiceUrl"];
                var clientFactory = app.ApplicationServices.GetService<IHttpClientFactory>();                using (var httpClient = clientFactory.CreateClient("Tracer"))
                {
                    var response = await httpClient.GetAsync(callServiceUrl);                    if (!response.IsSuccessStatusCode)
                    {
                        await context.Response.WriteAsync(response.ReasonPhrase);
                    }                    else
                    {
                        var content = await response.Content.ReadAsStringAsync();
                        await context.Response.WriteAsync(content);
                    }
                }
            });
        }
    }
}

继续按照示例代码,修改我们的逻辑,封装一个可供其他站点调用的HTTPHelper帮助类,提供能追踪站点的GetAsync方法,

namespace Demo.ZipkinCommon
{    public class HTTPHelper : ControllerBase
    {        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="url"></param>
        /// <param name="keyValues"></param>
        /// <param name="timeout"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static async Task<string> GetAsync(string url, Dictionary<string, string> keyValues, int timeout = 0, Encoding encoding = null)
        {            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            var appName = ConfigureSettings.AppSettingConfig["applicationName"];            using (var httpClient = new HttpClient(new TracingHandler(appName)))
            {
                var response = await httpClient.GetAsync(url);                if (!response.IsSuccessStatusCode)
                {                    return response.ReasonPhrase;
                }                else
                {
                    var content = await response.Content.ReadAsStringAsync();                    return content;
                }
            }
        }
    }
}

两个站点的Add方法做出修正,然后查看监控数据。

 [HttpPost]        public IActionResult Add([FromBody]User user)
        {
            _userService.AddUser(user);            //模拟调用其他站点请求。
            var url = $"{ConfigEx.WebSite}/user/get?id={user.Id}";            var content = HTTPHelper.GetAsync(url, null);            return Content(content.Result);
        }



监控spans有两级了,达到了我们要的效果

图片描述

监控层级


图片描述

点开查看效果

图片描述

站点聚合

图片描述

聚合效果图


经测试,重启linux后,不开启zipkin-dependencies的情况下,内存模式下依然能实时聚合,上篇文章的结论是本人不熟悉所导致。



作者:范存威
链接:https://www.jianshu.com/p/369afe848e56


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3278/viewspace-2820200/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-13

  • 博文量
    83
  • 访问量
    653