一、前置条件
- 1、安装zookeeper
zookeeper官方文档 - 2、安装kafka
kafka官方文档 - 3、安装elasticsearch
elasticsearch官方文档
二、搭建spring cloud应用
-
1、创建zipkin-server 应用
-
1.1 基于zipkin-server可执行jar文件
-
1.2 基于springcloud
-
1.2.1 添加依赖
-
-
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId>
<version>2.4.1</version>
</dependency>
-
1.2.2 在启动类上添加注解
@SpringBootApplication
@EnableZipkinStreamServer
public class ServerZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ServerZipkinApplication.class, args);
}
}
-
1.2.3 在application.properties配置kafka和elasticsearch
spring.cloud.stream.kafka.binder.brokers=localhost:9092
spring.cloud.stream.kafka.binder.zkNodes=localhost:2181
zipkin.storage.type=elasticsearch
zipkin.storage.elasticsearch.hosts=localhost:10200
zipkin.storage.elasticsearch.cluster=zipkin-test
zipkin.storage.elasticsearch.index=zipkin
zipkin.storage.elasticsearch.index-shards=1
zipkin.storage.elasticsearch.index-replicas=1
-
2、创建service-provider 应用
-
2.1 添加依赖
-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-stream</artifactId>
</dependency>
-
2.2 添加服务接口
@SpringBootApplication
@RestController
/**
* @author zhengwei
*/
public class ServiceProviderApplication {
private static final Logger LOG = Logger.getLogger(ServiceProviderApplication.class.getName());
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
@RequestMapping("/hi")
public String hi() {
LOG.log(Level.INFO, "calling trace service-provider");
return "hi";
}
}
-
2.3 在application.perperties里添加配置
server.port=8988
spring.application.name=service-provider
spring.sleuth.enabled=true
spring.sleuth.sampler.percentage = 1.0
spring.cloud.stream.kafka.binder.brokers=localhost:9092
spring.cloud.stream.kafka.binder.zkNodes=localhost:2181
-
3、创建service-consumer应用
-
3.1 添加依赖
-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-stream</artifactId>
</dependency>
-
3.2 添加HelloController
@RestController
public class HelloController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/hi")
public String hi(@RequestParam String name) {
return restTemplate.getForObject("http://localhost:8988/hi?name=" + name, String.class);
}
}
-
3.3 在application.properties添加配置项
server.port=8765
spring.application.name=service-consumer
spring.sleuth.enabled=true
spring.sleuth.sampler.percentage = 1.0
spring.cloud.stream.kafka.binder.brokers=localhost:9092
spring.cloud.stream.kafka.binder.zkNodes=localhost:2181
-
3.4 添加应用启动类
@SpringBootApplication
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
-
三、启动应用
-
1 启动zkipin-server服务
ServerZipkinApplication
-
2 启动service-provider服务
ServiceProviderApplication
-
3 启动service-consumer服务
ServiceConsumerApplication
-
-
四、访问consumer url和zipkin server
-
1 打开游览器访问服务消费方
-
2 访问zipkin server
-
-
五、demo 源码地址