必需学会SpringBoot基础知识html
spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,能够在开发人员的电脑上跑。java
JDK8git
apache-maven-3.5.2github
IntelliJ IDEA 2018.1 x64web
Add sleuth to the classpath of a Spring Boot application (see below for Maven and Gradle examples), and you will see the correlation data being collected in logs, as long as you are logging requests.spring
—— 摘自官网apache
Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,而且兼容支持了 zipkin,你只须要在pom文件中引入相应的依赖便可。浏览器
微服务架构上经过业务来划分服务的,经过REST调用,对外暴露的一个接口,可能须要不少个服务协同才能完成这个接口功能,若是链路上任何一个服务出现问题或者网络超时,都会造成致使接口调用失败。随着业务的不断扩张,服务之间互相调用会愈来愈复杂。网络
随着服务的愈来愈多,对调用链的分析会愈来愈复杂。它们之间的调用关系也许以下:架构
将Span和Trace在一个系统中使用Zipkin注解的过程图形化:
基本知识讲解完毕,下面咱们来实战,本文的案例主要有三个工程组成:一个eureka-server-zipkin,它的主要做用使用ZipkinServer 的功能,收集调用数据,并展现;一个eureka-server-zipkin-client,对外暴露hi接口;一个eureka-server-zipkin-eddie,对外暴露eddie接口;这两个service能够相互调用;而且只有调用了,eureka-server-zipkin才会收集数据的,这就是为何叫服务追踪了。
pom引入依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在其程序入口类, 加上注解@EnableZipkinServer,开启ZipkinServer的功能:
package com.lwc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import zipkin.server.EnableZipkinServer; /** * @author Eddie */ @EnableZipkinServer @SpringBootApplication public class EurekaServerZipkinApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerZipkinApplication.class, args); } }
在配置文件application.yml指定服务端口为:
server:
port: 9411
在其pom引入起步依赖spring-cloud-starter-zipkin,代码以下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在其配置文件application.yml指定zipkin server的地址,头经过配置“spring.zipkin.base-url”指定:
server: port: 8988 spring: zipkin: base-url: http://localhost:9411 application: name: eureka-server-zipkin-client
经过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url就能够了。
对外暴露接口:
package com.lwc.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * @author eddie.lee * @Package com.lwc.controller * @ClassName ZipkinController * @description * @date created in 2018-04-09 13:55 * @modified by */ @Slf4j @RestController public class ZipkinController { @Autowired private RestTemplate restTemplate; @GetMapping("/hi") public String callHome() { log.info(" callHome() ==> calling trace eureka-server-zipkin-client"); return restTemplate.getForObject("http://localhost:8989/eddie", String.class); } @GetMapping("/info2") public String info2() { log.info(" info2() ==> calling trace eureka-server-zipkin-client"); return "this is eureka-server-zipkin-client"; } }
建立过程 eureka-server-zipkin-client,引入相同的依赖,配置下spring.zipkin.base-url。
对外暴露接口:
package com.lwc.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * @author eddie.lee * @Package com.lwc.controller * @ClassName EddieController * @description * @date created in 2018-04-09 15:56 * @modified by */ @Slf4j @RestController public class EddieController { @Autowired private RestTemplate restTemplate; @GetMapping("/hi") public String home(){ log.info("==> hi is being called"); return "hi i'm eddie.lee!"; } @GetMapping("/eddie") public String info(){ log.info("==> info is being called"); return restTemplate.getForObject("http://localhost:8988/info2",String.class); } }
依次启动上面的三个工程,打开浏览器访问:http://localhost:9411/,会出现如下界面:
访问:http://localhost:8989/eddie,浏览器出现:
this is eureka-server-zipkin-client
再打开http://localhost:9411/的界面,点击 依赖分析 ,能够发现服务的依赖关系:
点击 查找 能够看到具体服务相互调用的数据:
PS: 在网上不少出现依赖找不到, 或者是WEB UI查看不到依赖分析等状况, 请按本文章对应代码参考, 欢迎评论;
标签 9-1
https://github.com/eddie-code/SpringCloudDemo