本文共 3555 字,大约阅读时间需要 11 分钟。
Spring Cloud Sleuth集成了zipkin组件
从spring Cloud为F版本开始,已经不需要自己构建Zipkin Server了,只需要下载jar运行即可。
命令:java -jar zipkin-server-2.12.9-exec.jar
D:\Users\chengyq>c:C:\>java -jar zipkin-server-2.12.9-exec.jar
http://localhost:9411
4.0.0 com.cntaiping.tpa server-x 0.0.1-SNAPSHOT jar server-x Demo project for Spring Boot com.cntaiping.tpa cloud 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-zipkin
server.port=7201spring.zipkin.base-url=http://localhost:9411spring.application.name=server-x
package com.cntaiping.tpa.serverx;import brave.sampler.Sampler;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplicationpublic class ServerXApplication { public static void main(String[] args) { SpringApplication.run(ServerXApplication.class, args); } @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; }}
package com.cntaiping.tpa.serverx.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import java.util.logging.Level;@RestControllerpublic class XController { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private RestTemplate restTemplate; @RequestMapping("/hello") public String hello(){ logger.info("calling trace server-x "); return restTemplate.getForObject("http://localhost:7202/info", String.class); } @RequestMapping("/info") public String info(){ logger.info("calling trace server-x "); return "i'm server-x"; }}
构建server-y与构建构建server-x过程相同,不同的项目文件如下
server.port=7202spring.zipkin.base-url=http://localhost:9411spring.application.name=server-y
package com.cntaiping.tpa.servery.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class YController { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private RestTemplate restTemplate; @RequestMapping("/hello") public String hello(){ logger.info("calling trace server-y "); return restTemplate.getForObject("http://localhost:7201/info", String.class); } @RequestMapping("/info") public String info(){ logger.info("calling trace server-y "); return "i'm server-y"; }}
转载地址:http://dwtab.baihongyu.com/