javascript
Spring Cloud sleuth with zipkin over RabbitMQ教程
文章目錄
- Spring Cloud sleuth with zipkin over RabbitMQ demo
- zipkin server的搭建(基于mysql和rabbitMQ)
- 客戶端環境的依賴
- 如何調用
Spring Cloud sleuth with zipkin over RabbitMQ demo
本項目是sleuth和zipkin在spring cloud環境中使用,其中sleuth和zipkin是通過RabbitMQ進行通信,同時zipkin的數據是存儲在mysql中。
Spring Cloud的版本是目前最新的Greenwich.SR2版本,對應的Spring boot是2.1.8.RELEASE。
本教程要解決的問題:
zipkin server的搭建(基于mysql和rabbitMQ)
最新的zipkin官網建議使用zipkin提供的官方包來啟動zipkin server。 步驟如下:
下載最新的zipkin server jar包:
curl -sSL https://zipkin.io/quickstart.sh | bash -s
配置環境變量,并啟動zipkin server,見startServer.sh:
請將rabbit mq 和 mysql 的配置修改成你對應的環境變量。
官方腳本地址
這里我也列出來了:
-- -- Copyright 2015-2019 The OpenZipkin Authors -- -- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except -- in compliance with the License. You may obtain a copy of the License at -- -- http://www.apache.org/licenses/LICENSE-2.0 -- -- Unless required by applicable law or agreed to in writing, software distributed under the License -- is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -- or implied. See the License for the specific language governing permissions and limitations under -- the License. --CREATE TABLE IF NOT EXISTS zipkin_spans (`trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',`trace_id` BIGINT NOT NULL,`id` BIGINT NOT NULL,`name` VARCHAR(255) NOT NULL,`remote_service_name` VARCHAR(255),`parent_id` BIGINT,`debug` BIT(1),`start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',`duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',PRIMARY KEY (`trace_id_high`, `trace_id`, `id`) ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds'; ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames'; ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames'; ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';CREATE TABLE IF NOT EXISTS zipkin_annotations (`trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',`trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',`span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',`a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',`a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',`a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',`a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',`endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',`endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',`endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',`endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null' ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate'; ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans'; ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds'; ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames'; ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values'; ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values'; ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';CREATE TABLE IF NOT EXISTS zipkin_dependencies (`day` DATE NOT NULL,`parent` VARCHAR(255) NOT NULL,`child` VARCHAR(255) NOT NULL,`call_count` BIGINT,`error_count` BIGINT,PRIMARY KEY (`day`, `parent`, `child`) ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;在正式環境中,官方推薦的使用Elastricsearch做數據存儲,因為zipkin收集的數據會比較多,使用mysql可能會有性能問題。后面有機會我們再講怎么用Elastricsearch作數據存儲。
客戶端環境的依賴
如果想要在客戶端使用sleuth+ rabbitMQ,需要如下配置:
<dependencyManagement> <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${release.train.version}</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement><dependency> <groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId> </dependency>本實例中我們使用了eureka, 其實它不是必須的。大家在實際使用中可以自己取舍。
我們看一下zipkin客戶端的配置文件:
spring:application:name: service2rabbitmq:host: localhostport: 5672username: guestpassword: guestvirtual-host: zipkinzipkin:sender:type: rabbitrabbitmq:queue: zipkinsleuth:sampler:probability: 1.0spring.application.name 很好理解,就是應用程序的名字,會被默認作為zipkin服務的名字。
我們使用rabbitMQ ,所以需要spring.rabbitmq的配置信息。
spring.zipkin.sender.type=rabbit 表示我們需要使用rabbit MQ來收集信息。當然你也可以設置成為web或者kafka。
這里spring.zipkin.rabbitmq.queue=zipkin表示使用MQ時候的queue名字,默認是zipkin。
spring.sleuth.sampler.probability=1.0 這個是采樣信息,1.0表示是100%采集。如果要在線上使用,可以自定義這個百分比。
如何調用
最后我們看下如何調用。
在service2中,我們定義了如下的方法:
@RestController @RequestMapping("/serviceTwo") public class ServiceTwoController {@GetMapping("callServiceTwo")public String callServiceOne(){log.info("service two is called!");return "service two is called!";} }我們在service1中用restTemplet來調用它:
@RestController @RequestMapping("/serviceOne") public class ServiceOneController {@GetMapping("callServiceOne")public String callServiceOne(){log.info("service one is called!");restTemplate().getForObject("http://localhost:9000/serviceTwo/callServiceTwo",String.class);return "service one and two are called!";}@BeanRestTemplate restTemplate() {return new RestTemplate();} }這樣,我們用get 去請求http://loalhost/serviceOne/callServiceOne 就會將調用信息發送到MQ,并被zipkin Server 處理。 我們就可以在zipkin web頁面看到調用信息啦 。
have fun !
更多精彩內容且看:
- 區塊鏈從入門到放棄系列教程-涵蓋密碼學,超級賬本,以太坊,Libra,比特幣等持續更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續更新
- Spring 5.X系列教程:滿足你對Spring5的一切想象-持續更新
- java程序員從小工到專家成神之路(2020版)-持續更新中,附詳細文章教程
更多教程請參考 flydean的博客
總結
以上是生活随笔為你收集整理的Spring Cloud sleuth with zipkin over RabbitMQ教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cloud OpenFei
- 下一篇: Java函数式编程和Lambda表达式