├── .gitignore ├── .mvn └── wrapper │ └── maven-wrapper.properties ├── README.md ├── filter-opentracing ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── weibo │ │ └── api │ │ └── motan │ │ └── filter │ │ └── sleuth │ │ ├── SleuthTracerFactory.java │ │ ├── SleuthTracingContext.java │ │ └── SleuthTracingFilter.java │ └── resources │ └── META-INF │ └── services │ └── com.weibo.api.motan.filter.Filter ├── goods ├── pom.xml └── src │ └── main │ ├── java │ └── moe │ │ └── cnkirito │ │ └── sleuth │ │ └── goods │ │ ├── GoodsApp.java │ │ ├── config │ │ └── MotanConfig.java │ │ ├── service │ │ └── GoodsApiImpl.java │ │ └── web │ │ ├── GoodsController.java │ │ └── model │ │ └── MainOrder.java │ └── resources │ └── application.yml ├── interface ├── pom.xml └── src │ └── main │ └── java │ └── moe │ └── cnkirito │ └── sleuth │ └── api │ └── GoodsApi.java ├── mvnw ├── mvnw.cmd ├── order ├── pom.xml └── src │ └── main │ ├── java │ └── moe │ │ └── cnkirito │ │ └── sleuth │ │ └── order │ │ ├── OrderApp.java │ │ ├── config │ │ └── MotanConfig.java │ │ ├── model │ │ └── MainOrder.java │ │ └── web │ │ └── OrderController.java │ └── resources │ └── application.yml ├── pom.xml └── zipkin-server ├── pom.xml └── src └── main ├── java └── moe │ └── cnkirito │ └── sleuth │ └── ZipkinServerApp.java └── resources ├── application.yml └── mysql.sql /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin 2 | 3 | *.class 4 | *.log 5 | log/ 6 | *.jar 7 | *.war 8 | *.ear 9 | *.zip 10 | *.tar.gz 11 | *.rar 12 | .idea/ 13 | out/ 14 | bin/ 15 | tmp/ 16 | *.tmp 17 | *.bak 18 | .settings/ 19 | .project 20 | .classpath 21 | .target 22 | target/ 23 | *.iml -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sleuth-starter 2 | 实现http和motan的链路监控 3 | -------------------------------------------------------------------------------- /filter-opentracing/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | sleuth-starter 7 | moe.cnkirito 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | filter-opentracing 13 | 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-starter-sleuth 18 | 19 | 20 | com.weibo 21 | motan-core 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /filter-opentracing/src/main/java/com/weibo/api/motan/filter/sleuth/SleuthTracerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009-2016 Weibo, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.weibo.api.motan.filter.sleuth; 17 | 18 | import org.springframework.cloud.sleuth.Tracer; 19 | 20 | /** 21 | * 22 | * @Description SleuthTracerFactory 23 | * @author huang 24 | * @date 2017.05.11 25 | * 26 | */ 27 | public interface SleuthTracerFactory { 28 | /** 29 | * get a Tracer implementation. this method may called every request, consider whether singleton 30 | * pattern is needed 31 | * 32 | * @return 33 | */ 34 | Tracer getTracer(); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /filter-opentracing/src/main/java/com/weibo/api/motan/filter/sleuth/SleuthTracingContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009-2016 Weibo, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 | * in compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License 10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 | * or implied. See the License for the specific language governing permissions and limitations under 12 | * the License. 13 | */ 14 | package com.weibo.api.motan.filter.sleuth; 15 | 16 | import com.weibo.api.motan.rpc.RpcContext; 17 | import org.springframework.cloud.sleuth.Span; 18 | import org.springframework.cloud.sleuth.Tracer; 19 | 20 | /** 21 | * 22 | * @Description SleuthTracingContext hold a SleuthTracerFactory which can replaced by different tracer 23 | * implementation. 24 | * @author zhanglei 25 | * @date Dec 8, 2016 26 | * 27 | */ 28 | public class SleuthTracingContext { 29 | // replace SleuthTracerFactory with any tracer implementation 30 | public static SleuthTracerFactory tracerFactory = null; 31 | public static final String ACTIVE_SPAN = "ot_active_span"; 32 | 33 | public static Tracer getTracer() { 34 | return tracerFactory.getTracer(); 35 | } 36 | 37 | public static Span getActiveSpan() { 38 | Object span = RpcContext.getContext().getAttribute(ACTIVE_SPAN); 39 | if (span != null && span instanceof Span) { 40 | return (Span) span; 41 | } 42 | return null; 43 | } 44 | 45 | public static void setActiveSpan(Span span) { 46 | RpcContext.getContext().putAttribute(ACTIVE_SPAN, span); 47 | } 48 | 49 | public void setTracerFactory(SleuthTracerFactory tracerFactory) { 50 | SleuthTracingContext.tracerFactory = tracerFactory; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /filter-opentracing/src/main/java/com/weibo/api/motan/filter/sleuth/SleuthTracingFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2009-2016 Weibo, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 | * in compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License 10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 | * or implied. See the License for the specific language governing permissions and limitations under 12 | * the License. 13 | */ 14 | package com.weibo.api.motan.filter.sleuth; 15 | 16 | import com.weibo.api.motan.core.extension.Activation; 17 | import com.weibo.api.motan.core.extension.SpiMeta; 18 | import com.weibo.api.motan.filter.Filter; 19 | import com.weibo.api.motan.rpc.Caller; 20 | import com.weibo.api.motan.rpc.Provider; 21 | import com.weibo.api.motan.rpc.Request; 22 | import com.weibo.api.motan.rpc.Response; 23 | import com.weibo.api.motan.util.LoggerUtil; 24 | import com.weibo.api.motan.util.MotanFrameworkUtil; 25 | import org.springframework.cloud.sleuth.Span; 26 | import org.springframework.cloud.sleuth.SpanInjector; 27 | import org.springframework.cloud.sleuth.Tracer; 28 | import org.springframework.cloud.sleuth.instrument.messaging.TraceMessageHeaders; 29 | import org.springframework.cloud.sleuth.instrument.web.HttpTraceKeysInjector; 30 | import org.springframework.cloud.sleuth.instrument.web.TraceRequestAttributes; 31 | import org.springframework.cloud.sleuth.trace.DefaultTracer; 32 | 33 | import java.util.Map.Entry; 34 | 35 | /** 36 | * 37 | * @Description This filter enables distributed tracing in Motan clients and servers via @see The OpenTracing Project : a set of consistent, 39 | * expressive, vendor-neutral APIs for distributed tracing and context propagation. 40 | * @author zhanglei 41 | * @date Dec 8, 2016 42 | * 43 | */ 44 | @SpiMeta(name = "sleuth-tracing") 45 | @Activation(sequence = 30) 46 | public class SleuthTracingFilter implements Filter { 47 | 48 | public static final String MOTAN_TAG = "motan"; 49 | 50 | @Override 51 | public Response filter(Caller caller, Request request) { 52 | Tracer tracer = getTracer(); 53 | if (tracer == null ||! (tracer instanceof DefaultTracer)) { 54 | return caller.call(request); 55 | } 56 | if (caller instanceof Provider) { // server end 57 | return processProviderTrace(tracer, caller, request); 58 | } else { // client end 59 | return processRefererTrace(tracer, caller, request); 60 | } 61 | } 62 | 63 | protected Tracer getTracer(){ 64 | return SleuthTracingContext.getTracer(); 65 | } 66 | 67 | /** 68 | * process trace in client end 69 | * 70 | * @param caller 71 | * @param request 72 | * @return 73 | */ 74 | protected Response processRefererTrace(Tracer tracer, Caller caller, Request request) { 75 | // String operationName = buildOperationName(request); 76 | Span span = extractTraceInfo(request, tracer); 77 | /*Span.SpanBuilder spanBuilder = tracer.buildSpan(operationName); 78 | Span activeSpan = SleuthTracingContext.getActiveSpan(); 79 | if (activeSpan != null) { 80 | spanBuilder.asChildOf(activeSpan); 81 | } 82 | Span span = spanBuilder.start(); 83 | span.setTag("requestId", request.getRequestId());*/ 84 | // Span span = tracer.createSpan(operationName,tracer.getCurrentSpan()); 85 | // span.logEvent("motan request created"); 86 | span.logEvent(Span.CLIENT_SEND); 87 | if(span.getSavedSpan()!=null && span.getSavedSpan().tags()!=null){ 88 | for (Entry stringStringEntry : span.getSavedSpan().tags().entrySet()) { 89 | setHeader(request, stringStringEntry.getKey(), stringStringEntry.getValue()); 90 | } 91 | } 92 | span.tag("requestId", String.valueOf(request.getRequestId())); 93 | span.tag(Span.SPAN_LOCAL_COMPONENT_TAG_NAME, MOTAN_TAG); 94 | attachTraceInfo(tracer, span, request); 95 | return process(tracer,caller, request, span); 96 | 97 | } 98 | 99 | protected Response process(Tracer tracer, Caller caller, Request request, Span span) { 100 | Exception ex = null; 101 | boolean exception = true; 102 | try { 103 | Response response = caller.call(request); 104 | if (response.getException() != null) { 105 | ex = response.getException(); 106 | } else { 107 | exception = false; 108 | } 109 | return response; 110 | } catch (RuntimeException e) { 111 | ex = e; 112 | throw e; 113 | } finally { 114 | try { 115 | if (exception) { 116 | // span.log("request fail." + (ex == null ? "unknown exception" : ex.getMessage())); 117 | span.logEvent("motan request fail." + (ex == null ? "unknown exception" : ex.getMessage())); 118 | } else { 119 | // span.log("request success."); 120 | if(caller instanceof Provider){ 121 | span.logEvent(Span.SERVER_SEND); 122 | 123 | }else { 124 | span.logEvent(Span.CLIENT_RECV); 125 | 126 | } 127 | } 128 | // span.finish(); 129 | tracer.close(span); 130 | } catch (Exception e) { 131 | LoggerUtil.error("opentracing span finish error!", e); 132 | } 133 | } 134 | } 135 | 136 | protected String buildOperationName(Request request) { 137 | return "Motan_" + MotanFrameworkUtil.getGroupMethodString(request); 138 | } 139 | 140 | SpanInjector spanInjector; 141 | HttpTraceKeysInjector httpTraceKeysInjector; 142 | 143 | private void setHeader(Request request,String key ,Object value){ 144 | if(value!=null){ 145 | request.setAttachment(key,value.toString()); 146 | } 147 | } 148 | 149 | private Long getParentId(Span span) { 150 | return !span.getParents().isEmpty() ? span.getParents().get(0) : null; 151 | } 152 | 153 | protected void attachTraceInfo(Tracer tracer, Span span, final Request request) { 154 | 155 | if (span == null) { 156 | setHeader(request, Span.SAMPLED_NAME, Span.SPAN_NOT_SAMPLED); 157 | return; 158 | } 159 | setHeader(request, TraceRequestAttributes.HANDLED_SPAN_REQUEST_ATTR, "true"); 160 | setHeader(request, Span.SPAN_ID_NAME, Span.idToHex(span.getSpanId())); 161 | setHeader(request, Span.TRACE_ID_NAME, span.traceIdString()); 162 | setHeader(request, Span.SPAN_NAME_NAME, span.getName()); 163 | setHeader(request, Span.SAMPLED_NAME, span.isExportable() ? 164 | Span.SPAN_SAMPLED : Span.SPAN_NOT_SAMPLED); 165 | setHeader(request, Span.PARENT_ID_NAME,Span.idToHex(getParentId(span))); 166 | setHeader(request, Span.PROCESS_ID_NAME, span.getProcessId()); 167 | 168 | if(span.getSavedSpan()!=null && span.getSavedSpan().tags()!=null){ 169 | for (Entry stringStringEntry : span.getSavedSpan().tags().entrySet()) { 170 | setHeader(request, stringStringEntry.getKey(), stringStringEntry.getValue()); 171 | } 172 | } 173 | /*spanInjector.inject(span,null); 174 | 175 | tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new TextMap() { 176 | 177 | @Override 178 | public void put(String key, String value) { 179 | request.setAttachment(key, value); 180 | } 181 | 182 | @Override 183 | public Iterator> iterator() { 184 | throw new UnsupportedOperationException("TextMapInjectAdapter should only be used with Tracer.inject()"); 185 | } 186 | });*/ 187 | } 188 | 189 | /** 190 | * process trace in server end 191 | * 192 | * @param caller 193 | * @param request 194 | * @return 195 | */ 196 | protected Response processProviderTrace(Tracer tracer, Caller caller, Request request) { 197 | Span span = extractTraceInfo(request, tracer); 198 | span.tag("requestId", String.valueOf(request.getRequestId())); 199 | span.logEvent(Span.SERVER_RECV); 200 | SleuthTracingContext.setActiveSpan(span); 201 | return process(tracer,caller, request, span); 202 | } 203 | 204 | protected Span extractTraceInfo(Request request, Tracer tracer) { 205 | Span parentSpan = tracer.getCurrentSpan(); 206 | if(parentSpan==null){ 207 | Span.SpanBuilder spanBuilder = Span.builder(); 208 | if(request.getAttachments().get(TraceMessageHeaders.TRACE_ID_NAME)!=null && 209 | !"".equals(request.getAttachments().get(TraceMessageHeaders.TRACE_ID_NAME))){ 210 | spanBuilder.traceId(Span.hexToId(request.getAttachments().get(TraceMessageHeaders.TRACE_ID_NAME))); 211 | spanBuilder.spanId(Span.hexToId(request.getAttachments().get(TraceMessageHeaders.SPAN_ID_NAME))); 212 | spanBuilder.exportable(Span.SPAN_SAMPLED.equals(request.getAttachments().get(TraceMessageHeaders.SAMPLED_NAME))); 213 | spanBuilder.processId(request.getAttachments().get(TraceMessageHeaders.PROCESS_ID_NAME)); 214 | spanBuilder.parent(Span.hexToId(request.getAttachments().get(TraceMessageHeaders.PARENT_ID_NAME))); 215 | spanBuilder.name(request.getAttachments().get(TraceMessageHeaders.SPAN_NAME_NAME)); 216 | spanBuilder.remote(true); 217 | parentSpan = spanBuilder.build(); 218 | }else if(request.getAttachments().get(Span.TRACE_ID_NAME)!=null && 219 | !"".equals(request.getAttachments().get(Span.TRACE_ID_NAME))){ 220 | spanBuilder.traceId(Span.hexToId(request.getAttachments().get(Span.TRACE_ID_NAME))); 221 | spanBuilder.spanId(Span.hexToId(request.getAttachments().get(Span.SPAN_ID_NAME))); 222 | spanBuilder.exportable(Span.SPAN_SAMPLED.equals(request.getAttachments().get(Span.SAMPLED_NAME))); 223 | spanBuilder.processId(request.getAttachments().get(Span.PROCESS_ID_NAME)); 224 | if(request.getAttachments().get(Span.PARENT_ID_NAME)!=null){ 225 | spanBuilder.parent(Span.hexToId(request.getAttachments().get(Span.PARENT_ID_NAME))); 226 | } 227 | spanBuilder.name(request.getAttachments().get(Span.SPAN_NAME_NAME)); 228 | spanBuilder.remote(true); 229 | parentSpan = spanBuilder.build(); 230 | } 231 | } 232 | String operationName = buildOperationName(request); 233 | Span newSpan = tracer.createSpan("motan:"+request.getMethodName(),parentSpan); 234 | newSpan.tag("motan_method",operationName); 235 | /*for (Object o : request.getAttachments()) { 236 | 237 | }*/ 238 | /*for (Entry stringStringEntry : request.getAttachments().entrySet()) { 239 | if(stringStringEntry.getValue()!=null){ 240 | newSpan.tag(stringStringEntry.getKey(),stringStringEntry.getValue()); 241 | } 242 | }*/ 243 | newSpan.tag("requestId", String.valueOf(request.getRequestId())); 244 | newSpan.tag(Span.SPAN_LOCAL_COMPONENT_TAG_NAME, MOTAN_TAG); 245 | 246 | return newSpan; 247 | /*Span.SpanBuilder span = tracer.buildSpan(operationName); 248 | try { 249 | SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(request.getAttachments())); 250 | if (spanContext != null) { 251 | span.asChildOf(spanContext); 252 | } 253 | } catch (Exception e) { 254 | span.withTag("Error", "extract from request fail, error msg:" + e.getMessage()); 255 | }*/ 256 | /* return span.start(); 257 | 258 | int httpStatus = RequestContext.getCurrentContext().getResponse().getStatus(); 259 | if (httpStatus > 0) { 260 | this.tracer.addTag(this.traceKeys.getHttp().getStatusCode(), 261 | String.valueOf(httpStatus)); 262 | } 263 | this.tracer.close(getCurrentSpan());*/ 264 | } 265 | 266 | } 267 | -------------------------------------------------------------------------------- /filter-opentracing/src/main/resources/META-INF/services/com.weibo.api.motan.filter.Filter: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2009-2016 Weibo, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | com.weibo.api.motan.filter.sleuth.SleuthTracingFilter -------------------------------------------------------------------------------- /goods/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | sleuth-starter 7 | moe.cnkirito 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | goods 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-web 18 | 19 | 20 | org.springframework.cloud 21 | spring-cloud-starter-zipkin 22 | 23 | 24 | com.weibo 25 | motan-core 26 | 27 | 28 | com.weibo 29 | motan-springsupport 30 | 31 | 32 | com.weibo 33 | motan-transport-netty 34 | 35 | 36 | com.weibo 37 | motan-registry-zookeeper 38 | 39 | 40 | com.weibo 41 | serialization-extension 42 | 43 | 44 | com.weibo 45 | filter-opentracing 46 | 47 | 48 | io.opentracing 49 | opentracing-api 50 | 51 | 52 | io.opentracing.brave 53 | brave-opentracing 54 | 55 | 56 | io.opentracing 57 | opentracing-api 58 | 59 | 60 | io.opentracing 61 | opentracing-impl 62 | 63 | 64 | moe.cnkirito 65 | filter-opentracing 66 | 0.0.1-SNAPSHOT 67 | 68 | 69 | moe.cnkirito 70 | interface 71 | 0.0.1-SNAPSHOT 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /goods/src/main/java/moe/cnkirito/sleuth/goods/GoodsApp.java: -------------------------------------------------------------------------------- 1 | package moe.cnkirito.sleuth.goods; 2 | 3 | import com.weibo.api.motan.common.MotanConstants; 4 | import com.weibo.api.motan.util.MotanSwitcherUtil; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | /** 11 | * Created by xujingfeng on 2017/11/7. 12 | */ 13 | @SpringBootApplication 14 | public class GoodsApp { 15 | 16 | @Bean 17 | RestTemplate restTemplate() { 18 | return new RestTemplate(); 19 | } 20 | 21 | public static void main(String[] args) { 22 | SpringApplication.run(GoodsApp.class, args); 23 | MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /goods/src/main/java/moe/cnkirito/sleuth/goods/config/MotanConfig.java: -------------------------------------------------------------------------------- 1 | package moe.cnkirito.sleuth.goods.config; 2 | 3 | import com.weibo.api.motan.config.springsupport.*; 4 | import com.weibo.api.motan.filter.sleuth.SleuthTracerFactory; 5 | import com.weibo.api.motan.filter.sleuth.SleuthTracingContext; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | 12 | /** 13 | * Created by xiaoqian on 2016/9/27. 14 | */ 15 | @Configuration 16 | public class MotanConfig { 17 | 18 | @Bean 19 | public AnnotationBean motanAnnotationBean(@Value("${motan.annotaiong-package}") String annotaiongPackage) { 20 | AnnotationBean motanAnnotationBean = new AnnotationBean(); 21 | motanAnnotationBean.setPackage(annotaiongPackage); 22 | if(motanAnnotationBean.getPackage()==null){ 23 | throw new RuntimeException("请配置maton api包"); 24 | } 25 | return motanAnnotationBean; 26 | } 27 | 28 | @Bean(name = "motanServer") 29 | public ProtocolConfigBean protocolConfig() { 30 | return getProtocolConfigBean(); 31 | } 32 | 33 | @Bean(name = "motanClient") 34 | public ProtocolConfigBean protocolConfigClient() { 35 | return getProtocolConfigBean(); 36 | } 37 | 38 | private ProtocolConfigBean getProtocolConfigBean() { 39 | ProtocolConfigBean config = new ProtocolConfigBean(); 40 | config.setDefault(true); 41 | config.setSerialization("fastjson"); 42 | config.setName("motan"); 43 | config.setMaxContentLength(1048576); 44 | return config; 45 | } 46 | 47 | @Bean(name = "registry") 48 | public RegistryConfigBean registryConfigSit(@Value("${motan.zookeeper-host}") String zookeeperHost) { 49 | RegistryConfigBean config = new RegistryConfigBean(); 50 | config.setDefault(true); 51 | config.setRegProtocol("zookeeper"); 52 | config.setAddress(zookeeperHost); 53 | return config; 54 | } 55 | 56 | /** 57 | * 基础服务端配置 58 | * @return 59 | */ 60 | @Bean(name = "motanServerBasicConfig") 61 | public BasicServiceConfigBean baseServiceConfig(@Value("${motan.export-port}") Integer exportPort, 62 | @Value("${motan.server-group}") String serverGroup, 63 | @Value("${motan.server-access-log}") Boolean serverAccessLog, 64 | @Value("${spring.sleuth.enabled:false}") Boolean tracing 65 | ) { 66 | BasicServiceConfigBean config = new BasicServiceConfigBean(); 67 | config.setDefault(true); 68 | config.setExport("motanServer:"+exportPort); 69 | config.setGroup(serverGroup); 70 | config.setAccessLog(serverAccessLog); 71 | config.setShareChannel(true); 72 | config.setRequestTimeout(60*1000); 73 | if(tracing){ 74 | config.setFilter("sleuth-tracing"); 75 | } 76 | config.setRegistry("registry"); 77 | return config; 78 | } 79 | 80 | 81 | @Bean(name = "motanClientBasicConfig") 82 | public BasicRefererConfigBean baseRefererConfig(@Value("${motan.client-group}") String clientGroup, 83 | @Value("${motan.client-access-log}") Boolean clientAccessLog) { 84 | BasicRefererConfigBean config = new BasicRefererConfigBean(); 85 | config.setProtocol("motanClient"); 86 | config.setGroup(clientGroup); 87 | config.setAccessLog(clientAccessLog); 88 | config.setRegistry("registry"); 89 | config.setCheck(false); 90 | config.setRetries(2); 91 | config.setRequestTimeout(60*1000); 92 | config.setThrowException(true); 93 | config.setDefault(true); 94 | return config; 95 | } 96 | 97 | 98 | @Bean 99 | SleuthTracingContext sleuthTracingContext(@Autowired(required = false) org.springframework.cloud.sleuth.Tracer tracer){ 100 | SleuthTracingContext context = new SleuthTracingContext(); 101 | context.setTracerFactory(new SleuthTracerFactory() { 102 | @Override 103 | public org.springframework.cloud.sleuth.Tracer getTracer() { 104 | return tracer; 105 | } 106 | }); 107 | 108 | return context; 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /goods/src/main/java/moe/cnkirito/sleuth/goods/service/GoodsApiImpl.java: -------------------------------------------------------------------------------- 1 | package moe.cnkirito.sleuth.goods.service; 2 | 3 | import com.weibo.api.motan.config.springsupport.annotation.MotanService; 4 | import moe.cnkirito.sleuth.api.GoodsApi; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | /** 9 | * Created by xujingfeng on 2017/11/7. 10 | */ 11 | @MotanService 12 | public class GoodsApiImpl implements GoodsApi { 13 | 14 | Logger logger = LoggerFactory.getLogger(GoodsApiImpl.class); 15 | 16 | @Override 17 | public String getGoodsList() { 18 | logger.info("GoodsApi invoking ..."); 19 | return "success"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /goods/src/main/java/moe/cnkirito/sleuth/goods/web/GoodsController.java: -------------------------------------------------------------------------------- 1 | package moe.cnkirito.sleuth.goods.web; 2 | 3 | import moe.cnkirito.sleuth.goods.web.model.MainOrder; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.http.ResponseEntity; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | /** 11 | * Created by xujingfeng on 2017/11/7. 12 | */ 13 | @RestController 14 | public class GoodsController { 15 | 16 | @Autowired 17 | RestTemplate restTemplate; 18 | 19 | @RequestMapping("test") 20 | public MainOrder test(){ 21 | ResponseEntity mainOrderResponseEntity = restTemplate.getForEntity("http://localhost:8060/api/order/1144", MainOrder.class); 22 | MainOrder body = mainOrderResponseEntity.getBody(); 23 | return body; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /goods/src/main/java/moe/cnkirito/sleuth/goods/web/model/MainOrder.java: -------------------------------------------------------------------------------- 1 | package moe.cnkirito.sleuth.goods.web.model; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.Date; 5 | 6 | /** 7 | * Created by xujingfeng on 2017/11/7. 8 | */ 9 | public class MainOrder { 10 | 11 | private String orderId; 12 | private BigDecimal totalFare; 13 | private Date createDate; 14 | 15 | public MainOrder() { 16 | } 17 | 18 | public MainOrder(String orderId, BigDecimal totalFare, Date createDate) { 19 | this.orderId = orderId; 20 | this.totalFare = totalFare; 21 | this.createDate = createDate; 22 | } 23 | 24 | public String getOrderId() { 25 | return orderId; 26 | } 27 | 28 | public void setOrderId(String orderId) { 29 | this.orderId = orderId; 30 | } 31 | 32 | public BigDecimal getTotalFare() { 33 | return totalFare; 34 | } 35 | 36 | public void setTotalFare(BigDecimal totalFare) { 37 | this.totalFare = totalFare; 38 | } 39 | 40 | public Date getCreateDate() { 41 | return createDate; 42 | } 43 | 44 | public void setCreateDate(Date createDate) { 45 | this.createDate = createDate; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /goods/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: goods 4 | zipkin: 5 | base-url: http://localhost:9411 6 | sleuth: 7 | enabled: true 8 | sampler: 9 | percentage: 1 10 | server: 11 | port: 8070 12 | 13 | motan: 14 | annotaiong-package: moe.cnkirito.sleuth.goods 15 | client-group: cnkirito 16 | client-access-log: true 17 | server-group: cnkirito 18 | server-access-log: true 19 | export-port: ${random.int[9001,9999]} 20 | zookeeper-host: localhost:2181 -------------------------------------------------------------------------------- /interface/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | sleuth-starter 7 | moe.cnkirito 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | interface 13 | 14 | 15 | -------------------------------------------------------------------------------- /interface/src/main/java/moe/cnkirito/sleuth/api/GoodsApi.java: -------------------------------------------------------------------------------- 1 | package moe.cnkirito.sleuth.api; 2 | 3 | /** 4 | * Created by xujingfeng on 2017/11/7. 5 | */ 6 | public interface GoodsApi { 7 | 8 | String getGoodsList(); 9 | 10 | } 11 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Migwn, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 204 | echo $MAVEN_PROJECTBASEDIR 205 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 206 | 207 | # For Cygwin, switch paths to Windows format before running java 208 | if $cygwin; then 209 | [ -n "$M2_HOME" ] && 210 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 211 | [ -n "$JAVA_HOME" ] && 212 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 213 | [ -n "$CLASSPATH" ] && 214 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 215 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 216 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 217 | fi 218 | 219 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 220 | 221 | exec "$JAVACMD" \ 222 | $MAVEN_OPTS \ 223 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 224 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 225 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 226 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 84 | @REM Fallback to current working directory if not found. 85 | 86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 88 | 89 | set EXEC_DIR=%CD% 90 | set WDIR=%EXEC_DIR% 91 | :findBaseDir 92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 93 | cd .. 94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 95 | set WDIR=%CD% 96 | goto findBaseDir 97 | 98 | :baseDirFound 99 | set MAVEN_PROJECTBASEDIR=%WDIR% 100 | cd "%EXEC_DIR%" 101 | goto endDetectBaseDir 102 | 103 | :baseDirNotFound 104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 105 | cd "%EXEC_DIR%" 106 | 107 | :endDetectBaseDir 108 | 109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 110 | 111 | @setlocal EnableExtensions EnableDelayedExpansion 112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 114 | 115 | :endReadAdditionalConfig 116 | 117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 118 | 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 121 | 122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 123 | if ERRORLEVEL 1 goto error 124 | goto end 125 | 126 | :error 127 | set ERROR_CODE=1 128 | 129 | :end 130 | @endlocal & set ERROR_CODE=%ERROR_CODE% 131 | 132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 136 | :skipRcPost 137 | 138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 140 | 141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 142 | 143 | exit /B %ERROR_CODE% 144 | -------------------------------------------------------------------------------- /order/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | sleuth-starter 7 | moe.cnkirito 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | order 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-web 18 | 19 | 20 | org.springframework.cloud 21 | spring-cloud-starter-zipkin 22 | 23 | 24 | com.weibo 25 | motan-core 26 | 27 | 28 | com.weibo 29 | motan-springsupport 30 | 31 | 32 | com.weibo 33 | motan-transport-netty 34 | 35 | 36 | com.weibo 37 | motan-registry-zookeeper 38 | 39 | 40 | com.weibo 41 | serialization-extension 42 | 43 | 44 | com.weibo 45 | filter-opentracing 46 | 47 | 48 | io.opentracing 49 | opentracing-api 50 | 51 | 52 | io.opentracing.brave 53 | brave-opentracing 54 | 55 | 56 | io.opentracing 57 | opentracing-api 58 | 59 | 60 | io.opentracing 61 | opentracing-impl 62 | 63 | 64 | moe.cnkirito 65 | filter-opentracing 66 | 0.0.1-SNAPSHOT 67 | 68 | 69 | moe.cnkirito 70 | interface 71 | 0.0.1-SNAPSHOT 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /order/src/main/java/moe/cnkirito/sleuth/order/OrderApp.java: -------------------------------------------------------------------------------- 1 | package moe.cnkirito.sleuth.order; 2 | 3 | import com.weibo.api.motan.common.MotanConstants; 4 | import com.weibo.api.motan.util.MotanSwitcherUtil; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | 8 | /** 9 | * Created by xujingfeng on 2017/11/7. 10 | */ 11 | @SpringBootApplication 12 | public class OrderApp { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(OrderApp.class, args); 16 | MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /order/src/main/java/moe/cnkirito/sleuth/order/config/MotanConfig.java: -------------------------------------------------------------------------------- 1 | package moe.cnkirito.sleuth.order.config; 2 | 3 | import com.weibo.api.motan.config.springsupport.*; 4 | import com.weibo.api.motan.filter.sleuth.SleuthTracerFactory; 5 | import com.weibo.api.motan.filter.sleuth.SleuthTracingContext; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | 12 | /** 13 | * Created by xiaoqian on 2016/9/27. 14 | */ 15 | @Configuration 16 | public class MotanConfig { 17 | 18 | @Bean 19 | public AnnotationBean motanAnnotationBean(@Value("${motan.annotaiong-package}") String annotaiongPackage) { 20 | AnnotationBean motanAnnotationBean = new AnnotationBean(); 21 | motanAnnotationBean.setPackage(annotaiongPackage); 22 | if(motanAnnotationBean.getPackage()==null){ 23 | throw new RuntimeException("请配置maton api包"); 24 | } 25 | return motanAnnotationBean; 26 | } 27 | 28 | @Bean(name = "motanServer") 29 | public ProtocolConfigBean protocolConfig() { 30 | return getProtocolConfigBean(); 31 | } 32 | 33 | @Bean(name = "motanClient") 34 | public ProtocolConfigBean protocolConfigClient() { 35 | return getProtocolConfigBean(); 36 | } 37 | 38 | private ProtocolConfigBean getProtocolConfigBean() { 39 | ProtocolConfigBean config = new ProtocolConfigBean(); 40 | config.setDefault(true); 41 | config.setSerialization("fastjson"); 42 | config.setName("motan"); 43 | config.setMaxContentLength(1048576); 44 | // config.setSerialization("fastjson"); 45 | return config; 46 | } 47 | 48 | @Bean(name = "registry") 49 | public RegistryConfigBean registryConfigSit(@Value("${motan.zookeeper-host}") String zookeeperHost) { 50 | RegistryConfigBean config = new RegistryConfigBean(); 51 | config.setDefault(true); 52 | config.setRegProtocol("zookeeper"); 53 | config.setAddress(zookeeperHost); 54 | return config; 55 | } 56 | 57 | /** 58 | * 基础服务端配置 59 | * @return 60 | */ 61 | @Bean(name = "motanServerBasicConfig") 62 | public BasicServiceConfigBean baseServiceConfig(@Value("${motan.export-port}") Integer exportPort, 63 | @Value("${motan.server-group}") String serverGroup, 64 | @Value("${motan.server-access-log}") Boolean serverAccessLog, 65 | @Value("${spring.sleuth.enabled:false}") Boolean tracing 66 | ) { 67 | BasicServiceConfigBean config = new BasicServiceConfigBean(); 68 | config.setDefault(true); 69 | config.setExport("motanServer:"+exportPort); 70 | config.setGroup(serverGroup); 71 | config.setAccessLog(serverAccessLog); 72 | config.setShareChannel(true); 73 | config.setRequestTimeout(60*1000); 74 | if(tracing){ 75 | config.setFilter("sleuth-tracing"); 76 | } 77 | config.setRegistry("registry"); 78 | return config; 79 | } 80 | 81 | 82 | @Bean(name = "motanClientBasicConfig") 83 | public BasicRefererConfigBean baseRefererConfig(@Value("${motan.client-group}") String clientGroup, 84 | @Value("${motan.client-access-log}") Boolean clientAccessLog) { 85 | BasicRefererConfigBean config = new BasicRefererConfigBean(); 86 | config.setProtocol("motanClient"); 87 | config.setGroup(clientGroup); 88 | config.setAccessLog(clientAccessLog); 89 | config.setRegistry("registry"); 90 | config.setCheck(false); 91 | config.setRetries(2); 92 | config.setRequestTimeout(60*1000); 93 | config.setThrowException(true); 94 | config.setDefault(true); 95 | return config; 96 | } 97 | 98 | 99 | @Bean 100 | SleuthTracingContext sleuthTracingContext(@Autowired(required = false) org.springframework.cloud.sleuth.Tracer tracer){ 101 | SleuthTracingContext context = new SleuthTracingContext(); 102 | context.setTracerFactory(new SleuthTracerFactory() { 103 | @Override 104 | public org.springframework.cloud.sleuth.Tracer getTracer() { 105 | return tracer; 106 | } 107 | }); 108 | 109 | return context; 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /order/src/main/java/moe/cnkirito/sleuth/order/model/MainOrder.java: -------------------------------------------------------------------------------- 1 | package moe.cnkirito.sleuth.order.model; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.Date; 5 | 6 | /** 7 | * Created by xujingfeng on 2017/11/7. 8 | */ 9 | public class MainOrder { 10 | 11 | private String orderId; 12 | private BigDecimal totalFare; 13 | private Date createDate; 14 | 15 | public MainOrder() { 16 | } 17 | 18 | public MainOrder(String orderId, BigDecimal totalFare, Date createDate) { 19 | this.orderId = orderId; 20 | this.totalFare = totalFare; 21 | this.createDate = createDate; 22 | } 23 | 24 | public String getOrderId() { 25 | return orderId; 26 | } 27 | 28 | public void setOrderId(String orderId) { 29 | this.orderId = orderId; 30 | } 31 | 32 | public BigDecimal getTotalFare() { 33 | return totalFare; 34 | } 35 | 36 | public void setTotalFare(BigDecimal totalFare) { 37 | this.totalFare = totalFare; 38 | } 39 | 40 | public Date getCreateDate() { 41 | return createDate; 42 | } 43 | 44 | public void setCreateDate(Date createDate) { 45 | this.createDate = createDate; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /order/src/main/java/moe/cnkirito/sleuth/order/web/OrderController.java: -------------------------------------------------------------------------------- 1 | package moe.cnkirito.sleuth.order.web; 2 | 3 | import com.weibo.api.motan.config.springsupport.annotation.MotanReferer; 4 | import moe.cnkirito.sleuth.api.GoodsApi; 5 | import moe.cnkirito.sleuth.order.model.MainOrder; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.math.BigDecimal; 13 | import java.util.Date; 14 | 15 | /** 16 | * Created by xujingfeng on 2017/11/7. 17 | */ 18 | @RestController 19 | @RequestMapping("/api") 20 | public class OrderController { 21 | 22 | Logger logger = LoggerFactory.getLogger(OrderController.class); 23 | 24 | @RequestMapping("/order/{id}") 25 | public MainOrder getOrder(@PathVariable("id") String id) { 26 | logger.info("order invoking ..."); 27 | return new MainOrder(id, new BigDecimal(200D), new Date()); 28 | } 29 | 30 | @MotanReferer 31 | GoodsApi goodsApi; 32 | 33 | @RequestMapping("/goods") 34 | public String getGoodsList() { 35 | logger.info("getGoodsList invoking ..."); 36 | return goodsApi.getGoodsList(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /order/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: order 4 | zipkin: 5 | base-url: http://localhost:9411 6 | sleuth: 7 | enabled: true 8 | sampler: 9 | percentage: 1 10 | server: 11 | port: 8060 12 | 13 | 14 | motan: 15 | annotaiong-package: moe.cnkirito.sleuth.order 16 | client-group: cnkirito 17 | client-access-log: true 18 | server-group: cnkirito 19 | server-access-log: true 20 | export-port: ${random.int[9001,9999]} 21 | zookeeper-host: localhost:2181 -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | moe.cnkirito 7 | sleuth-starter 8 | 0.0.1-SNAPSHOT 9 | 10 | zipkin-server 11 | goods 12 | order 13 | interface 14 | filter-opentracing 15 | 16 | pom 17 | 18 | sleuth-starter 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-parent 23 | 1.5.8.RELEASE 24 | 25 | 26 | 27 | 28 | UTF-8 29 | UTF-8 30 | 1.8 31 | Dalston.SR4 32 | 33 | 34 | 35 | 36 | 37 | com.weibo 38 | motan-core 39 | 0.3.0 40 | 41 | 42 | org.slf4j 43 | slf4j-log4j12 44 | 45 | 46 | 47 | 48 | com.weibo 49 | motan-springsupport 50 | 0.3.0 51 | 52 | 53 | org.slf4j 54 | slf4j-log4j12 55 | 56 | 57 | 58 | 59 | com.weibo 60 | motan-transport-netty 61 | 0.3.0 62 | 63 | 64 | org.slf4j 65 | slf4j-log4j12 66 | 67 | 68 | 69 | 70 | com.weibo 71 | motan-registry-zookeeper 72 | 0.3.0 73 | 74 | 75 | org.slf4j 76 | slf4j-log4j12 77 | 78 | 79 | 80 | 81 | com.weibo 82 | motan-extension 83 | 0.3.0 84 | 85 | 86 | org.slf4j 87 | slf4j-log4j12 88 | 89 | 90 | 91 | 92 | com.weibo 93 | serialization-extension 94 | 0.3.0 95 | 96 | 97 | org.slf4j 98 | slf4j-log4j12 99 | 100 | 101 | 102 | 103 | com.weibo 104 | filter-extension 105 | 0.3.0 106 | 107 | 108 | org.slf4j 109 | slf4j-log4j12 110 | 111 | 112 | 113 | 114 | com.weibo 115 | filter-opentracing 116 | 0.3.0 117 | 118 | 119 | org.slf4j 120 | slf4j-log4j12 121 | 122 | 123 | 124 | 125 | io.opentracing 126 | opentracing-api 127 | 0.21.0 128 | 129 | 130 | io.opentracing 131 | opentracing-impl 132 | 0.21.0 133 | 134 | 135 | io.opentracing.brave 136 | brave-opentracing 137 | 0.20.0 138 | 139 | 140 | io.zipkin.brave 141 | brave 142 | 4.2.0 143 | 144 | 145 | io.zipkin.brave 146 | brave-http 147 | 4.2.0 148 | 149 | 150 | io.zipkin.brave 151 | brave-okhttp 152 | 4.2.0 153 | 154 | 155 | io.zipkin.reporter 156 | zipkin-sender-okhttp3 157 | 0.7.1 158 | 159 | 160 | org.springframework.cloud 161 | spring-cloud-dependencies 162 | ${spring-cloud.version} 163 | pom 164 | import 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | ali 173 | http://maven.aliyun.com/nexus/content/groups/public/ 174 | 175 | true 176 | always 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | org.springframework.boot 185 | spring-boot-maven-plugin 186 | 187 | 188 | 189 | 190 | 191 | 192 | -------------------------------------------------------------------------------- /zipkin-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | sleuth-starter 7 | moe.cnkirito 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | zipkin-server 13 | 14 | 15 | 16 | io.zipkin.java 17 | zipkin-server 18 | 19 | 20 | io.zipkin.java 21 | zipkin-autoconfigure-ui 22 | 23 | 24 | mysql 25 | mysql-connector-java 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-jdbc 30 | 31 | 32 | io.zipkin.java 33 | zipkin-storage-mysql 34 | 1.28.0 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /zipkin-server/src/main/java/moe/cnkirito/sleuth/ZipkinServerApp.java: -------------------------------------------------------------------------------- 1 | package moe.cnkirito.sleuth; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.Bean; 6 | import zipkin.server.EnableZipkinServer; 7 | import zipkin.storage.mysql.MySQLStorage; 8 | 9 | import javax.sql.DataSource; 10 | 11 | /** 12 | * Created by xujingfeng on 2017/11/7. 13 | */ 14 | @SpringBootApplication 15 | @EnableZipkinServer 16 | public class ZipkinServerApp { 17 | 18 | @Bean 19 | public MySQLStorage mySQLStorage(DataSource datasource) { 20 | return MySQLStorage.builder().datasource(datasource).executor(Runnable::run).build(); 21 | } 22 | 23 | public static void main(String[] args) { 24 | SpringApplication.run(ZipkinServerApp.class, args); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /zipkin-server/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: zipkin-server 4 | datasource: 5 | url: jdbc:mysql://localhost:3306/zipkin 6 | username: root 7 | password: root 8 | driver-class-name: com.mysql.jdbc.Driver 9 | zipkin: 10 | storage: 11 | type: mysql 12 | server: 13 | port: 9411 14 | -------------------------------------------------------------------------------- /zipkin-server/src/main/resources/mysql.sql: -------------------------------------------------------------------------------- 1 | create table if not exists zipkin_spans ( 2 | `trace_id_high` bigint not null default 0 comment 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit', 3 | `trace_id` bigint not null, 4 | `id` bigint not null, 5 | `name` varchar(255) not null, 6 | `parent_id` bigint, 7 | `debug` bit(1), 8 | `start_ts` bigint comment 'Span.timestamp(): epoch micros used for endTs query and to implement TTL', 9 | `duration` bigint comment 'Span.duration(): micros used for minDuration and maxDuration query' 10 | ) engine=innodb row_format=compressed character set=utf8 collate utf8_general_ci; 11 | 12 | alter table zipkin_spans add unique key(`trace_id_high`, `trace_id`, `id`) comment 'ignore insert on duplicate'; 13 | alter table zipkin_spans add index(`trace_id_high`, `trace_id`, `id`) comment 'for joining with zipkin_annotations'; 14 | alter table zipkin_spans add index(`trace_id_high`, `trace_id`) comment 'for getTracesByIds'; 15 | alter table zipkin_spans add index(`name`) comment 'for getTraces and getSpanNames'; 16 | alter table zipkin_spans add index(`start_ts`) comment 'for getTraces ordering and range'; 17 | 18 | create table if not exists zipkin_annotations ( 19 | `trace_id_high` bigint not null default 0 comment 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit', 20 | `trace_id` bigint not null comment 'coincides with zipkin_spans.trace_id', 21 | `span_id` bigint not null comment 'coincides with zipkin_spans.id', 22 | `a_key` varchar(255) not null comment 'BinaryAnnotation.key or Annotation.value if type == -1', 23 | `a_value` blob comment 'BinaryAnnotation.value(), which must be smaller than 64KB', 24 | `a_type` int not null comment 'BinaryAnnotation.type() or -1 if Annotation', 25 | `a_timestamp` bigint comment 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp', 26 | `endpoint_ipv4` int comment 'Null when Binary/Annotation.endpoint is null', 27 | `endpoint_ipv6` binary(16) comment 'Null when Binary/Annotation.endpoint is null, or no IPv6 address', 28 | `endpoint_port` smallint comment 'Null when Binary/Annotation.endpoint is null', 29 | `endpoint_service_name` varchar(255) comment 'Null when Binary/Annotation.endpoint is null' 30 | ) engine=innodb row_format=compressed character set=utf8 collate utf8_general_ci; 31 | 32 | alter table zipkin_annotations add unique key(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) comment 'Ignore insert on duplicate'; 33 | alter table zipkin_annotations add index(`trace_id_high`, `trace_id`, `span_id`) comment 'for joining with zipkin_spans'; 34 | alter table zipkin_annotations add index(`trace_id_high`, `trace_id`) comment 'for getTraces/ByIds'; 35 | alter table zipkin_annotations add index(`endpoint_service_name`) comment 'for getTraces and getServiceNames'; 36 | alter table zipkin_annotations add index(`a_type`) comment 'for getTraces'; 37 | alter table zipkin_annotations add index(`a_key`) comment 'for getTraces'; 38 | alter table zipkin_annotations add index(`trace_id`, `span_id`, `a_key`) comment 'for dependencies job'; 39 | 40 | create table if not exists zipkin_dependencies ( 41 | `day` date not null, 42 | `parent` varchar(255) not null, 43 | `child` varchar(255) not null, 44 | `call_count` bigint, 45 | `error_count` bigint 46 | ) engine=innodb row_format=compressed character set=utf8 collate utf8_general_ci; 47 | 48 | alter table zipkin_dependencies add unique key(`day`, `parent`, `child`); --------------------------------------------------------------------------------