日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

發(fā)布時(shí)間:2024/9/27 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這篇文章主要講述服務(wù)追蹤組件zipkin,Spring Cloud Sleuth集成了zipkin組件。

一、簡介

Spring Cloud Sleuth 主要功能就是在分布式系統(tǒng)中提供追蹤解決方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相應(yīng)的依賴即可。

二、服務(wù)追蹤分析

微服務(wù)架構(gòu)上通過業(yè)務(wù)來劃分服務(wù)的,通過REST調(diào)用,對(duì)外暴露的一個(gè)接口,可能需要很多個(gè)服務(wù)協(xié)同才能完成這個(gè)接口功能,如果鏈路上任何一個(gè)服務(wù)出現(xiàn)問題或者網(wǎng)絡(luò)超時(shí),都會(huì)形成導(dǎo)致接口調(diào)用失敗。隨著業(yè)務(wù)的不斷擴(kuò)張,服務(wù)之間互相調(diào)用會(huì)越來越復(fù)雜。

三、構(gòu)建工程

基本知識(shí)講解完畢,下面我們來實(shí)戰(zhàn),本文的案例主要有三個(gè)工程組成:一個(gè)server-zipkin,它的主要作用使用ZipkinServer 的功能,收集調(diào)用數(shù)據(jù),并展示;一個(gè)service-hi,對(duì)外暴露hi接口;一個(gè)service-miya,對(duì)外暴露miya接口;這兩個(gè)service可以相互調(diào)用;并且只有調(diào)用了,server-zipkin才會(huì)收集數(shù)據(jù)的,這就是為什么叫服務(wù)追蹤了。

3.1 構(gòu)建server-zipkin
在spring Cloud為F版本的時(shí)候,已經(jīng)不需要自己構(gòu)建Zipkin Server了,只需要下載jar即可,下載地址:

https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

也可以在這里下載:

鏈接: https://pan.baidu.com/s/1w614Z8gJXHtqLUB6dKWOpQ 密碼: 26pf

下載完成jar 包之后,需要運(yùn)行jar,如下:

java -jar zipkin-server-2.10.1-exec.jar

訪問瀏覽器localhost:9494

3.2 創(chuàng)建service-zipkin
在其pom引入起步依賴spring-cloud-starter-zipkin,代碼如下:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.gblfy</groupId><artifactId>sc-f-chapter9</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.gblfy</groupId><artifactId>service-zipkin</artifactId><version>0.0.1-SNAPSHOT</version><name>service-zipkin</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></dependency></dependencies> </project>

在其配置文件application.yml指定zipkin server的地址,頭通過配置“spring.zipkin.base-url”指定:

server:port: 8988 spring:application:name: service-zipkinzipkin:base-url: http://localhost:9411/sender:type: websleuth:sampler:probability: 1 eureka:client:service-url:defaultZone: http://localhost:8761/eureka

通過引入spring-cloud-starter-zipkin依賴和設(shè)置spring.zipkin.base-url就可以了
啟動(dòng)類作以下修改:

@SpringBootApplication @EnableDiscoveryClient @EnableEurekaClient public class ServiceZipkinApplication {public static void main(String[] args) {SpringApplication.run(ServiceZipkinApplication.class, args);}@Beanpublic RestTemplate getRestTemplate(){return new RestTemplate();}@Beanpublic Sampler defaultSampler() {return Sampler.ALWAYS_SAMPLE;} }

對(duì)外暴露接口:

@RestController public class ClientController {private static final Logger LOG = Logger.getLogger(ClientController.class.getName());@Autowiredprivate RestTemplate 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";} }

33 創(chuàng)建service-miya
創(chuàng)建過程痛service-hi,引入相同的依賴,配置下spring.zipkin.base-url。

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.gblfy</groupId><artifactId>sc-f-chapter9</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.gblfy</groupId><artifactId>service-miya</artifactId><version>0.0.1-SNAPSHOT</version><name>service-miya</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></dependency></dependencies></project> server:port: 8989 spring:zipkin:base-url: http://localhost:9411/sender:type: webapplication:name: service-miya eureka:client:service-url:defaultZone: http://localhost:8761/eureka

啟動(dòng)類作以下修改:

@SpringBootApplication @EnableDiscoveryClient @EnableEurekaClient public class ServiceMiyaApplication {public static void main(String[] args) {SpringApplication.run(ServiceMiyaApplication.class, args);}@Beanpublic RestTemplate getRestTemplate(){return new RestTemplate();}@Beanpublic Sampler defaultSampler() {return Sampler.ALWAYS_SAMPLE;} }

對(duì)外暴露接口:

@RestController public class ClientController {private static final Logger LOG = Logger.getLogger(ClientController.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);}@Autowiredprivate RestTemplate restTemplate; }

3.4 啟動(dòng)工程,演示追蹤
依次啟動(dòng)上面的工程,打開瀏覽器訪問:http://localhost:9411/,會(huì)出現(xiàn)以下界面:

訪問:http://localhost:8989/miya,

瀏覽器出現(xiàn):

i’m service-hi

再打開http://localhost:9411/的界面,點(diǎn)擊Dependencies,可以發(fā)現(xiàn)服務(wù)的依賴關(guān)系:
點(diǎn)擊find traces,可以看到具體服務(wù)相互調(diào)用的數(shù)據(jù):

本文源碼下載:

https://github.com/gb-heima/springcloud-practical-column/tree/master/sc-f-chapter9

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。