SpringCloud 2.x学习笔记:8、Spring Cloud Sleuth(Greenwich版本)

1、服务追踪组件zipkin

Spring Cloud Sleuth集成了zipkin组件

2、构建server-zipkin

从spring Cloud为F版本开始,已经不需要自己构建Zipkin Server了,只需要下载jar运行即可。

命令:java -jar zipkin-server-2.12.9-exec.jar

D:\Users\chengyq>c:

C:\>java -jar zipkin-server-2.12.9-exec.jar

在这里插入图片描述
浏览器打开

http://localhost:9411

在这里插入图片描述

3、构建server-x

3.1 pom.xml

<?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.cntaiping.tpa</groupId>
    <artifactId>server-x</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>server-x</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.cntaiping.tpa</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <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>
    </dependencies>

</project>

3.2 application.properties

server.port=7201
spring.zipkin.base-url=http://localhost:9411
spring.application.name=server-x

3.3 Application类

package com.cntaiping.tpa.serverx;

import brave.sampler.Sampler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ServerXApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerXApplication.class, args);
    }

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }
}

3.4 控制器

package com.cntaiping.tpa.serverx.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
public class XController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RestTemplate restTemplate;



    @RequestMapping("/hello")
    public String hello(){
        logger.info("calling trace server-x ");
        return restTemplate.getForObject("http://localhost:7202/info", String.class);
    }
    @RequestMapping("/info")
    public String info(){
        logger.info("calling trace server-x ");
        return "i'm server-x";
    }

}

4、构建server-y

构建server-y与构建构建server-x过程相同,不同的项目文件如下

4.1 application.properties

server.port=7202
spring.zipkin.base-url=http://localhost:9411
spring.application.name=server-y

4.2 控制器

package com.cntaiping.tpa.servery.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class YController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/hello")
    public String hello(){
        logger.info("calling trace server-y ");
        return restTemplate.getForObject("http://localhost:7201/info", String.class);
    }
    @RequestMapping("/info")
    public String info(){
        logger.info("calling trace server-y ");
        return "i'm server-y";
    }

}

5、运行效果

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值