欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Cloud Sleuth

程序员文章站 2022-06-12 15:25:01
...

zipkin-server-2.10.1-exec.jar 下载地址zipkin-server-2.10.1-exec.jar
命令运行
java -jar zipkin-server-2.10.1-exec.jar
访问地址:http://localhost:9411/zipkin/

Spring Cloud Sleuth


spring-cloud-sleuth-provider

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zipkin</artifactId>
		</dependency>

application.yml


server:
  port: 6818

spring:
  application:
    name: spring-cloud-sleuth-provider


  zipkin:
    base-url: http://127.0.0.1:9411/
    ###全部采集
    # 默认是0.1 抽样采集,修改为1.0 为全部采集,但是影响接口使用效率
  sleuth:
    sampler:
      probability: 1.0

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:6888/eureka/

SpringCloudSleuthProviderApplication.java

package com.mingshine.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringCloudSleuthProviderApplication {

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

}

TestController.java

package com.mingshine.cloud;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class TestController {
    @Value("${server.port}")
    private String serverPort;

    @RequestMapping("/test")
    public String test() {
        return "spring-cloud-sleuth-provider: test========" + serverPort + "\t" + UUID.randomUUID().toString();
    }
}


spring-cloud-sleuth-consumer

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zipkin</artifactId>
		</dependency>

application.yml



server:
  port: 6819

spring:
  application:
    name: spring-cloud-sleuth-consumer


  zipkin:
    base-url: http://127.0.0.1:9411/
  sleuth:
    sampler:
      probability: 1.0

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:6888/eureka/

ApplicationContextConfig.java

package com.mingshine.cloud;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getTemplate(){
        return new RestTemplate();
    }
}

SpringCloudSleuthConsumerApplication.java

package com.mingshine.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringCloudSleuthConsumerApplication {

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

}

TestController.java

package com.mingshine.cloud;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class TestController {


    private static final String INVOKE_URL = "http://spring-cloud-sleuth-provider";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/test")
    public String test(){
        String result = restTemplate.getForObject(INVOKE_URL + "/test", String.class);
        return result;
    }

}

相关标签: Spring Cloud