博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第九篇: Spring Cloud Sleuth(Hoxton版本)
阅读量:4182 次
发布时间:2019-05-26

本文共 4953 字,大约阅读时间需要 16 分钟。

一、 构建Zipkin Server,官方推荐下载jar即可:

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

java -jar zipkin-server-2.12.9-exec.jar

默认端口是9411 启动完成后访问:http://localhost:9411/

1610957798(1).jpg

二、创建service-hi

4.0.0
com.xiaobu
springcloud-demo
0.0.1-SNAPSHOT
service-hi
0.0.1-SNAPSHOT
service-hi
service-hi project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.cloud
spring-cloud-starter-zipkin
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin

application.properties

server.port=8988spring.zipkin.base-url=http://localhost:9411spring.application.name=service-hi

通过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url就可以了。

接口暴露:

package com.xiaobu;import brave.sampler.Sampler;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;/** * @author xiaobu */@SpringBootApplication@RestController@Slf4jpublic class ServiceHiApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args); } @Value("${server.port}") private String port; @Override public void run(String... args) throws Exception {
System.out.println("service-hi在端口: " +port+"启动完成....." ); } @Autowired private RestTemplate restTemplate; @Bean public RestTemplate getRestTemplate(){
return new RestTemplate(); } @GetMapping("/hi") public String callHome(){
log.info("calling trace service-hi "); return restTemplate.getForObject("http://localhost:8989/miya", String.class); } @GetMapping("/info") public String info(){
log.info("calling trace service-hi "); return "i'm service-hi"; } @Bean public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE; }}

三、 创建service-miya

相同的依赖,修改下server port端口以及服务名即可

接口暴露:

package com.xiaobu;import brave.sampler.Sampler;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@SpringBootApplication@Slf4j@RestControllerpublic class ServiceMiyaApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ServiceMiyaApplication.class, args); } @Value("${server.port}") private String port; @Override public void run(String... args) throws Exception {
log.info("service-miya在端口:{} 启动完成....." ,port); } @RequestMapping("/hi") public String home(){
log.info( "hi is being called"); return "hi i'm miya!"; } @GetMapping("/miya") public String info(){
log.info("info is being called"); return restTemplate.getForObject("http://localhost:8988/info",String.class); } @Autowired private RestTemplate restTemplate; @Bean public RestTemplate getRestTemplate(){
return new RestTemplate(); } @Bean public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE; }}

访问http://localhost:8989/miya 出现:

i’m service-hi

点击依赖:

1610959346(1).jpg

点击查找,查看具体服务相互调用的数据

1610959745(1).jpg

转载地址:http://ukgai.baihongyu.com/

你可能感兴趣的文章
C++程序员技术需求规划(发展方向)
查看>>
JNI
查看>>
Cardboard虚拟现实开发初步(二)
查看>>
60个优秀的免费3D模型下载网站
查看>>
Cardboard虚拟现实开发初步(三)
查看>>
Android native和h5混合开发几种常见的hybrid通信方式
查看>>
Vista/Win7 UAC兼容程序开发指南
查看>>
IOS程序开发框架
查看>>
安装jdk的步骤
查看>>
简述JAVA运算符
查看>>
简易ATM源代码及运行结果
查看>>
简述Java中的简单循环
查看>>
用JAVA实现各种乘法表
查看>>
for双重循环实现图形
查看>>
Java类和对象基础
查看>>
简述Java继承和多态
查看>>
Java中Arrays工具类的用法
查看>>
简述JAVA抽象类和接口
查看>>
JAVA常用基础类
查看>>
简述Java异常处理
查看>>