本文共 4953 字,大约阅读时间需要 16 分钟。
一、 构建Zipkin Server,官方推荐下载jar即可:
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
java -jar zipkin-server-2.12.9-exec.jar
默认端口是9411 启动完成后访问:http://localhost:9411/
二、创建service-hi
4.0.0 com.xiaobu springcloud-demo 0.0.1-SNAPSHOT service-hi 0.0.1-SNAPSHOT service-hi service-hi project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.cloud spring-cloud-starter-zipkin org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
application.properties
server.port=8988spring.zipkin.base-url=http://localhost:9411spring.application.name=service-hi
通过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url就可以了。
接口暴露:
package com.xiaobu;import brave.sampler.Sampler;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;/** * @author xiaobu */@SpringBootApplication@RestController@Slf4jpublic class ServiceHiApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(ServiceHiApplication.class, args); } @Value("${server.port}") private String port; @Override public void run(String... args) throws Exception { System.out.println("service-hi在端口: " +port+"启动完成....." ); } @Autowired private RestTemplate restTemplate; @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } @GetMapping("/hi") public String callHome(){ log.info("calling trace service-hi "); return restTemplate.getForObject("http://localhost:8989/miya", String.class); } @GetMapping("/info") public String info(){ log.info("calling trace service-hi "); return "i'm service-hi"; } @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; }}
三、 创建service-miya
相同的依赖,修改下server port端口以及服务名即可
接口暴露:
package com.xiaobu;import brave.sampler.Sampler;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@SpringBootApplication@Slf4j@RestControllerpublic class ServiceMiyaApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(ServiceMiyaApplication.class, args); } @Value("${server.port}") private String port; @Override public void run(String... args) throws Exception { log.info("service-miya在端口:{} 启动完成....." ,port); } @RequestMapping("/hi") public String home(){ log.info( "hi is being called"); return "hi i'm miya!"; } @GetMapping("/miya") public String info(){ log.info("info is being called"); return restTemplate.getForObject("http://localhost:8988/info",String.class); } @Autowired private RestTemplate restTemplate; @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; }}
访问http://localhost:8989/miya 出现:
i’m service-hi
点击依赖:
点击查找,查看具体服务相互调用的数据
转载地址:http://ukgai.baihongyu.com/