分布式跟踪系统Zipkin详解(3)

OkHttpClient 添加拦截器,需要clientRequestInterceptor,clientResponseInterceptor 分别完成cs和cr操作,该功能由
brave中的brave-okhttp模块提供,同样的道理如果需要记录数据库的延迟只要在数据库操作前后完成cs和cr即可,当然brave提供其封装。

以上还缺少一个配置信息ZipkinProperties.java

package com.lkl.zipkin.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; /** * Created by liaokailin on 16/7/28. */ @Configuration @ConfigurationProperties(prefix = "com.zipkin") public class ZipkinProperties { private String serviceName; private String url; private int connectTimeout; private int readTimeout; private int flushInterval; private boolean compressionEnabled; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public int getConnectTimeout() { return connectTimeout; } public void setConnectTimeout(int connectTimeout) { this.connectTimeout = connectTimeout; } public int getReadTimeout() { return readTimeout; } public void setReadTimeout(int readTimeout) { this.readTimeout = readTimeout; } public int getFlushInterval() { return flushInterval; } public void setFlushInterval(int flushInterval) { this.flushInterval = flushInterval; } public boolean isCompressionEnabled() { return compressionEnabled; } public void setCompressionEnabled(boolean compressionEnabled) { this.compressionEnabled = compressionEnabled; } public String getServiceName() { return serviceName; } public void setServiceName(String serviceName) { this.serviceName = serviceName; } }

则可以在配置文件application.properties中配置相关信息

com.zipkin.serviceName=service1 com.zipkin.url=http://110.173.14.57:9411 com.zipkin.connectTimeout=6000 com.zipkin.readTimeout=6000 com.zipkin.flushInterval=1 com.zipkin.compressionEnabled=true server.port=8080

那么其中的service1即完成,同样的道理,修改配置文件(调整com.zipkin.serviceName,以及server.port)以及controller对应的方法构造若干服务

service1 中访问:8080/start需要访问:9090/foo,则构造server2提供该方法

server2配置

com.zipkin.serviceName=service2 com.zipkin.url=http://110.173.14.57:9411 com.zipkin.connectTimeout=6000 com.zipkin.readTimeout=6000 com.zipkin.flushInterval=1 com.zipkin.compressionEnabled=true server.port=9090

controller方法

@RequestMapping("foo") public String foo() throws InterruptedException, IOException { Random random = new Random(); int sleep= random.nextInt(100); TimeUnit.MILLISECONDS.sleep(sleep); Request request = new Request.Builder().url("http://localhost:9091/bar").get().build(); //service3 Response response = client.newCall(request).execute(); String result = response.body().string(); request = new Request.Builder().url("http://localhost:9092/tar").get().build(); //service4 response = client.newCall(request).execute(); result += response.body().string(); return " [service2 sleep " + sleep+" ms]" + result; }

在server2中调用server3和server4中的方法

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/f78ef258305c19bcf8576bcaef6b84ab.html