Spring Cloud学习笔记---Spring Cloud Sleuth--新建两个互相调用的服务测试zipkin
之前已经介绍了如何手动搭建一个zipkin,但是那个是很久之前的东西了,没啥技术含量还浪费时间,所以今天就来一个有意思的,好玩的,其实这个也没啥技术含量。233333
https://zipkin.io/pages/quickstart.html
这个是zipkin的官网,我们可以进行下载它的jar包,直接运行。
启动成功后,zipkin默认是在9411端口的,访问
http://localhost:9411/zipkin/
就可以看到zipkin的追踪界面
接下来,我们写两个互相调用的小的demo。
首先是我们的serviceThree.
其整个demo的目录结构如下
pom文件如下。
<?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><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>serviceThree</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.SR1</spring-cloud.version></properties><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></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
然后就是application.yml
spring:application:name: serviceThreezipkin:base-url: http://localhost:9411#eureka:
# client:
# serviceUrl:
# defaultZone: http://localhost:9999/eureka/server:port: 7779
注释掉注册中心的原因是,zipkin进行服务追踪的时候并不需要注册中心。
为了方便我就直接在入口函数写了
package com.example.demo;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;@RestController
@SpringBootApplication
public class ServiceThreeApplication {public static void main(String[] args) {SpringApplication.run(ServiceThreeApplication.class, args);}@Autowiredprivate RestTemplate restTemplate;@Beanpublic RestTemplate getRestTemplate(){return new RestTemplate();}@RequestMapping("/getInfo")public String info(){return "I'm serviceThree";}@RequestMapping("/callServiceFour")public String callServiceTwo(){return restTemplate.getForObject("http://localhost:7780/getInfo",String.class)+"form serviceThree";}@Beanpublic Sampler defaultSampler() {return Sampler.ALWAYS_SAMPLE;}}
接下来就是serviceFour,这次为了稍微规范一点,改了一下目录结构,这个无所谓的。
然后是pom文件,
<?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><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>serviceFour</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.SR1</spring-cloud.version></properties><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></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
然后就是配置文件
spring.application.name=serviceFour
spring.zipkin.base-url=http://localhost:9411
server.port=7780
#eureka.client.service-url.defaultZone=http://localhost:9999/eureka/
之后就是写的测试调用
package com.example.demo.controller;import brave.sampler.Sampler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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;/*** Created by chenzhen on 2018/8/7.*/
@RestController
public class xxxController {private Logger logger = LoggerFactory.getLogger(xxxController.class);@AutowiredRestTemplate restTemplate;@Beanpublic RestTemplate getRestTemplate(){return new RestTemplate();}@RequestMapping("/getInfo")public String getInfo(){return "I'm serviceFour";}@RequestMapping("/callServiceThree")public String callServiceThree(){return restTemplate.getForObject("http://localhost:7779/getInfo",String.class)+" form serviceFour";}@Beanpublic Sampler defaultSampler() {return Sampler.ALWAYS_SAMPLE;}
}
两个都写完之后,访问
http://localhost:7780/callServiceThree
http://localhost:7779/callServiceFour
之后再打开zipkin的追踪页面就会发现
http://localhost:9411/zipkin/dependency/
至此,整个小demo的服务依赖关系已经追踪完毕。
但是就目前来看,用zipkin来进行服务链路追踪,对代码有入侵,比如
代码中加的如下的代码就很重要。
@Beanpublic Sampler defaultSampler() {return Sampler.ALWAYS_SAMPLE;}
还有就是配置文件中需要配置zipkin的url,
spring.zipkin.base-url=http://localhost:9411
以及在pom中要引入zipkin依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></dependency>
以上三者缺一不可。
Spring Cloud学习笔记---Spring Cloud Sleuth--新建两个互相调用的服务测试zipkin相关推荐
- Dynamic CRM 2013学习笔记(二十二)插件里调用WCF服务
1. 添加service: 2.调用WCF BasicHttpBinding myBinding = new BasicHttpBinding(); myBinding.Name = "Ba ...
- Spring Boot学习笔记——Spring Boot与ActiveMQ的集成
Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ.Artemis等.这里以ActiveMQ为 ...
- [Spring入门学习笔记][Spring的AOP原理]
AOP是什么? 面向切面编程 软件工程有一个基本原则叫做"关注点分离"(Concern Separation),通俗的理解就是不同的问题交给不同的部分去解决,每部分专注于解决自己的 ...
- spring揭密学习笔记
spring揭密学习笔记 spring揭密学习笔记 spring揭密学习笔记(1) --spring的由来 spring揭密学习笔记(2)-spring ioc容器:IOC的基本概念 posted o ...
- Spring Cloud 学习笔记(2 / 3)
Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(3 / 3) - - - 56_Hystrix之全局服务降级DefaultProperties 57_Hystri ...
- Spring Cloud 学习笔记(2 3)
Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(3 / 3) - - - 56_Hystrix之全局服务降级DefaultProperties 57_Hystri ...
- Spring Cloud 学习笔记(1 / 3)
Spring Cloud 学习笔记(2 / 3) Spring Cloud 学习笔记(3 / 3) - - - 01_前言闲聊和课程说明 02_零基础微服务架构理论入门 03_第二季Boot和Clou ...
- Spring Cloud 学习笔记(3 3)
Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(2 / 3) - - - 108_Nacos之Linux版本安装 109_Nacos集群配置(上) 110_Nac ...
- Spring Cloud 学习笔记(3 / 3)
Spring Cloud 学习笔记(1 / 3) Spring Cloud 学习笔记(2 / 3) - - - 108_Nacos之Linux版本安装 109_Nacos集群配置(上) 110_Nac ...
- Spring Cloud学习笔记—网关Spring Cloud Gateway官网教程实操练习
Spring Cloud学习笔记-网关Spring Cloud Gateway官网教程实操练习 1.Spring Cloud Gateway介绍 2.在Spring Tool Suite4或者IDEA ...
最新文章
- 给Python加Markdown式排版,在线运行可做Jupyter替身丨谷歌大脑出品
- idea console中文乱码_Python3的字符编码乱码问题解决思路
- 简单几步制作软raid
- geometry-api-java 学习笔记(七)拓扑运算之cut
- C语言中的格式化打印printf/sprintf以及嵌入式printf重定向进行DEBUG
- python jsonpath-rw_Python使用jsonpath-rw模块处理Json对象操作示例
- 金融数据公司发展趋势小探
- python基础-字典的增删改查
- header.vue 调用变量,别的组件导入引用,组件方法事例实例
- Servlet JSP - 转发与重定向的区别
- 走进音视频的世界——杜比音效之AC3与AC4
- 机器人控制框架行为树py_trees <一、行为树介绍>
- Matlab 批量读取,处理及保存图片
- Linux设置每分钟、每小时、每天、每周、每月、每年定时执行
- 操作系统虚拟内存的作用概括
- SEO优化:关于网站降权的方方面面!!!
- 【附源码】计算机毕业设计SSM七七美食汇
- 漏洞库:爬取NVD-美国国家信息安全漏洞库
- 移动路由猫虚拟服务器,移动路由器连接光猫怎么设置?
- Android系统自带的层次状态机StateMachine(Hierarchical State Machine)
热门文章
- 如何在 Ubuntu 16.10 的 Unity 8 上运行老式 Xorg 程序
- 使用grub2制作U盘启动盘安装操作系统
- mysql 查询一年中每个月份的数据量
- 交换机路由器常用命令
- java模板引擎哪个好_浅谈Java模板引擎性能对比
- 循环队列和链队的表示和实现
- 控制反转和依赖注入的理解(通俗易懂)_Spring 的本质系列之依赖注入_一点课堂(多岸学院)...
- 布线问题 nyoj38
- java 继承特点_java 继承特点
- python如何输入数据形成列表_将Python字典/列表插入到SQL数据库中最有效的方法是什么?...