标签:键值 很多 帮助 过程 访问 ima 开始 通过 地址
微服务架构上通过业务来划分服务的,通过REST调用,对外暴露的一个接口,可能需要很多个服务协同才能完成这个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会导致接口调用失败。随着业务的不断扩张,服务之间互相调用会越来越复杂。随着服务的越来越多,对调用链的分析会越来越复杂。
Spring Cloud Sleuth服务链路跟踪功能就可以帮助我们快速的发现错误根源以及监控分析每条请求链路上的性能等等。
相关术语:
如果一个服务的调用关系如下:
那么此时将Span和Trace在一个系统中使用Zipkin注解的过程图形化:
本文的案例主要有三个工程组成:一个server-zipkin,它的主要作用使用ZipkinServer 的功能,收集调用数据,并展示;一个service-hi,对外暴露hi接口;一个service-miya,对外暴露miya接口;这两个service可以相互调用;并且只有调用了,server-zipkin才会收集数据的,这就是为什么叫服务追踪了。
在spring Cloud为F版本的时候,已不需要自己构建Zipkin Server了,只需要下载jar即可,下载地址:https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/ 。
下载完毕,执行命令运行
java -jar zipkin-server-2.12.9-exec.jar
访问:localhost:9411
通过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url即可,步骤如下:
在其pom引入起步依赖spring-cloud-starter-zipkin:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
在其配置文件application.yml指定zipkin server的地址,头通过配置“spring.zipkin.base-url”指定:
server.port=8988
spring.application.name=service-hi
spring.zipkin.base-url=http://localhost:9411
对外暴露接口:
import brave.sampler.Sampler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; 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; import java.util.logging.Logger; @SpringBootApplication @RestController public class ServiceHiApplication { public static void main(String[] args) { SpringApplication.run(ServiceHiApplication.class, args); } private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName()); @Autowired private RestTemplate restTemplate; @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } @RequestMapping("/hi") public String callHome(){ LOG.log(Level.INFO, "calling trace service-hi "); return restTemplate.getForObject("http://localhost:8989/miya", String.class); } @RequestMapping("/info") public String info(){ LOG.log(Level.INFO, "calling trace service-hi "); return "i‘m service-hi"; } @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
创建过程痛service-hi,引入相同的依赖,配置下spring.zipkin.base-url。
对外暴露接口:
import brave.sampler.Sampler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; 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; import java.util.logging.Logger; @SpringBootApplication @RestController public class ServiceMiyaApplication { public static void main(String[] args) { SpringApplication.run(ServiceMiyaApplication.class, args); } private static final Logger LOG = Logger.getLogger(ServiceMiyaApplication.class.getName()); @RequestMapping("/hi") public String home(){ LOG.log(Level.INFO, "hi is being called"); return "hi i‘m miya!"; } @RequestMapping("/miya") public String info(){ LOG.log(Level.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,浏览器出现:
此时访问http://localhost:9411/的界面,点击依赖,可以发现服务的依赖关系:
查看服务详情,可以看到具体服务相互调用的数据。
(六)Spring Cloud教程——Sleuth(F版本)
标签:键值 很多 帮助 过程 访问 ima 开始 通过 地址
原文地址:https://www.cnblogs.com/zjfjava/p/12251691.html