本文主要是介绍sleuth + kafka + zipkin,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、基本环境
- zipkin
- zookeeper
- kafka
- 一个eureka项目
- 一个消费者项目
- 一个提供者项目
二、zipkin下载与启动
参考 https://blog.csdn.net/u012965203/article/details/100006168
三、zookeeper下载与安装
参考 https://blog.csdn.net/u012965203/article/details/90302171
四、kafka下载与安装
参考 https://blog.csdn.net/u012965203/article/details/92634343
https://blog.csdn.net/u012965203/article/details/92796384
注意点:kafka配置问题(如果在阿里云)
五、创建eureka项目
(1)创建项目,命名00-eurekaserver-8000
(2)依赖
<?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.abc</groupId><artifactId>00-eurekaserver-8000</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><java.version>1.8</java.version><spring-cloud.version>Greenwich.SR2</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</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>
(3)application.yml配置
server:port: 8000eureka:instance:hostname: localhost # 指定Eureka主机client:register-with-eureka: false # 指定当前主机是否向Eureka服务器进行注册fetch-registry: false # 指定当前主机是否要从Eurka服务器下载服务注册列表service-url: # 服务暴露地址defaultZone: http://localhost:8000/eureka
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka# server:
# enable-self-preservation: false # 关闭自我保护
(4)启动类
package com.abc.eureka;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer // 开启Eureka服务
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}
六、创建消费项目
(1) 创建项目
(2)依赖
<?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.abc</groupId><artifactId>07-kafka-sleuth-consumer-8080</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><java.version>1.8</java.version><spring-cloud.version>Greenwich.SR1</spring-cloud.version></properties><dependencies><!--kafka依赖--><dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId></dependency><!--zipkin客户端依赖,其包含了sleuth依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></dependency><!--feign依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!--actuator依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--eureka客户端依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></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>
(3)application.yml配置
(4)实体类
package com.abc.consumer.bean;import lombok.Data;@Data
public class Depart {private Integer id;private String name;
}
(5)RestTemplate类
package com.abc.consumer.codeconfig;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 DepartCodeConfig {@LoadBalanced@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}
}
(6)控制层
package com.abc.consumer.controller;import com.abc.consumer.bean.Depart;
import com.abc.consumer.service.DepartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@Slf4j
@RestController
@RequestMapping("/consumer/depart")
public class DepartController {@Autowiredprivate DepartService service;@GetMapping("/get/{id}")public Depart getHandle(@PathVariable("id") int id) {log.info("消费者的处理器方法被调用");return service.getDepartById(id);}}
(7)接口层
package com.abc.consumer.service;import com.abc.consumer.bean.Depart;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;import java.util.List;@Service
@FeignClient("abcmsc-provider-depart")
@RequestMapping("/provider/depart")
public interface DepartService {boolean modifyDepart(@RequestBody Depart depart);@GetMapping("/get/{id}")Depart getDepartById(@PathVariable("id") int id);}
(8)启动类
package com.abc.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;// 指定Feign接口所在的包
@EnableFeignClients(basePackages = "com.abc.consumer.service")
@SpringBootApplication
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}}
七、创建提供者项目
(1)创建项目
(2)依赖
<?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.abc</groupId><artifactId>07-kafka-sleuth-provider-8081</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><java.version>1.8</java.version><spring-cloud.version>Greenwich.SR1</spring-cloud.version></properties><dependencies><!--kafka依赖--><dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId></dependency><!--zipkin客户端依赖,其包含了sleuth依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></dependency><!--actuator依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--eureka客户端依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><!--修改MySQL驱动版本--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></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>
(3)application.yml配置
(4)实体类
package com.abc.provider.bean;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Data
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Depart {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;private String name;
}
(5)数据链路层
package com.abc.provider.repository;import com.abc.provider.bean.Depart;
import org.springframework.data.jpa.repository.JpaRepository;// 第一个泛型:当前Repository的操作对象类型
// 第二个泛型:当前Repository的操作对象的id类型
public interface DepartRepository extends JpaRepository<Depart, Integer> {
}
(6)控制层
package com.abc.provider.controller;import com.abc.provider.bean.Depart;
import com.abc.provider.service.DepartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;import java.util.List;@Slf4j
@RequestMapping("/provider/depart")
@RestController
public class DepartController {@Autowiredprivate DepartService service;@GetMapping("/get/{id}")public Depart getHandle(@PathVariable("id") int id) {log.info("生产者的处理器方法被调用");return service.getDepartById(id);}}
(7)接口层
package com.abc.provider.service;import com.abc.provider.bean.Depart;import java.util.List;public interface DepartService {Depart getDepartById(int id);
}
(8)接口实现类
package com.abc.provider.service;import com.abc.provider.bean.Depart;
import com.abc.provider.repository.DepartRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class DepartServiceImpl implements DepartService {@Autowiredprivate DepartRepository repository;@Overridepublic Depart getDepartById(int id) {if(repository.existsById(id)) {return repository.getOne(id);}Depart depart = new Depart();depart.setName("no this depart");return depart;}}
(9)启动类
package com.abc.provider;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}
}
八、测试
(1)启动zookeeper
(2)启动kafka
(3)启动zipkin
java -DKAFKA_BOOTSTRAP_SERVERS=localhost:9092 -jar zipkin.jar
(4)启动eureka项目
(5)启动消费项目
(6)启动消费项目
(7)效果
请求 http://localhost:8080/consumer/depart/get/2之后
访问 http://localhost:9411/zipkin/
这篇关于sleuth + kafka + zipkin的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!